class Terrestrial::FunctionalPipeline

Public Class Methods

from_array(steps = []) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 3
def self.from_array(steps = [])
  new(steps.map { |name, func| Step.new(name, func) })
end
new(steps = []) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 7
def initialize(steps = [])
  @steps = steps
end

Public Instance Methods

append(name, func) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 21
def append(name, func)
  self.class.new(@steps + [Step.new(name, func)])
end
call(args, &block) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 11
def call(args, &block)
  result = execution_result([[:input, args]], &block)

  [result.last.last, result]
end
describe() click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 17
def describe
  @steps.map(&:name)
end
drop_until(step_name) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 33
def drop_until(step_name)
  step = @steps.detect { |step| step.name == step_name }
  first_step_index = @steps.index(step) + 1
  steps = @steps.slice(first_step_index..-1)

  self.class.new(steps)
end
take_until(step_name) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 25
def take_until(step_name)
  step = @steps.detect { |step| step.name == step_name }
  last_step_index = @steps.index(step)
  steps = @steps.slice(0..last_step_index)

  self.class.new(steps)
end

Private Instance Methods

execution_result(initial_state, &block) click to toggle source
# File lib/terrestrial/functional_pipeline.rb, line 43
def execution_result(initial_state, &block)
  @steps.reduce(initial_state) { |state, step|
    new_value = step.call(state.last.last)
    block && block.call(step.name, new_value)
    state + [ [step.name, new_value] ]
  }
end