From 8733b631e34c208382b9ba88c818987f8069d18e Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 1 Jan 2009 23:11:01 -0800 Subject: [PATCH] more reorg for couchapp --- lib/couchrest/helper/file_manager.rb | 93 +++++++++---------- .../{views/_lib => library/helpers}/helper.js | 0 .../templates}/example-template.html | 0 spec/couchapp_spec.rb | 5 +- 4 files changed, 50 insertions(+), 48 deletions(-) rename lib/couchrest/helper/template-app/{views/_lib => library/helpers}/helper.js (100%) rename lib/couchrest/helper/template-app/{forms/lib => library/templates}/example-template.html (100%) diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index 0f62aa6..f9e022e 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -14,7 +14,8 @@ module CouchRest "css" => "text/css", "js" => "test/javascript", "txt" => "text/plain" - } + } + def initialize(dbname, host="http://127.0.0.1:5984") @db = CouchRest.new(host).database(dbname) end @@ -25,8 +26,9 @@ module CouchRest attachdir = File.join(appdir,"_attachments") fields = dir_to_fields(appdir) - package_forms(fields["forms"]) - package_views(fields["views"]) + library = fields["library"] + package_forms(fields["forms"], library) + package_views(fields["views"], library) docid = "_design/#{appname}" design = @db.get(docid) rescue {} @@ -36,7 +38,38 @@ module CouchRest @db.save(design) push_directory(attachdir, docid) end - + + def dir_to_fields(dir) + fields = {} + (Dir["#{dir}/**/*.*"] - + Dir["#{dir}/_attachments/**/*.*"]).each do |file| + farray = file.sub(dir, '').sub(/^\//,'').split('/') + myfield = fields + while farray.length > 1 + front = farray.shift + myfield[front] ||= {} + myfield = myfield[front] + end + fname, fext = farray.shift.split('.') + fguts = File.open(file).read + if fext == 'json' + myfield[fname] = JSON.parse(fguts) + else + myfield[fname] = fguts + end + end + return fields + end + + + # Generate an application in the given directory. + # This is a class method because it doesn't depend on + # specifying a database. + def self.generate_app(app_dir) + templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app') + FileUtils.cp_r(templatedir, app_dir) + end + def push_directory(push_dir, docid=nil) docid ||= push_dir.split('/').reverse.find{|part|!part.empty?} @@ -116,55 +149,21 @@ module CouchRest end end - - def dir_to_fields(dir) - fields = {} - (Dir["#{dir}/**/*.*"] - - Dir["#{dir}/_attachments/**/*.*"]).each do |file| - farray = file.sub(dir, '').sub(/^\//,'').split('/') - myfield = fields - while farray.length > 1 - front = farray.shift - myfield[front] ||= {} - myfield = myfield[front] - end - fname, fext = farray.shift.split('.') - fguts = File.open(file).read - if fext == 'json' - myfield[fname] = JSON.parse(fguts) - else - myfield[fname] = fguts - end - end - return fields - end - - - # Generate an application in the given directory. - # This is a class method because it doesn't depend on - # specifying a database. - def self.generate_app(app_dir) - templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app') - FileUtils.cp_r(templatedir, app_dir) - end - private - def package_forms(funcs) - if funcs["lib"] - lib = "var lib = #{funcs["lib"].to_json};" - funcs.delete("lib") + def package_forms(funcs, library) + if library + lib = "var library = #{library.to_json};" apply_lib(funcs, lib) end end - def package_views(views) - if views["_lib"] - lib = "var lib = #{views["_lib"].to_json};" - views.delete("_lib") - end - views.each do |view, funcs| - apply_lib(funcs, lib) if lib + def package_views(views, library) + if library + lib = "var library = #{library.to_json};" + views.each do |view, funcs| + apply_lib(funcs, lib) if lib + end end end diff --git a/lib/couchrest/helper/template-app/views/_lib/helper.js b/lib/couchrest/helper/template-app/library/helpers/helper.js similarity index 100% rename from lib/couchrest/helper/template-app/views/_lib/helper.js rename to lib/couchrest/helper/template-app/library/helpers/helper.js diff --git a/lib/couchrest/helper/template-app/forms/lib/example-template.html b/lib/couchrest/helper/template-app/library/templates/example-template.html similarity index 100% rename from lib/couchrest/helper/template-app/forms/lib/example-template.html rename to lib/couchrest/helper/template-app/library/templates/example-template.html diff --git a/spec/couchapp_spec.rb b/spec/couchapp_spec.rb index 66a35b4..866bc69 100644 --- a/spec/couchapp_spec.rb +++ b/spec/couchapp_spec.rb @@ -34,7 +34,7 @@ describe "couchapp" do end it "should create a forms and libs" do Dir["#{@fixdir}/my-app/forms/*"].select{|x|x =~ /example-form.js/}.length.should == 1 - Dir["#{@fixdir}/my-app/forms/lib/*"].select{|x|x =~ /example-template.html/}.length.should == 1 + Dir["#{@fixdir}/my-app/library/templates/*"].select{|x|x =~ /example-template.html/}.length.should == 1 end end @@ -57,6 +57,9 @@ describe "couchapp" do it "should create the view libs" do @doc['views']['example']['map'].should match(/aHelperFunction/) end + it "should create specific view libs" do + @doc['views']['example']['map'].should match(/aHelperFunction/) + end it "should create view for all the views" do `mkdir -p #{@fixdir}/my-app/views/more` `echo 'moremap' > #{@fixdir}/my-app/views/more/map.js`