implemented view argument currying
This commit is contained in:
parent
6a86a00d21
commit
b58259ec1a
2 changed files with 33 additions and 15 deletions
|
@ -127,15 +127,19 @@ module CouchRest
|
|||
module MagicViews
|
||||
def view_by *keys
|
||||
opts = keys.pop if keys.last.is_a?(Hash)
|
||||
opts ||= {}
|
||||
type = self.to_s
|
||||
|
||||
method_name = "by_#{keys.join('_and_')}"
|
||||
@@design_doc ||= default_design_doc
|
||||
|
||||
if opts && opts[:map]
|
||||
if opts[:map]
|
||||
view = {}
|
||||
view['map'] = opts[:map]
|
||||
view['reduce'] = opts[:reduce] if opts[:reduce]
|
||||
view['map'] = opts.delete(:map)
|
||||
if opts[:reduce]
|
||||
view['reduce'] = opts.delete(:reduce)
|
||||
opts[:reduce] = false
|
||||
end
|
||||
@@design_doc['views'][method_name] = view
|
||||
else
|
||||
doc_keys = keys.collect{|k|"doc['#{k}']"}
|
||||
|
@ -157,17 +161,18 @@ module CouchRest
|
|||
|
||||
self.meta_class.instance_eval do
|
||||
define_method method_name do |*args|
|
||||
opts = args[0] || {}
|
||||
query = opts.merge(args[0] || {})
|
||||
query[:raw] = true if query[:reduce]
|
||||
unless @@design_doc_fresh
|
||||
refresh_design_doc
|
||||
end
|
||||
raw = opts.delete(:raw)
|
||||
raw = query.delete(:raw)
|
||||
view_name = "#{type}/#{method_name}"
|
||||
|
||||
view = fetch_view(view_name, query)
|
||||
if raw
|
||||
fetch_view(view_name, opts)
|
||||
view
|
||||
else
|
||||
view = fetch_view(view_name, opts)
|
||||
# TODO this can be optimized once the include-docs patch is applied
|
||||
view['rows'].collect{|r|new(database.get(r['id']))}
|
||||
end
|
||||
|
@ -239,6 +244,5 @@ module CouchRest
|
|||
model.send(:include, Callbacks)
|
||||
end
|
||||
|
||||
|
||||
end # module Model
|
||||
end # module CouchRest
|
|
@ -9,7 +9,7 @@ class Article
|
|||
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
|
||||
unique_id :slug
|
||||
|
||||
view_by :date
|
||||
view_by :date, :descending => true
|
||||
view_by :user_id, :date
|
||||
|
||||
view_by :tags,
|
||||
|
@ -28,7 +28,7 @@ class Article
|
|||
|
||||
key_writer :date
|
||||
key_reader :slug, :created_at, :updated_at
|
||||
key_accessor :title
|
||||
key_accessor :title, :tags
|
||||
|
||||
timestamps!
|
||||
|
||||
|
@ -200,7 +200,7 @@ describe CouchRest::Model do
|
|||
end
|
||||
end
|
||||
|
||||
describe "a model with simple views" do
|
||||
describe "a model with simple views and a default param" do
|
||||
before(:all) do
|
||||
written_at = Time.now - 24 * 3600 * 7
|
||||
@titles = ["this and that", "also interesting", "more fun", "some junk"]
|
||||
|
@ -223,8 +223,13 @@ describe CouchRest::Model do
|
|||
view['rows'].length.should == 4
|
||||
end
|
||||
|
||||
it "should return the matching object" do
|
||||
it "should return the matching objects (with descending)" do
|
||||
articles = Article.by_date
|
||||
articles.collect{|a|a.title}.should == @titles.reverse
|
||||
end
|
||||
|
||||
it "should allow you to override default args" do
|
||||
articles = Article.by_date :descending => false
|
||||
articles.collect{|a|a.title}.should == @titles
|
||||
end
|
||||
end
|
||||
|
@ -267,12 +272,21 @@ describe CouchRest::Model do
|
|||
u = i % 2
|
||||
a = Article.new(:title => title, :tags => [@tags[u]])
|
||||
a.save
|
||||
puts a.inspect
|
||||
end
|
||||
end
|
||||
it "should be available raw" do
|
||||
view = Article.by_tags :raw => true, :group => true
|
||||
view.should == 'x'
|
||||
view = Article.by_tags :raw => true
|
||||
view['rows'].length.should == 5
|
||||
end
|
||||
|
||||
it "should be default to :reduce => false" do
|
||||
ars = Article.by_tags
|
||||
ars.first.tags.first.should == 'cool'
|
||||
end
|
||||
|
||||
it "should be raw when reduce is true" do
|
||||
view = Article.by_tags :reduce => true, :group => true
|
||||
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue