deep include

This commit is contained in:
Chris Anderson 2009-01-02 03:22:28 -08:00
parent 7644217add
commit ba6caf8d41
2 changed files with 62 additions and 21 deletions

View file

@ -25,14 +25,13 @@ module CouchRest
viewdir = File.join(appdir,"views") viewdir = File.join(appdir,"views")
attachdir = File.join(appdir,"_attachments") attachdir = File.join(appdir,"_attachments")
fields = dir_to_fields(appdir) @doc = dir_to_fields(appdir)
library = fields["lib"] package_forms(@doc["forms"])
package_forms(fields["forms"], library) package_views(@doc["views"])
package_views(fields["views"], library)
docid = "_design/#{appname}" docid = "_design/#{appname}"
design = @db.get(docid) rescue {} design = @db.get(docid) rescue {}
design.merge!(fields) design.merge!(@doc)
design['_id'] = docid design['_id'] = docid
# design['language'] = lang if lang # design['language'] = lang if lang
@db.save(design) @db.save(design)
@ -151,30 +150,68 @@ module CouchRest
private private
def package_forms(funcs, library) def package_forms(funcs)
if library apply_lib(funcs)
lib = "var lib = #{library.to_json};" end
apply_lib(funcs, lib)
def package_views(views)
views.each do |view, funcs|
apply_lib(funcs)
end end
end end
def package_views(views, library) def apply_lib(funcs)
if library
lib = "var lib = #{library.to_json};"
puts lib
views.each do |view, funcs|
apply_lib(funcs, lib) if lib
end
end
end
def apply_lib(funcs, lib)
funcs.each do |k,v| funcs.each do |k,v|
next unless v.is_a?(String) next unless v.is_a?(String)
funcs[k] = v.sub(/(\/\/|#)\ ?!include lib/,lib) funcs[k] = preprocess_func(v)
end end
end end
def preprocess_func(f_string)
# process includes
included = {}
f_string.gsub /(\/\/|#)\ ?!include (.*)/ do
fields = $2.split('.')
library = @doc
include_to = included
count = fields.length
fields.each_with_index do |field, i|
break unless library[field]
library = library[field]
# normal case
if i+1 < count
include_to[field] = include_to[field] || {}
include_to = include_to[field]
else
# last one
include_to[field] = library
end
end
end
# puts included.inspect
if included == {}
return f_string
else
# process requires
puts "\n\n\n\nBEFORE"
puts f_string
puts "\nAFTER"
# puts f_string
varstrings = included.collect do |k, v|
"var #{k} = #{v.to_json};"
end
rst = f_string.sub /(\/\/|#)\ ?!include (.*)/, varstrings.join("\n")
puts rst
return rst
end
end
def say words def say words
puts words if @loud puts words if @loud
end end

View file

@ -70,6 +70,10 @@ describe "couchapp" do
it "should push the forms" do it "should push the forms" do
@doc['forms']['example-form'].should match(/Generated CouchApp Form Template/) @doc['forms']['example-form'].should match(/Generated CouchApp Form Template/)
end end
it "should allow deeper includes" do
@doc['forms']['example-form'].should_not match(/\"helpers\"/)
end
end end
describe "push . #{TESTDB}" do describe "push . #{TESTDB}" do