Module: Batching

Defined in:
lib/batching.rb

Overview

Purpose to provide a in_batches interface around each

Defined Under Namespace

Classes: Batch

Instance Method Summary collapse

Instance Method Details

#in_batches(batch_size: 1_000) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/batching.rb', line 14

def in_batches(batch_size: 1_000)
  batch = Batch.new

  process_batch_proc = proc do
    yield batch

    batch = Batch.new
  end

  each_with_index do |item, index|
    batch.start_index ||= index
    batch << item

    process_batch_proc.call if batch.size >= batch_size
  end

  process_batch_proc.call unless batch.empty?
end