diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index b0c33ff..61ce76b 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -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 diff --git a/lib/couchrest/helper/template-app/lib/helpers/math.js b/lib/couchrest/helper/template-app/lib/helpers/math.js new file mode 100644 index 0000000..2b0ee11 --- /dev/null +++ b/lib/couchrest/helper/template-app/lib/helpers/math.js @@ -0,0 +1 @@ +function stddev() {}; \ No newline at end of file diff --git a/lib/couchrest/helper/template-app/views/example/map.js b/lib/couchrest/helper/template-app/views/example/map.js index e3fdd80..11668f5 100644 --- a/lib/couchrest/helper/template-app/views/example/map.js +++ b/lib/couchrest/helper/template-app/views/example/map.js @@ -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); diff --git a/spec/couchapp_spec.rb b/spec/couchapp_spec.rb index 1fb4e3f..4d7fbba 100644 --- a/spec/couchapp_spec.rb +++ b/spec/couchapp_spec.rb @@ -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