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.



3
4
5
6
7
# File 'lib/ruby_event_store/batch_enumerator.rb', line 3

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

Instance Method Details

#eachObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby_event_store/batch_enumerator.rb', line 9

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



20
21
22
# File 'lib/ruby_event_store/batch_enumerator.rb', line 20

def first
  each.first
end

#to_aObject



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

def to_a
  each.to_a
end