Permalink
Browse files

When reduce is run then include_docs does not make sense. Fixing that

  • Loading branch information...
sreeix committed Feb 28, 2011
1 parent 7bacf76 commit 228c3881b4b5d061e0e3d8cd4782ab3f7be80991
Showing with 18 additions and 11 deletions.
  1. +6 −1 lib/couchup/couchup.rb
  2. +12 −10 lib/couchup/mapreduce.rb
View
@@ -19,9 +19,14 @@ def views(design = nil)
params = design.nil? ? {:startkey => '_design', :endkey => '_design0'} : {:key => "_design\\#{design}"}
designs = database.documents(params.merge(:include_docs => true))["rows"]
designs.collect do |d|
- d["doc"]["views"].keys.collect{|view| "#{d['key'].gsub('_design/','')}/#{view}"}
+ d["doc"]["views"].keys.collect do |view|
+ map = !d["doc"]["views"][view]["map"].blank?
+ reduce = !d["doc"]["views"][view]["reduce"].blank?
+ {"#{d['key'].gsub('_design/','')}/#{view}" => {:reduce => reduce, :map => map}}
+ end
end.flatten
end
+
def delete_doc(id)
doc = database.get(id)
database.delete_doc(doc)
View
@@ -3,10 +3,19 @@ class MapReduce
def self.map(*params)
#FIXME. should probably get view meta data to do one view call. but this is also doing 2 calls.
begin
- view({}, *params)
+ response = view({:include_docs => true}, *params)
rescue RestClient::BadRequest
- view({:reduce => false}, *params)
+ response = view({:reduce => false, :include_docs => true}, *params)
end
+
+ docs = response["rows"].collect do |r|
+ r["doc"].instance_eval "def save
+ ::Couchup::Couchup.database.save_doc(self)
+ end
+ "
+ r["doc"]
+ end
+
end
def self.reduce(*params)
@@ -16,7 +25,7 @@ def self.reduce(*params)
private
def self.view(options, *params)
name = params.shift
- view_params = {:include_docs => true}.merge(options)
+ view_params = options
if params.size == 1
val = params.first
if val.is_a? Array
@@ -28,13 +37,6 @@ def self.view(options, *params)
end
end
response = Couchup.database.view(name, view_params)
- docs = response["rows"].collect do |r|
- r["doc"].instance_eval "def save
- ::Couchup::Couchup.database.save_doc(self)
- end
- "
- r["doc"]
- end
end
end
end

0 comments on commit 228c388

Please sign in to comment.