Class: Fractor::Workflow::Helpers::ReduceWorker

Inherits:
Fractor::Worker show all
Defined in:
lib/fractor/workflow/helpers.rb

Overview

Worker for reducing/aggregating collections Implement the reduce_items method

Example:

class SumNumbers < Fractor::Workflow::Helpers::ReduceWorker
def reduce_items(collection)
  collection.sum
end
end

Instance Method Summary collapse

Methods inherited from Fractor::Worker

effective_timeout, #initialize, input_type, output_type, timeout, #timeout

Constructor Details

This class inherits a constructor from Fractor::Worker

Instance Method Details

#build_output(reduced_value, _original_input) ⇒ Object

Override to specify how to build output from reduced value



137
138
139
# File 'lib/fractor/workflow/helpers.rb', line 137

def build_output(reduced_value, _original_input)
  reduced_value
end

#extract_collection(input) ⇒ Object

Override to specify how to extract collection from input



132
133
134
# File 'lib/fractor/workflow/helpers.rb', line 132

def extract_collection(input)
  input.respond_to?(:to_a) ? input.to_a : [input]
end

#process(work) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/fractor/workflow/helpers.rb', line 116

def process(work)
  input = work.input
  collection = extract_collection(input)

  result = reduce_items(collection)
  output = build_output(result, input)

  Fractor::WorkResult.new(result: output, work: work)
end

#reduce_items(collection) ⇒ Object

Override in subclasses to define reduce logic

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/fractor/workflow/helpers.rb', line 127

def reduce_items(collection)
  raise NotImplementedError, "Subclasses must implement #reduce_items"
end