Class: Hekenga::Iterator
- Inherits:
-
Object
- Object
- Hekenga::Iterator
- Includes:
- Enumerable
- Defined in:
- lib/hekenga/iterator.rb
Constant Summary collapse
- SMALLEST_ID =
BSON::ObjectId.from_string('0'*24)
Instance Attribute Summary collapse
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(scope, size:) ⇒ Iterator
constructor
A new instance of Iterator.
Constructor Details
#initialize(scope, size:) ⇒ Iterator
Returns a new instance of Iterator.
9 10 11 12 |
# File 'lib/hekenga/iterator.rb', line 9 def initialize(scope, size:) @scope = scope @size = size end |
Instance Attribute Details
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
7 8 9 |
# File 'lib/hekenga/iterator.rb', line 7 def scope @scope end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/hekenga/iterator.rb', line 7 def size @size end |
Instance Method Details
#each(&block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hekenga/iterator.rb', line 14 def each(&block) current_id = SMALLEST_ID base_scope = scope.asc(:_id).limit(size) loop do ids = base_scope.and(_id: {'$gt': current_id}).pluck(:_id) break if ids.empty? yield ids current_id = ids.sort.last end end |