module Wor::Batchifier

Constants

VERSION

Public Instance Methods

execute_in_batches(collection, batch_size: 100, strategy: :add) { |chunk| ... } click to toggle source

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

# File lib/wor/batchifier.rb, line 18
def execute_in_batches(collection, batch_size: 100, strategy: :add)
  collection.lazy.each_slice(batch_size).inject(strategy.merge_base_case) do |memo, chunk|
    response = yield(chunk)
    merge(strategy.merge_method, response, memo)
  end
end

Private Instance Methods

merge(merge_method, response, memo) click to toggle source
# File lib/wor/batchifier.rb, line 27
def merge(merge_method, response, memo)
  merge_method.call(response, memo)
end