more reorg for couchapp

This commit is contained in:
Chris Anderson 2009-01-01 23:11:01 -08:00
parent c435aa0932
commit 8733b631e3
4 changed files with 50 additions and 48 deletions

View file

@ -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

View file

@ -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`