Class: Hekenga::Iterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hekenga/iterator.rb

Constant Summary collapse

SMALLEST_ID =
BSON::ObjectId.from_string('0'*24)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/hekenga/iterator.rb', line 7

def scope
  @scope
end

#sizeObject (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