Class: Fractor::Workflow::Helpers::MapWorker

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

Overview

Worker for mapping over collections Implement the map_item method

Example:

class ProcessItems < Fractor::Workflow::Helpers::MapWorker
def map_item(item)
  item.upcase
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(mapped_collection, _original_input) ⇒ Object

Override to specify how to build output from mapped collection Default: returns the array



65
66
67
# File 'lib/fractor/workflow/helpers.rb', line 65

def build_output(mapped_collection, _original_input)
  mapped_collection
end

#extract_collection(input) ⇒ Object

Override to specify how to extract collection from input Default: assumes input responds to :to_a



59
60
61
# File 'lib/fractor/workflow/helpers.rb', line 59

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

#map_item(item) ⇒ Object

Override in subclasses to define how to map each item

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/fractor/workflow/helpers.rb', line 53

def map_item(item)
  raise NotImplementedError, "Subclasses must implement #map_item"
end

#process(work) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/fractor/workflow/helpers.rb', line 42

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

  mapped = collection.map { |item| map_item(item) }
  output = build_output(mapped, input)

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