diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 3b51de3..a235998 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -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]) diff --git a/test/cursor_test.rb b/test/cursor_test.rb index 9bc763b..3b11ca1 100644 --- a/test/cursor_test.rb +++ b/test/cursor_test.rb @@ -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