Class: RubyEventStore::BatchEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/batch_enumerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(batch_size, total_limit, reader) ⇒ BatchEnumerator

Returns a new instance of BatchEnumerator.


5
6
7
8
9
# File 'lib/ruby_event_store/batch_enumerator.rb', line 5

def initialize(batch_size, total_limit, reader)
  @batch_size  = batch_size
  @total_limit = total_limit
  @reader      = reader
end

Instance Method Details

#eachObject


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_event_store/batch_enumerator.rb', line 11

def each
  return to_enum unless block_given?

  0.step(total_limit - 1, batch_size) do |batch_offset|
    batch_limit = [batch_size, total_limit - batch_offset].min
    result      = reader.call(batch_offset, batch_limit)

    break if result.empty?
    yield result
  end
end

#firstObject


23
24
25
# File 'lib/ruby_event_store/batch_enumerator.rb', line 23

def first
  each.first
end

#to_aObject


27
28
29
# File 'lib/ruby_event_store/batch_enumerator.rb', line 27

def to_a
  each.to_a
end