Finalizing support for including hash codes in Design documents

This commit is contained in:
Sam Lown 2011-04-14 00:19:10 +02:00
parent 706880fceb
commit 00a4cc7f3b
7 changed files with 25 additions and 94 deletions

2
.gitignore vendored
View file

@ -6,4 +6,4 @@ pkg
.bundle .bundle
couchdb.std* couchdb.std*
*.*~ *.*~
Gemfile.lock

View file

@ -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)

View file

@ -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 == 1.1.0.beta2
* Minor enhancements: * Minor enhancements:

View file

@ -391,11 +391,9 @@ module CouchRest
return self.result if result return self.result if result
raise "Database must be defined in model or view!" if use_database.nil? 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? 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) model.save_design_doc(use_database)
self.result = model.design_doc.view_on(use_database, name, query.reject{|k,v| v.nil?}) self.result = model.design_doc.view_on(use_database, name, query.reject{|k,v| v.nil?})

View file

@ -15,8 +15,16 @@ CouchRest::Design.class_eval do
base.delete('_rev') base.delete('_rev')
result = nil result = nil
flatten = flatten =
lambda {|v| lambda {|r|
v.is_a?(Hash) ? v.flatten.map{|v| flatten.call(v)}.flatten : v.to_s (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('')) Digest::MD5.hexdigest(flatten.call(base).sort.join(''))
end end

View file

@ -535,6 +535,7 @@ describe "Design View" do
# disable real execution! # disable real execution!
@design_doc = mock("DesignDoc") @design_doc = mock("DesignDoc")
@design_doc.stub!(:view_on) @design_doc.stub!(:view_on)
@obj.model.stub!(:save_design_doc)
@obj.model.stub!(:design_doc).and_return(@design_doc) @obj.model.stub!(:design_doc).and_return(@design_doc)
end end
@ -557,6 +558,12 @@ describe "Design View" do
@obj.send(:execute) @obj.send(:execute)
end 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 it "should populate the results" do
@obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:can_reduce?).and_return(true)
@design_doc.should_receive(:view_on).and_return('foos') @design_doc.should_receive(:view_on).and_return('foos')
@ -564,22 +571,6 @@ describe "Design View" do
@obj.result.should eql('foos') @obj.result.should eql('foos')
end 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 it "should remove nil values from query" do
@obj.should_receive(:can_reduce?).and_return(true) @obj.should_receive(:can_reduce?).and_return(true)
@obj.stub!(:use_database).and_return('database') @obj.stub!(:use_database).and_return('database')

View file

@ -32,7 +32,7 @@ RSpec.configure do |config|
cr = TEST_SERVER cr = TEST_SERVER
test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ } test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
test_dbs.each do |db| test_dbs.each do |db|
# cr.database(db).delete! rescue nil cr.database(db).delete! rescue nil
end end
end end
end end