Skip to content

Instantly share code, notes, and snippets.

@dblock
Created May 24, 2012 19:38
Show Gist options
  • Save dblock/2783755 to your computer and use it in GitHub Desktop.
Save dblock/2783755 to your computer and use it in GitHub Desktop.
Mongoid::Criteria each_by iterator
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