Created
May 24, 2012 19:38
-
-
Save dblock/2783755 to your computer and use it in GitHub Desktop.
Mongoid::Criteria each_by iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mongoid | |
class Criteria | |
def each_by(by, &block) | |
idx = 0 | |
total = 0 | |
set_limit = options[:limit] | |
while ((results = ordered_clone.limit(by).skip(idx)) && results.any?) | |
results.each do |result| | |
return self if set_limit and set_limit >= total | |
total += 1 | |
yield result | |
end | |
idx += by | |
end | |
self | |
end | |
private | |
def ordered_clone | |
options[:sort] ? clone : clone.asc(:_id) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment