added block yields to db.view

This commit is contained in:
Chris Anderson 2008-10-13 16:46:48 -07:00
parent b1847cb465
commit 54a0afdf8e
2 changed files with 14 additions and 2 deletions

View file

@ -16,6 +16,7 @@ module CouchRest
@server = server
@host = server.uri
@root = "#{host}/#{name}"
@streamer = Streamer.new(self)
end
# returns the database's uri
@ -48,15 +49,19 @@ module CouchRest
end
# Query a CouchDB view as defined by a <tt>_design</tt> document. Accepts paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi
def view name, params = {}
def view name, params = {}, &block
keys = params.delete(:keys)
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
if keys
CouchRest.post(url, {:keys => keys})
else
if block_given?
@streamer.view(name, params, &block)
else
CouchRest.get url
end
end
end
# GET a document from CouchDB, by id. Returns a Ruby Hash.
def get id

View file

@ -117,6 +117,13 @@ describe CouchRest::Database do
rs = @db.view('first/test', :keys => ["another", "wild"])
rs['rows'].length.should == 2
end
it "should accept a block" do
rows = []
rs = @db.view('first/test', :include_docs => true) do |row|
rows << row
end
rows.length.should == 3
end
end
describe "GET (document by id) when the doc exists" do