Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Enumerator return when #each is called without a block #120

Merged
merged 1 commit into from Oct 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/mongo/cursor.rb
Expand Up @@ -276,7 +276,7 @@ def batch_size(size=nil)
end

# Iterate over each document in this cursor, yielding it to the given
# block.
# block, if provided. An Enumerator is returned if no block is given.
#
# Iterating over an entire cursor will close it.
#
Expand All @@ -287,11 +287,18 @@ def batch_size(size=nil)
# puts doc['user']
# end
def each
while doc = self.next
yield doc
if block_given?
while doc = self.next
yield doc
end
else
Enumerator.new do |yielder|
while doc = self.next
yielder.yield doc
end
end
end
end

# Receive all the documents from this cursor as an array of hashes.
#
# Notes:
Expand Down
4 changes: 4 additions & 0 deletions test/cursor_test.rb
Expand Up @@ -93,6 +93,10 @@ def test_explain
assert_kind_of Numeric, explaination['nscanned']
end

def test_each_with_no_block
assert_kind_of Enumerator, @@coll.find().each
end

def test_count
@@coll.remove

Expand Down