Permalink
Browse files

store total_rows on collection

we get total_rows for free on any view queries,
so we might as well keep it around where it can
be of use.
  • Loading branch information...
1 parent aa11af4 commit 2e6255c6ec4f5a3f141a62c3f8a326f53a02a0ec Wesley Beary committed Oct 5, 2008
View
35 adapters/dm-couchdb-adapter/lib/couchdb_adapter.rb
@@ -127,12 +127,11 @@ def read_many(query)
doc = request do |http|
http.request(build_request(query))
end
- if doc['rows']
- if doc['rows'].empty?
- Collection.new(query) { [] }
- elsif query.view && query.model.views[query.view.to_sym].has_key?('reduce')
- doc['rows'].map {|row| OpenStruct.new(row)}
- else
+ if query.view && query.model.views[query.view.to_sym].has_key?('reduce')
+ doc['rows'].map {|row| OpenStruct.new(row)}
+ else
+ collection =
+ if doc['rows'] && !doc['rows'].empty?
Collection.new(query) do |collection|
doc['rows'].each do |doc|
data = doc["value"]
@@ -143,18 +142,20 @@ def read_many(query)
)
end
end
+ elsif doc['couchdb_type'] && doc['couchdb_type'] == query.model.storage_name(repository.name)
+ data = doc
+ Collection.new(query) do |collection|
+ collection.load(
+ query.fields.map do |property|
+ property.typecast(data[property.field.to_s])
+ end
+ )
+ end
+ else
+ Collection.new(query) { [] }
end
- elsif doc['couchdb_type'] && doc['couchdb_type'] == query.model.storage_name(repository.name)
- data = doc
- Collection.new(query) do |collection|
- collection.load(
- query.fields.map do |property|
- property.typecast(data[property.field.to_s])
- end
- )
- end
- else
- []
+ collection.total_rows = doc && doc['total_rows'] || 0
+ collection
end
end
View
6 adapters/dm-couchdb-adapter/lib/couchdb_adapter/view.rb
@@ -5,6 +5,12 @@ class Query
end
module DataMapper
+ class Collection
+ attr_accessor :total_rows
+ end
+end
+
+module DataMapper
class View
attr_reader :model, :name
View
5 adapters/dm-couchdb-adapter/spec/couchdb_adapter_spec.rb
@@ -151,6 +151,11 @@ class Broken
User.all.length.should == 3
end
+ it "should set total_rows on collection" do
+ pending("No CouchDB connection.") if @no_connection
+ User.all.total_rows.should == 3
+ end
+
it "should get records by eql matcher" do
pending("No CouchDB connection.") if @no_connection
new_user(:name => "John", :age => 50).save

0 comments on commit 2e6255c

Please sign in to comment.