Permalink
Browse files

Use load method instead of bulk_load to load several documents

  • Loading branch information...
matthiasjakel committed Feb 26, 2012
1 parent 563cb58 commit 8d9e2f65d2fff56ac8b3d7d4d10cffbefe46b200
Showing with 74 additions and 81 deletions.
  1. +1 −1 README.md
  2. +23 −26 lib/couch_potato/database.rb
  3. +50 −54 spec/unit/database_spec.rb
View
@@ -163,7 +163,7 @@ You can of course also retrieve your instance:
You can also load a bunch of documents with one request.
- CouchPotato.database.bulk_load ['user1', 'user2', 'user3'] # => [<#User 0x3075>, <#User 0x3076>, <#User 0x3077>]
+ CouchPotato.database.load ['user1', 'user2', 'user3'] # => [<#User 0x3075>, <#User 0x3076>, <#User 0x3077>]
#### Properties
@@ -99,40 +99,30 @@ def destroy_document(document)
end
alias_method :destroy, :destroy_document
- # loads a document by its id
+ # loads a document by its id(s)
def load_document(id)
raise "Can't load a document without an id (got nil)" if id.nil?
- begin
- instance = couchrest_database.get(id)
- instance.database = self
- instance
- rescue(RestClient::ResourceNotFound)
- nil
+
+ if id.is_a?(Array)
+ bulk_load id
+ else
+ begin
+ instance = couchrest_database.get(id)
+ instance.database = self
+ instance
+ rescue(RestClient::ResourceNotFound)
+ nil
+ end
end
end
alias_method :load, :load_document
def load!(id)
- load(id) || raise(CouchPotato::NotFound)
- end
-
- def bulk_load(ids)
- if ids.is_a?(Array)
- response = couchrest_database.bulk_load ids
- existing_rows = response['rows'].select{|row| row.key? 'doc'}
- docs = existing_rows.map{|row| row["doc"]}
- docs.each{|doc| doc.database = self}
- else
- raise "Input must be an array."
- end
- end
-
- def bulk_load!(ids)
- docs = bulk_load(ids)
- if ids.length > docs.length
- missing_docs = ids - docs.map(&:id)
- raise CouchPotato::NotFound, missing_docs
+ doc = load(id)
+ if id.is_a?(Array)
+ missing_docs = id - doc.map(&:id)
end
+ raise(CouchPotato::NotFound, missing_docs) if doc.nil? || missing_docs
end
def inspect #:nodoc:
@@ -145,6 +135,13 @@ def couchrest_database
end
private
+
+ def bulk_load(ids)
+ response = couchrest_database.bulk_load ids
+ existing_rows = response['rows'].select{|row| row.key? 'doc'}
+ docs = existing_rows.map{|row| row["doc"]}
+ docs.each{|doc| doc.database = self}
+ end
def create_document(document, validate)
document.database = self
View
@@ -45,8 +45,11 @@ class Child
end
describe CouchPotato::Database, 'load' do
+
+ let(:couchrest_db) { stub('couchrest db', :info => nil) }
+ let(:db) { CouchPotato::Database.new couchrest_db }
+
it "should raise an exception if nil given" do
- db = CouchPotato::Database.new(stub('couchrest db', :info => nil))
lambda {
db.load nil
}.should raise_error("Can't load a document without an id (got nil)")
@@ -55,75 +58,68 @@ class Child
it "should set itself on the model" do
user = mock('user').as_null_object
DbTestUser.stub!(:new).and_return(user)
- db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => DbTestUser.json_create({JSON.create_id => 'DbTestUser'})))
+ couchrest_db.stub(:get).and_return DbTestUser.json_create({JSON.create_id => 'DbTestUser'})
user.should_receive(:database=).with(db)
db.load '1'
end
it "should load namespaced models" do
- db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => Parent::Child.json_create({JSON.create_id => 'Parent::Child'})))
+ couchrest_db.stub(:get).and_return Parent::Child.json_create({JSON.create_id => 'Parent::Child'})
db.load('1').class.should == Parent::Child
end
+
+ context "when several ids given" do
+
+ let(:doc1) { DbTestUser.new }
+ let(:doc2) { DbTestUser.new }
+ let(:response) do
+ {"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
+ end
+
+ before(:each) do
+ couchrest_db.stub(:bulk_load).and_return response
+ end
+
+ it "requests the couchrest bulk method" do
+ couchrest_db.should_receive(:bulk_load).with(['1', '2', '3'])
+ db.load ['1', '2', '3']
+ end
+
+ it "returns only found documents" do
+ db.load(['1', '2', '3']).should have(2).items
+ end
+
+ it "writes itself to each of the documents" do
+ db.load(['1', '2', '3']).each do |doc|
+ doc.database.should eql(db)
+ end
+ end
+ end
end
describe CouchPotato::Database, 'load!' do
+
+ let(:db) { CouchPotato::Database.new(stub('couchrest db', :info => nil)) }
+
it "should raise an error if no document found" do
- couchrest_db = stub('couchrest db', :info => nil)
- couchrest_db.stub(:get).and_raise(RestClient::ResourceNotFound)
- db = CouchPotato::Database.new(couchrest_db)
+ db.couchrest_database.stub(:get).and_raise(RestClient::ResourceNotFound)
lambda {
db.load! '1'
}.should raise_error(CouchPotato::NotFound)
end
-end
-
-describe CouchPotato::Database, 'bulk_load' do
-
- let(:doc1) { DbTestUser.new }
- let(:doc2) { DbTestUser.new }
- let(:response) do
- {"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
- end
- let(:couchrest_db) { stub('couchrest db', :info => nil, :bulk_load => response).as_null_object }
- let(:db) { CouchPotato::Database.new(couchrest_db) }
-
- it "raises an exception when no array is given" do
- lambda {
- db.bulk_load "1"
- }.should raise_error("Input must be an array.")
- end
-
- it "requests the couchrest bulk method" do
- couchrest_db.should_receive(:bulk_load).with(['1', '2', '3'])
- db.bulk_load ['1', '2', '3']
- end
-
- it "returns only found documents" do
- db.bulk_load(['1', '2', '3']).should have(2).items
- end
- it "writes itself to each of the documents" do
- doc1.should_receive(:database=).with(db)
- doc2.should_receive(:database=).with(db)
- db.bulk_load ['1', '2', '3']
- end
-end
-
-describe CouchPotato::Database, 'bulk_load!' do
-
- it "raises an exception when not all documents could be found" do
- couchrest_db = stub('couchrest db', :info => nil)
- db = CouchPotato::Database.new(couchrest_db)
-
- docs = [
- DbTestUser.new(:id => '1'),
- DbTestUser.new(:id => '2')
- ]
-
- db.stub(:bulk_load).and_return(docs)
- lambda {
- db.bulk_load! ['1', '2', '3']
- }.should raise_error(CouchPotato::NotFound, ['3'])
+ context "when several ids given" do
+ it "raises an exception when not all documents could be found" do
+ docs = [
+ DbTestUser.new(:id => '1'),
+ DbTestUser.new(:id => '2')
+ ]
+
+ db.stub(:load).and_return(docs)
+ lambda {
+ db.load! ['1', '2', '3']
+ }.should raise_error(CouchPotato::NotFound, ['3'])
+ end
end
end

0 comments on commit 8d9e2f6

Please sign in to comment.