RUBY-113 allow has_next for cursors

This commit is contained in:
Kyle Banker 2010-03-19 14:31:31 -04:00
parent a80120f803
commit fd1b58401e
2 changed files with 23 additions and 9 deletions

View File

@ -78,6 +78,13 @@ module Mongo
doc
end
# Determine whether this cursor has any remaining results.
#
# @return [Boolean]
def has_next?
num_remaining > 0
end
# Get the size of the result set for this query.
#
# @return [Integer] the number of objects in the result set for this query. Does
@ -169,7 +176,7 @@ module Mongo
# end
def each
num_returned = 0
while more? && (@limit <= 0 || num_returned < @limit)
while has_next? && (@limit <= 0 || num_returned < @limit)
yield next_document
num_returned += 1
end
@ -188,7 +195,7 @@ module Mongo
raise InvalidOperation, "can't call Cursor#to_a on a used cursor" if @query_run
rows = []
num_returned = 0
while more? && (@limit <= 0 || num_returned < @limit)
while has_next? && (@limit <= 0 || num_returned < @limit)
rows << next_document
num_returned += 1
end
@ -305,13 +312,6 @@ module Mongo
@cache.length
end
# Internal method, not for general use. Return +true+ if there are
# more records to retrieve. This method does not check @limit;
# Cursor#each is responsible for doing that.
def more?
num_remaining > 0
end
def refill_via_get_more
return if send_initial_query || @cursor_id.zero?
message = ByteBuffer.new([0, 0, 0, 0])

View File

@ -376,4 +376,18 @@ class CursorTest < Test::Unit::TestCase
assert_equal(1, @@coll.find({}, :fields => ["a"]).count())
end
end
def test_has_next
@@coll.remove
200.times do |n|
@@coll.save("x" => n)
end
cursor = @@coll.find
while cursor.has_next?
assert cursor.next_document
end
assert_equal false, cursor.has_next?
end
end