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) def apply_lib(funcs)
funcs.each do |k,v| funcs.each do |k,v|
next unless v.is_a?(String) next unless v.is_a?(String)
funcs[k] = preprocess_func(v) funcs[k] = process_require(process_include(v))
end end
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 # process includes
included = {} included = {}
@ -191,24 +205,16 @@ module CouchRest
end end
# puts included.inspect # puts included.inspect
if included == {} rval = if included == {}
return f_string f_string
else else
# process requires
puts "\n\n\n\nBEFORE"
puts f_string
puts "\nAFTER"
# puts f_string
varstrings = included.collect do |k, v| varstrings = included.collect do |k, v|
"var #{k} = #{v.to_json};" "var #{k} = #{v.to_json};"
end end
f_string.sub /(\/\/|#)\ ?!include (.*)/, varstrings.join("\n")
rst = f_string.sub /(\/\/|#)\ ?!include (.*)/, varstrings.join("\n")
puts rst
return rst
end end
rval
end end

View file

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

View file

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

View file

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