From 00a4cc7f3b7db966a0484f0aa0b474b44c5492fc Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 14 Apr 2011 00:19:10 +0200 Subject: [PATCH] Finalizing support for including hash codes in Design documents --- .gitignore | 2 +- Gemfile.lock | 71 ------------------- history.txt | 5 ++ lib/couchrest/model/designs/view.rb | 4 +- .../model/support/couchrest_design.rb | 12 +++- spec/couchrest/designs/view_spec.rb | 23 ++---- spec/spec_helper.rb | 2 +- 7 files changed, 25 insertions(+), 94 deletions(-) delete mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index b0683b0..8eba16e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,4 @@ pkg .bundle couchdb.std* *.*~ - +Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index e6e03aa..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,71 +0,0 @@ -PATH - remote: . - specs: - couchrest_model (1.1.0.beta2) - activemodel (~> 3.0.0) - couchrest (= 1.1.0.pre2) - mime-types (~> 1.15) - railties (~> 3.0.0) - tzinfo (~> 0.3.22) - -GEM - remote: http://rubygems.org/ - specs: - abstract (1.0.0) - actionpack (3.0.6) - activemodel (= 3.0.6) - activesupport (= 3.0.6) - builder (~> 2.1.2) - erubis (~> 2.6.6) - i18n (~> 0.5.0) - rack (~> 1.2.1) - rack-mount (~> 0.6.14) - rack-test (~> 0.5.7) - tzinfo (~> 0.3.23) - activemodel (3.0.6) - activesupport (= 3.0.6) - builder (~> 2.1.2) - i18n (~> 0.5.0) - activesupport (3.0.6) - builder (2.1.2) - couchrest (1.1.0.pre2) - json (~> 1.5.1) - mime-types (~> 1.15) - rest-client (~> 1.6.1) - diff-lcs (1.1.2) - erubis (2.6.6) - abstract (>= 1.0.0) - i18n (0.5.0) - json (1.5.1) - mime-types (1.16) - rack (1.2.2) - rack-mount (0.6.14) - rack (>= 1.0.0) - rack-test (0.5.7) - rack (>= 1.0) - railties (3.0.6) - actionpack (= 3.0.6) - activesupport (= 3.0.6) - rake (>= 0.8.7) - thor (~> 0.14.4) - rake (0.8.7) - rest-client (1.6.1) - mime-types (>= 1.16) - rspec (2.5.0) - rspec-core (~> 2.5.0) - rspec-expectations (~> 2.5.0) - rspec-mocks (~> 2.5.0) - rspec-core (2.5.1) - rspec-expectations (2.5.0) - diff-lcs (~> 1.1.2) - rspec-mocks (2.5.0) - thor (0.14.6) - tzinfo (0.3.26) - -PLATFORMS - ruby - -DEPENDENCIES - couchrest_model! - rack-test (>= 0.5.7) - rspec (>= 2.0.0) diff --git a/history.txt b/history.txt index 986cf0b..a967347 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,8 @@ +== 1.1.0.beta3 + +* Minor enhancements: + * Adding "couchrest-hash" to Design Docs with aim to improve view update handling. + == 1.1.0.beta2 * Minor enhancements: diff --git a/lib/couchrest/model/designs/view.rb b/lib/couchrest/model/designs/view.rb index de7a54d..886632f 100644 --- a/lib/couchrest/model/designs/view.rb +++ b/lib/couchrest/model/designs/view.rb @@ -391,11 +391,9 @@ module CouchRest return self.result if result raise "Database must be defined in model or view!" if use_database.nil? - # Remove the reduce value if its not needed + # Remove the reduce value if its not needed to prevent CouchDB errors query.delete(:reduce) unless can_reduce? - # Save the design doc for the current database. This should be efficient - # and check for changes model.save_design_doc(use_database) self.result = model.design_doc.view_on(use_database, name, query.reject{|k,v| v.nil?}) diff --git a/lib/couchrest/model/support/couchrest_design.rb b/lib/couchrest/model/support/couchrest_design.rb index d91adbc..bec1ab1 100644 --- a/lib/couchrest/model/support/couchrest_design.rb +++ b/lib/couchrest/model/support/couchrest_design.rb @@ -15,8 +15,16 @@ CouchRest::Design.class_eval do base.delete('_rev') result = nil flatten = - lambda {|v| - v.is_a?(Hash) ? v.flatten.map{|v| flatten.call(v)}.flatten : v.to_s + lambda {|r| + (recurse = lambda {|v| + if v.is_a?(Hash) + v.to_a.map{|v| recurse.call(v)}.flatten + elsif v.is_a?(Array) + v.flatten.map{|v| recurse.call(v)} + else + v.to_s + end + }).call(r) } Digest::MD5.hexdigest(flatten.call(base).sort.join('')) end diff --git a/spec/couchrest/designs/view_spec.rb b/spec/couchrest/designs/view_spec.rb index 3116c19..44dbdfc 100644 --- a/spec/couchrest/designs/view_spec.rb +++ b/spec/couchrest/designs/view_spec.rb @@ -535,6 +535,7 @@ describe "Design View" do # disable real execution! @design_doc = mock("DesignDoc") @design_doc.stub!(:view_on) + @obj.model.stub!(:save_design_doc) @obj.model.stub!(:design_doc).and_return(@design_doc) end @@ -557,6 +558,12 @@ describe "Design View" do @obj.send(:execute) end + it "should call to save the design document" do + @obj.should_receive(:can_reduce?).and_return(false) + @obj.model.should_receive(:save_design_doc).with(DB) + @obj.send(:execute) + end + it "should populate the results" do @obj.should_receive(:can_reduce?).and_return(true) @design_doc.should_receive(:view_on).and_return('foos') @@ -564,22 +571,6 @@ describe "Design View" do @obj.result.should eql('foos') end - it "should retry once on a resource not found error" do - @obj.should_receive(:can_reduce?).and_return(true) - @obj.model.should_receive(:save_design_doc) - @design_doc.should_receive(:view_on).ordered.and_raise(RestClient::ResourceNotFound) - @design_doc.should_receive(:view_on).ordered.and_return('foos') - @obj.send(:execute) - @obj.result.should eql('foos') - end - - it "should retry twice and fail on a resource not found error" do - @obj.should_receive(:can_reduce?).and_return(true) - @obj.model.should_receive(:save_design_doc) - @design_doc.should_receive(:view_on).twice.and_raise(RestClient::ResourceNotFound) - lambda { @obj.send(:execute) }.should raise_error(RestClient::ResourceNotFound) - end - it "should remove nil values from query" do @obj.should_receive(:can_reduce?).and_return(true) @obj.stub!(:use_database).and_return('database') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8b234ac..3a865b5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,7 +32,7 @@ RSpec.configure do |config| cr = TEST_SERVER test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ } test_dbs.each do |db| - # cr.database(db).delete! rescue nil + cr.database(db).delete! rescue nil end end end