require system

This commit is contained in:
Chris Anderson 2009-01-02 03:46:26 -08:00
parent ba6caf8d41
commit 0341b039a7
4 changed files with 29 additions and 18 deletions

View file

@ -163,11 +163,25 @@ module CouchRest
def apply_lib(funcs)
funcs.each do |k,v|
next unless v.is_a?(String)
funcs[k] = preprocess_func(v)
funcs[k] = process_require(process_include(v))
end
end
def preprocess_func(f_string)
# process requires
def process_require(f_string)
f_string.gsub /(\/\/|#)\ ?!require (.*)/ do
fields = $2.split('.')
library = @doc
fields.each do |field|
library = library[field]
break unless library
end
library
end
end
def process_include(f_string)
# process includes
included = {}
@ -191,24 +205,16 @@ module CouchRest
end
# puts included.inspect
if included == {}
return f_string
rval = if included == {}
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
f_string.sub /(\/\/|#)\ ?!include (.*)/, varstrings.join("\n")
end
rval
end

View file

@ -0,0 +1 @@
function stddev() {};

View file

@ -1,8 +1,8 @@
// an example map function, emits the doc id
// and the list of keys it contains
// !require lib.helpers.math
function(doc) {
// !include lib
var k, keys = []
for (k in doc) keys.push(k);
emit(doc._id, keys);

View file

@ -55,7 +55,8 @@ describe "couchapp" do
@doc['views']['example']['map'].should match(/function/)
end
it "should create the view libs" do
@doc['views']['example']['map'].should match(/Resig/)
@doc['views']['example']['map'].should match(/stddev/)
@doc['forms']['example-form'].should_not match(/\"helpers\"/)
end
it "should create view for all the views" do
`mkdir -p #{@fixdir}/my-app/views/more`
@ -72,7 +73,10 @@ describe "couchapp" do
end
it "should allow deeper includes" do
@doc['forms']['example-form'].should_not match(/\"helpers\"/)
end
it "deep requires" do
@doc['forms']['example-form'].should_not match(/\"template\"/)
@doc['forms']['example-form'].should match(/Resig/)
end
end