Class: Miscellany::BatchProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/miscellany/batch_processor.rb

Overview

An array that “processes” after so many items are added.

Example Usage:

batches = BatchProcessor.new(of: 1000) do |batch|
  # Process the batch somehow
end
enumerator_of_some_kind.each { |item| batches << item }
batches.flush

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(of: 1000, ensure_once: false, &blk) ⇒ BatchProcessor

Returns a new instance of BatchProcessor.



13
14
15
16
17
18
19
20
# File 'lib/miscellany/batch_processor.rb', line 13

def initialize(of: 1000, ensure_once: false, &blk)
  @batch_size = of
  @block = blk
  @ensure_once = ensure_once
  @current_batch = []

  @flush_count = 0
end

Instance Attribute Details

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



11
12
13
# File 'lib/miscellany/batch_processor.rb', line 11

def batch_size
  @batch_size
end

#ensure_onceObject (readonly)

Returns the value of attribute ensure_once.



11
12
13
# File 'lib/miscellany/batch_processor.rb', line 11

def ensure_once
  @ensure_once
end

Instance Method Details

#<<(item) ⇒ Object



22
23
24
25
# File 'lib/miscellany/batch_processor.rb', line 22

def <<(item)
  @current_batch << item
  process_batch if @current_batch.count >= batch_size
end

#add_all(items) ⇒ Object



27
28
29
30
31
# File 'lib/miscellany/batch_processor.rb', line 27

def add_all(items)
  items.each do |i|
    self << i
  end
end

#flushObject



33
34
35
# File 'lib/miscellany/batch_processor.rb', line 33

def flush
  process_batch if @current_batch.present? || (@flush_count.zero? && ensure_once)
end