remove redundant num_to_return from Cursor class. credits. clean up last commit

This commit is contained in:
Mike Dirolf 2009-09-16 17:52:41 -04:00
parent c397758d1d
commit 3321a90739
3 changed files with 31 additions and 31 deletions

View File

@ -329,6 +329,9 @@ Cyril Mougel, cyril.mougel@gmail.com
Jack Chen, chendo on github
* Test case + fix for deserializing pre-epoch Time instances
Kyle Banker, banker on github
* #limit and #skip methods for Cursor instances
= License
Copyright 2008-2009 10gen Inc.

View File

@ -32,7 +32,6 @@ module Mongo
# Should not be called directly by application developers.
def initialize(db, collection, query, admin=false)
@db, @collection, @query, @admin = db, collection, query, admin
@num_to_return = @query.number_to_return || 0
@cache = []
@closed = false
@query_run = false
@ -74,25 +73,23 @@ module Mongo
raise OperationFailure, "Count failed: #{response['errmsg']}"
end
# Sets a limit on the number of results returned by the query.
# Returns a cursor object.
# Limits the number of results to be returned by this cursor.
#
# Note: this method overrides any limit specified in the #find method.
def limit(number_to_return)
check_modifiable
raise ArgumentError, "limit requires an integer" unless number_to_return.is_a? Integer
@number_to_return = number_to_return
@query.number_to_return = number_to_return
return self
end
# Sets an offset on the query results. Returns a cursor object.
# Skips the first +number_to_skip+ results of this cursor.
#
# Note: this method overrides any offset specified in the #find method.
def offset(number_to_skip)
def skip(number_to_skip)
check_modifiable
raise ArgumentError, "limit requires an integer" unless number_to_skip.is_a? Integer
raise ArgumentError, "skip requires an integer" unless number_to_skip.is_a? Integer
@query.number_to_skip = number_to_skip
return self
@ -104,7 +101,7 @@ module Mongo
# Iterating over an entire cursor will close it.
def each
num_returned = 0
while more? && (@num_to_return <= 0 || num_returned < @num_to_return)
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
yield next_object()
num_returned += 1
end
@ -121,7 +118,7 @@ module Mongo
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
rows = []
num_returned = 0
while more? && (@num_to_return <= 0 || num_returned < @num_to_return)
while more? && (@query.number_to_return <= 0 || num_returned < @query.number_to_return)
rows << next_object()
num_returned += 1
end
@ -198,8 +195,8 @@ module Mongo
end
# Internal method, not for general use. Return +true+ if there are
# more records to retrieve. We do not check @num_to_return; #each is
# responsible for doing that.
# more records to retrieve. We do not check @query.number_to_return;
# #each is responsible for doing that.
def more?
num_remaining > 0
end

View File

@ -88,7 +88,7 @@ class CursorTest < Test::Unit::TestCase
end
end
def test_offset
def test_skip
@@coll.clear
10.times do |i|
@ -97,41 +97,41 @@ class CursorTest < Test::Unit::TestCase
assert_equal 10, @@coll.find().count()
all_results = @@coll.find().to_a
offset_results = @@coll.find().offset(2).to_a
skip_results = @@coll.find().skip(2).to_a
assert_equal 10, all_results.length
assert_equal 8, offset_results.length
assert_equal 8, skip_results.length
assert_equal all_results.slice(2...10), offset_results
assert_equal all_results.slice(2...10), skip_results
end
def test_offset_exceptions
def test_skip_exceptions
assert_raise ArgumentError do
cursor = @@coll.find().offset('not-an-integer')
cursor = @@coll.find().skip('not-an-integer')
end
cursor = @@coll.find()
firstResult = cursor.next_object()
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
cursor.offset(1)
cursor.skip(1)
end
cursor = @@coll.find()
cursor.close
assert_raise InvalidOperation, "Cannot modify the query once it has been run or closed." do
cursor.offset(1)
cursor.skip(1)
end
end
def test_limit_offset_chaining
def test_limit_skip_chaining
@@coll.clear
10.times do |i|
@@coll.save("x" => i)
end
all_results = @@coll.find().to_a
limited_offset_results = @@coll.find().limit(5).offset(3).to_a
limited_skip_results = @@coll.find().limit(5).skip(3).to_a
assert_equal all_results.slice(3...8), limited_offset_results
assert_equal all_results.slice(3...8), limited_skip_results
end
def test_close_no_query_sent