Module: Wor::Batchifier

Defined in:
lib/wor/batchifier.rb,
lib/wor/batchifier/add.rb,
lib/wor/batchifier/version.rb,
lib/wor/batchifier/strategy.rb,
lib/wor/batchifier/exceptions.rb,
lib/wor/batchifier/array_merge.rb,
lib/wor/batchifier/no_response.rb,
lib/wor/batchifier/maintain_unique.rb

Defined Under Namespace

Modules: Exceptions Classes: Add, ArrayMerge, MaintainUnique, NoResponse, Strategy

Constant Summary collapse

VERSION =
'0.0.2'.freeze

Instance Method Summary collapse

Instance Method Details

#execute_in_batches(collection, batch_size: 100, strategy: :add) ⇒ Object

This module exports the function execute_in_batches, that needs a collections and

> optionaly a batch_size and a merge strategy. It will slice the collection and

> apply the chozen strategy to all chunks and merge the results. It expects the responses

> to be Hash. It can ignore them if the given strategy is no_response



15
16
17
18
19
20
21
# File 'lib/wor/batchifier.rb', line 15

def execute_in_batches(collection, batch_size: 100, strategy: :add)
  strategy_class = classify_strategy(strategy)
  collection.lazy.each_slice(batch_size).inject(strategy_class.new.base_case) do |rec, chunk|
    response = yield(chunk)
    merge(response, rec, strategy)
  end
end