Commit 9d61d9138d8e353e6e7327ee146bacf54de7192a
- Diff rendering mode:
- inline
- side by side
lib/couch_object/database.rb
(16 / 0)
|   | |||
| 117 | 117 | resp.to_document | |
| 118 | 118 | end | |
| 119 | 119 | ||
| 120 | # Query this database via a _temp_view view | ||
| 121 | def query(fun, language = "javascript") | ||
| 122 | resp = Response.new(post("_temp_view", {"language" => language, "map" => fun}.to_json)) | ||
| 123 | resp.to_document | ||
| 124 | end | ||
| 125 | |||
| 126 | # Query this database with a +map_fun+ and a +reduce_fun+ to get the reduced result | ||
| 127 | def reduce_query(map_fun, reduce_fun, language = "javascript") | ||
| 128 | resp = Response.new(post("_temp_view", { | ||
| 129 | "language" => language, | ||
| 130 | "map" => map_fun, | ||
| 131 | "reduce" => reduce_fun, | ||
| 132 | }.to_json)) | ||
| 133 | resp.to_document | ||
| 134 | end | ||
| 135 | |||
| 120 | 136 | end | |
| 121 | 137 | end |
spec/database_spec.rb
(25 / 0)
|   | |||
| 170 | 170 | res = db.bulk_load([CouchObject::Document.new({"foo" => "bar"}), CouchObject::Document.new({"foo" => "baz"})]) | |
| 171 | 171 | end | |
| 172 | 172 | ||
| 173 | it "should query the temp view with a map function" do | ||
| 174 | db = CouchObject::Database.new(@uri, "foo") | ||
| 175 | resp = Struct.new(:body).new | ||
| 176 | resp.body = { "rows" => [{"_id" => "1"}, {"_id" => "1"}] }.to_json | ||
| 177 | db.should_receive(:post).with("_temp_view", JSON.unparse({ | ||
| 178 | "language"=>"javascript", | ||
| 179 | "map" => "function(doc){ map(null, doc) }" | ||
| 180 | })).and_return(resp) | ||
| 181 | res = db.query("function(doc){ map(null, doc) }") | ||
| 182 | res.should be_instance_of(CouchObject::Document) | ||
| 183 | end | ||
| 184 | |||
| 185 | it "should reduce_query the temp view with a map & reduce function" do | ||
| 186 | db = CouchObject::Database.new(@uri, "foo") | ||
| 187 | resp = Struct.new(:body).new | ||
| 188 | resp.body = { "result" => 42, "ok" => true }.to_json | ||
| 189 | db.should_receive(:post).with("_temp_view", JSON.unparse({ | ||
| 190 | "language"=>"javascript", | ||
| 191 | "map" => "function(doc){ map(null, doc) }", | ||
| 192 | "reduce" => "function (keys, values) { return sum(values) }" | ||
| 193 | })).and_return(resp) | ||
| 194 | res = db.reduce_query("function(doc){ map(null, doc) }", "function (keys, values) { return sum(values) }") | ||
| 195 | res.should be_instance_of(CouchObject::Document) | ||
| 196 | end | ||
| 197 | |||
| 173 | 198 | #it "should url encode paths" | |
| 174 | 199 | end |

