class Linearly::Mixins::StepCollection::Reducer

{Reducer} encapsulates the logic required to process a single Step in a larger collection.

@api private

Attributes

input[R]

Return the original {Statefully::State}

@return [Statefully::State] @api private

step[R]

Return the {Step} to run

@return [Step] @api private

Public Class Methods

reduce(input, step) click to toggle source

Public interface for the {Reducer}

@param input [Statefully::State] @param step [Step]

@return [Statefully::State] @api private

# File lib/linearly/mixins/step_collection.rb, line 34
def self.reduce(input, step)
  new(input: input, step: step).reduce
end

Private Class Methods

new(input:, step:) click to toggle source

Private constructor for the {Reducer}

@param input [Statefully::State] @param step [Step]

@api private

# File lib/linearly/mixins/step_collection.rb, line 68
def initialize(input:, step:)
  @input = input
  @step = step
end

Public Instance Methods

reduce() click to toggle source

Internal Reducer method to create {Step} output

@return [Statefully::State] @api private

# File lib/linearly/mixins/step_collection.rb, line 42
def reduce
  return input if input.failed? || input.finished?
  return input.fail(bad_output_error) unless state_returned?
  output
end

Private Instance Methods

bad_output_error() click to toggle source

Construct an {Errors::StateNotReturned} error from internal state

@return [Errors::StateNotReturned] @api private

# File lib/linearly/mixins/step_collection.rb, line 78
def bad_output_error
  Errors::StateNotReturned.new(
    output: output,
    step: step.class.name,
  )
end
output() click to toggle source

Create output and memoize for reuse

@return [Statefully::State] @api private

# File lib/linearly/mixins/step_collection.rb, line 89
def output
  @output ||= begin
    step.call(input)
  rescue StandardError => error
    input.fail(error)
  end
end
state_returned?() click to toggle source

Check if output is an instance of {Statefully::State}

@return [Boolean] @api private

# File lib/linearly/mixins/step_collection.rb, line 101
def state_returned?
  output.is_a?(Statefully::State)
end