removed metaprogramming

This commit is contained in:
Chris Anderson 2008-10-14 15:30:38 -07:00
parent 320f6b99aa
commit 59f81d2d77
2 changed files with 28 additions and 19 deletions

View file

@ -297,20 +297,25 @@ module CouchRest
'map' => map_function 'map' => map_function
} }
end end
generated_design_doc['views'][method_name]['couchrest-defaults'] = opts
self.design_doc_fresh = false self.design_doc_fresh = false
method_name
end
self.meta_class.instance_eval do def method_missing m, *args
define_method method_name do |*args| if opts = has_view?(m)
query = opts.merge(args[0] || {}) query = args.shift || {}
query[:raw] = true if query[:reduce] view(m, opts.merge(query), *args)
unless design_doc_fresh else
refresh_design_doc super
end end
raw = query.delete(:raw)
view_name = "#{design_doc_slug}/#{method_name}"
fetch_view_with_docs(view_name, query, raw)
end end
# returns true if the there is a view named this in the design doc
def has_view?(view)
view = view.to_s
if generated_design_doc['views'][view]
generated_design_doc['views'][view]["couchrest-defaults"]
end end
end end
@ -321,26 +326,29 @@ module CouchRest
# Dispatches to any named view. # Dispatches to any named view.
def view name, query={}, &block def view name, query={}, &block
name = name.to_s unless design_doc_fresh
refresh_design_doc
end
query[:raw] = true if query[:reduce]
raw = query.delete(:raw)
view_name = "#{design_doc_slug}/#{name}" view_name = "#{design_doc_slug}/#{name}"
fetch_view_with_docs(view_name, query, true, &block) fetch_view_with_docs(view_name, query, raw, &block)
end end
private private
def fetch_view_with_docs name, opts, raw=false, &block def fetch_view_with_docs name, opts, raw=false, &block
if raw if raw
fetch_view name, opts, &block fetch_view name, opts, &block
else else
begin begin
view = fetch_view name, opts.merge({:include_docs => true}), &block view = fetch_view name, opts.merge({:include_docs => true}), &block
view['rows'].collect{|r|new(r['doc'])} if view view['rows'].collect{|r|new(r['doc'])} if view['rows']
rescue rescue
# fallback for old versions of couchdb that don't # fallback for old versions of couchdb that don't
# have include_docs support # have include_docs support
view = fetch_view name, opts, &block view = fetch_view name, opts, &block
view['rows'].collect{|r|new(database.get(r['id']))} if view view['rows'].collect{|r|new(database.get(r['id']))} if view['rows']
end end
end end
end end

View file

@ -431,7 +431,7 @@ describe CouchRest::Model do
# register methods with method-missing, for local dispatch. method # register methods with method-missing, for local dispatch. method
# missing lookup table, no heuristics. # missing lookup table, no heuristics.
view = Course.view :by_title view = Course.view :by_title
designed = Course.by_title :raw => true designed = Course.by_title
view.should == designed view.should == designed
end end
it "should get them" do it "should get them" do
@ -445,7 +445,8 @@ describe CouchRest::Model do
# puts "course" # puts "course"
courses << course courses << course
end end
courses[0]["key"].should =='aaa' # courses.should == 'x'
courses[0]["doc"]["title"].should =='aaa'
end end
end end