Skip to content
This repository
Browse code

Use load method instead of bulk_load to load several documents

  • Loading branch information...
commit 8d9e2f65d2fff56ac8b3d7d4d10cffbefe46b200 1 parent 563cb58
Matthias Jakel authored February 26, 2012
2  README.md
Source Rendered
@@ -163,7 +163,7 @@ You can of course also retrieve your instance:
163 163
 
164 164
 You can also load a bunch of documents with one request.
165 165
 
166  
-    CouchPotato.database.bulk_load ['user1', 'user2', 'user3'] # => [<#User 0x3075>, <#User 0x3076>, <#User 0x3077>]
  166
+    CouchPotato.database.load ['user1', 'user2', 'user3'] # => [<#User 0x3075>, <#User 0x3076>, <#User 0x3077>]
167 167
 
168 168
 #### Properties
169 169
 
49  lib/couch_potato/database.rb
@@ -99,40 +99,30 @@ def destroy_document(document)
99 99
     end
100 100
     alias_method :destroy, :destroy_document
101 101
 
102  
-    # loads a document by its id
  102
+    # loads a document by its id(s)
103 103
     def load_document(id)
104 104
       raise "Can't load a document without an id (got nil)" if id.nil?
105  
-      begin
106  
-        instance = couchrest_database.get(id)
107  
-        instance.database = self
108  
-        instance
109  
-      rescue(RestClient::ResourceNotFound)
110  
-        nil
  105
+      
  106
+      if id.is_a?(Array)
  107
+        bulk_load id
  108
+      else
  109
+        begin
  110
+          instance = couchrest_database.get(id)
  111
+          instance.database = self
  112
+          instance
  113
+        rescue(RestClient::ResourceNotFound)
  114
+          nil
  115
+        end
111 116
       end
112 117
     end
113 118
     alias_method :load, :load_document
114 119
 
115 120
     def load!(id)
116  
-      load(id) || raise(CouchPotato::NotFound)
117  
-    end
118  
-    
119  
-    def bulk_load(ids)
120  
-      if ids.is_a?(Array)
121  
-        response = couchrest_database.bulk_load ids
122  
-        existing_rows = response['rows'].select{|row| row.key? 'doc'}
123  
-        docs = existing_rows.map{|row| row["doc"]}
124  
-        docs.each{|doc| doc.database = self}
125  
-      else
126  
-        raise "Input must be an array."
127  
-      end
128  
-    end
129  
-
130  
-    def bulk_load!(ids)
131  
-      docs = bulk_load(ids)
132  
-      if ids.length > docs.length
133  
-        missing_docs = ids - docs.map(&:id)
134  
-        raise CouchPotato::NotFound, missing_docs
  121
+      doc = load(id)
  122
+      if id.is_a?(Array)
  123
+        missing_docs = id - doc.map(&:id)
135 124
       end
  125
+      raise(CouchPotato::NotFound, missing_docs) if doc.nil? || missing_docs
136 126
     end
137 127
 
138 128
     def inspect #:nodoc:
@@ -145,6 +135,13 @@ def couchrest_database
145 135
     end
146 136
 
147 137
     private
  138
+    
  139
+    def bulk_load(ids)
  140
+      response = couchrest_database.bulk_load ids
  141
+      existing_rows = response['rows'].select{|row| row.key? 'doc'}
  142
+      docs = existing_rows.map{|row| row["doc"]}
  143
+      docs.each{|doc| doc.database = self}
  144
+    end
148 145
 
149 146
     def create_document(document, validate)
150 147
       document.database = self
104  spec/unit/database_spec.rb
@@ -45,8 +45,11 @@ class Child
45 45
 end
46 46
 
47 47
 describe CouchPotato::Database, 'load' do
  48
+  
  49
+  let(:couchrest_db) { stub('couchrest db', :info => nil) }
  50
+  let(:db) { CouchPotato::Database.new couchrest_db }
  51
+  
48 52
   it "should raise an exception if nil given" do
49  
-    db = CouchPotato::Database.new(stub('couchrest db', :info => nil))
50 53
     lambda {
51 54
       db.load nil
52 55
     }.should raise_error("Can't load a document without an id (got nil)")
@@ -55,75 +58,68 @@ class Child
55 58
   it "should set itself on the model" do
56 59
     user = mock('user').as_null_object
57 60
     DbTestUser.stub!(:new).and_return(user)
58  
-    db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => DbTestUser.json_create({JSON.create_id => 'DbTestUser'})))
  61
+    couchrest_db.stub(:get).and_return DbTestUser.json_create({JSON.create_id => 'DbTestUser'})
59 62
     user.should_receive(:database=).with(db)
60 63
     db.load '1'
61 64
   end
62 65
 
63 66
   it "should load namespaced models" do
