change count to limit

This commit is contained in:
Chris Anderson 2009-01-04 21:00:36 -08:00
parent ba8f84127c
commit 7aaffe5d63
6 changed files with 23 additions and 23 deletions

View file

@ -61,7 +61,7 @@ module CouchRest
# * The most recent 20 articles. Remember that the <tt>view_by :date</tt>
# has the default option <tt>:descending => true</tt>.
#
# Article.by_date :count => 20
# Article.by_date :limit => 20
#
# * The raw CouchDB view reduce result for the custom <tt>:tags</tt> view.
# In this case we'll get a count of the number of articles tagged "ruby".
@ -131,7 +131,7 @@ module CouchRest
# opts<Hash>::
# View options, see <tt>CouchRest::Database#view</tt> options for more info.
def first opts = {}
first_instance = self.all(opts.merge!(:count => 1))
first_instance = self.all(opts.merge!(:limit => 1))
first_instance.empty? ? nil : first_instance.first
end

View file

@ -5,13 +5,13 @@ module CouchRest
@db = db
end
def all_docs(count=100, &block)
def all_docs(limit=100, &block)
startkey = nil
oldend = nil
while docrows = request_all_docs(count+1, startkey)
while docrows = request_all_docs(limit+1, startkey)
startkey = docrows.last['key']
docrows.pop if docrows.length > count
docrows.pop if docrows.length > limit
if oldend == startkey
break
end
@ -20,13 +20,13 @@ module CouchRest
end
end
def key_reduce(view, count=2000, firstkey = nil, lastkey = nil, &block)
def key_reduce(view, limit=2000, firstkey = nil, lastkey = nil, &block)
# start with no keys
startkey = firstkey
# lastprocessedkey = nil
keepgoing = true
while keepgoing && viewrows = request_view(view, count, startkey)
while keepgoing && viewrows = request_view(view, limit, startkey)
startkey = viewrows.first['key']
endkey = viewrows.last['key']
@ -37,7 +37,7 @@ module CouchRest
# we need to do an offset thing to find the next startkey
# otherwise we just get stuck
lastdocid = viewrows.last['id']
fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :count => 2)['rows']
fornextloop = @db.view(view, :startkey => startkey, :startkey_docid => lastdocid, :limit => 2)['rows']
newendkey = fornextloop.last['key']
if (newendkey == endkey)
@ -79,18 +79,18 @@ module CouchRest
private
def request_all_docs count, startkey = nil
def request_all_docs limit, startkey = nil
opts = {}
opts[:count] = count if count
opts[:limit] = limit if limit
opts[:startkey] = startkey if startkey
results = @db.documents(opts)
rows = results['rows']
rows unless rows.length == 0
end
def request_view view, count = nil, startkey = nil, endkey = nil
def request_view view, limit = nil, startkey = nil, endkey = nil
opts = {}
opts[:count] = count if count
opts[:limit] = limit if limit
opts[:startkey] = startkey if startkey
opts[:endkey] = endkey if endkey

View file

@ -29,8 +29,8 @@ describe CouchRest::Database do
rs = @db.temp_view(@temp_view, :key => "wild")
rs['rows'].length.should == 1
end
it "should work with a count" do
rs = @db.temp_view(@temp_view, :count => 1)
it "should work with a limit" do
rs = @db.temp_view(@temp_view, :limit => 1)
rs['rows'].length.should == 1
end
it "should work with multi-keys" do
@ -42,9 +42,9 @@ describe CouchRest::Database do
describe "map/reduce query with _temp_view in Javascript" do
before(:each) do
@db.bulk_save([
{"beverage" => "beer", :count => 4},
{"beverage" => "beer", :count => 2},
{"beverage" => "tea", :count => 3}
{"beverage" => "beer", :limit => 4},
{"beverage" => "beer", :limit => 2},
{"beverage" => "tea", :limit => 3}
])
end
it "should return the result of the temporary function" do
@ -109,8 +109,8 @@ describe CouchRest::Database do
rs = @db.view('first/test', :key => "wild")
rs['rows'].length.should == 1
end
it "should work with a count" do
rs = @db.view('first/test', :count => 1)
it "should work with a limit" do
rs = @db.view('first/test', :limit => 1)
rs['rows'].length.should == 1
end
it "should work with multi-keys" do

View file

@ -611,7 +611,7 @@ describe CouchRest::Model do
articles[1].title.should == 'not junk'
end
it "should be queryable with couchrest options" do
articles = Article.by_user_id_and_date :count => 1, :startkey => 'quentin'
articles = Article.by_user_id_and_date :limit => 1, :startkey => 'quentin'
articles.length.should == 1
articles[0].title.should == "even more interesting"
end

View file

@ -32,7 +32,7 @@ describe CouchRest::FileManager, "generating an app" do
CouchRest::FileManager.generate_app(@appdir)
end
it "should create an attachments directory" do
Dir["#{@appdir}/*"].select{|x|x =~ /attachments/}.length.should == 1
Dir["#{@appdir}/*"].select{|x|x =~ /_attachments/}.length.should == 1
end
it "should create a views directory" do
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1

View file

@ -28,7 +28,7 @@ describe CouchRest::Pager do
end
@db.bulk_save(@docs)
end
it "should yield total_docs / count times" do
it "should yield total_docs / limit times" do
n = 0
@pager.all_docs(10) do |doc|
n += 1
@ -76,7 +76,7 @@ describe CouchRest::Pager do
end
it "should have a view" do
@db.view('magic/number', :count => 10)['rows'][0]['key'].should == 0
@db.view('magic/number', :limit => 10)['rows'][0]['key'].should == 0
end
it "should yield once per key" do