Permalink
Browse files

first pass at supporting map/reduce views

  • Loading branch information...
1 parent ddb03a7 commit c89dc080bf502b9cc9a6ea967cc4d6f470101d58 Wesley Beary committed Jul 19, 2008
@@ -118,14 +118,20 @@ def read_many(query)
http.request(build_request(query))
end
if doc['rows']
- Collection.new(query) do |collection|
- doc['rows'].each do |doc|
- data = doc["value"]
- collection.load(
- query.fields.map do |property|
- data[property.field.to_s]
- end
- )
+ if doc['rows'].empty?
+ []
+ elsif query.view && query.model.views[query.view.to_sym].has_key?('reduce')
+ doc['rows'].first['value']
+ else
+ Collection.new(query) do |collection|
+ doc['rows'].each do |doc|
+ data = doc["value"]
+ collection.load(
+ query.fields.map do |property|
+ data[property.field.to_s]
+ end
+ )
+ end
end
end
elsif doc['type'] && doc['type'].downcase == query.model.name.downcase
@@ -24,6 +24,8 @@ class User
# creates methods for accessing stored/indexed views in the CouchDB database
view :by_name, { "map" => "function(doc) { if (doc.type == 'user') { emit(doc.name, doc); } }" }
view :by_age, { "map" => "function(doc) { if (doc.type == 'user') { emit(doc.age, doc); } }" }
+ view :count, { "map" => "function(doc) { if (doc.type == 'user') { emit(null, 1); } }",
+ "reduce" => "function(keys, values) { return sum(values); }" }
before :create do
self.created_at = DateTime.now
@@ -221,6 +223,11 @@ class Company
User.by_age(30).first == User.by_age(:key => 30).first
end
+ it "should return a value from a view with reduce defined" do
+ pending("No CouchDB connection.") if @no_connection
+ User.count.should == User.all.length
+ end
+
def create_procedures
DataMapper.auto_migrate!
DataMapper.auto_migrate!

0 comments on commit c89dc08

Please sign in to comment.