64  
-    db = CouchPotato::Database.new(stub('couchrest db', :info => nil, :get => Parent::Child.json_create({JSON.create_id => 'Parent::Child'})))
  67
+    couchrest_db.stub(:get).and_return Parent::Child.json_create({JSON.create_id => 'Parent::Child'})
65 68
     db.load('1').class.should == Parent::Child
66 69
   end
  70
+  
  71
+  context "when several ids given" do
  72
+    
  73
+    let(:doc1) { DbTestUser.new }
  74
+    let(:doc2) { DbTestUser.new }
  75
+    let(:response) do
  76
+      {"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
  77
+    end
  78
+    
  79
+    before(:each) do
  80
+      couchrest_db.stub(:bulk_load).and_return response
  81
+    end
  82
+
  83
+    it "requests the couchrest bulk method" do
  84
+      couchrest_db.should_receive(:bulk_load).with(['1', '2', '3'])
  85
+      db.load ['1', '2', '3']
  86
+    end
  87
+
  88
+    it "returns only found documents" do
  89
+      db.load(['1', '2', '3']).should have(2).items
  90
+    end
  91
+
  92
+    it "writes itself to each of the documents" do
  93
+      db.load(['1', '2', '3']).each do |doc|
  94
+        doc.database.should eql(db)
  95
+      end
  96
+    end
  97
+  end
67 98
 end
68 99
 
69 100
 describe CouchPotato::Database, 'load!' do
  101
+  
  102
+  let(:db) { CouchPotato::Database.new(stub('couchrest db', :info => nil)) }
  103
+  
70 104
   it "should raise an error if no document found" do
71  
-    couchrest_db = stub('couchrest db', :info => nil)
72  
-    couchrest_db.stub(:get).and_raise(RestClient::ResourceNotFound)
73  
-    db = CouchPotato::Database.new(couchrest_db)
  105
+    db.couchrest_database.stub(:get).and_raise(RestClient::ResourceNotFound)
74 106
     lambda {
75 107
       db.load! '1'
76 108
     }.should raise_error(CouchPotato::NotFound)
77 109
   end
78  
-end
79  
-
80  
-describe CouchPotato::Database, 'bulk_load' do
81  
-
82  
-  let(:doc1) { DbTestUser.new }
83  
-  let(:doc2) { DbTestUser.new }
84  
-  let(:response) do
85  
-    {"rows" => [{}, {"doc" => doc1}, {"doc" => doc2}]}
86  
-  end
87  
-  let(:couchrest_db) { stub('couchrest db', :info => nil, :bulk_load => response).as_null_object }
88  
-  let(:db) { CouchPotato::Database.new(couchrest_db) }
89  
-
90  
-  it "raises an exception when no array is given" do
91  
-    lambda {
92  
-      db.bulk_load "1"
93  
-    }.should raise_error("Input must be an array.")
94  
-  end
95  
-
96  
-  it "requests the couchrest bulk method" do
97  
-    couchrest_db.should_receive(:bulk_load).with(['1', '2', '3'])
98  
-    db.bulk_load ['1', '2', '3']
99  
-  end
100  
-
101  
-  it "returns only found documents" do
102  
-    db.bulk_load(['1', '2', '3']).should have(2).items
103  
-  end
104 110
   
105  
-  it "writes itself to each of the documents" do
106  
-    doc1.should_receive(:database=).with(db)
107  
-    doc2.should_receive(:database=).with(db)
108  
-    db.bulk_load ['1', '2', '3']
109  
-  end
110  
-end
111  
-
112  
-describe CouchPotato::Database, 'bulk_load!' do
113  
-
114  
-  it "raises an exception when not all documents could be found" do
115  
-    couchrest_db = stub('couchrest db', :info => nil)
116  
-    db = CouchPotato::Database.new(couchrest_db)
117  
-
118  
-    docs = [
119  
-      DbTestUser.new(:id => '1'),
120  
-      DbTestUser.new(:id => '2')
121  
-    ]
122  
-
123  
-    db.stub(:bulk_load).and_return(docs)
124  
-    lambda {
125  
-      db.bulk_load! ['1', '2', '3']
126  
-    }.should raise_error(CouchPotato::NotFound, ['3'])
  111
+  context "when several ids given" do
  112
+    it "raises an exception when not all documents could be found" do
  113
+      docs = [
  114
+        DbTestUser.new(:id => '1'),
  115
+        DbTestUser.new(:id => '2')
  116
+      ]
  117
+      
  118
+      db.stub(:load).and_return(docs)
  119
+      lambda {
  120
+        db.load! ['1', '2', '3']
  121
+      }.should raise_error(CouchPotato::NotFound, ['3'])
  122
+    end
127 123
   end
128 124
 end
129 125
 

0 notes on commit 8d9e2f6

Please sign in to comment.
Something went wrong with that request. Please try again.