From b58259ec1a9765630bcf20a36cdcdc10ef0e80d0 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Mon, 29 Sep 2008 20:18:18 -0700 Subject: [PATCH] implemented view argument currying --- lib/couchrest/core/model.rb | 20 ++++++++++++-------- spec/couchrest/core/model_spec.rb | 28 +++++++++++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index ccbf91a..ff6e140 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -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 \ No newline at end of file diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index f92ca7a..fc490b2 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -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 \ No newline at end of file