class ActiveOperation::Pipeline

Public Class Methods

compose(*operations, &block) click to toggle source
# File lib/active_operation/pipeline.rb, line 43
def compose(*operations, &block)
  raise ArgumentError, "Expects either an array of operations or a block with configuration instructions" unless !!block ^ !operations.empty?

  if block
    Class.new(self, &block)
  else
    Class.new(self) do
      operations.each do |operation|
        use operation
      end
    end
  end
end
operations() click to toggle source
# File lib/active_operation/pipeline.rb, line 25
def operations
  []
end
use(operation, options = {}) click to toggle source
# File lib/active_operation/pipeline.rb, line 29
def use(operation, options = {})
  operation = ActiveOperation::Base.from_proc(operation) if operation.kind_of?(Proc)

  if operations.empty?
    inputs = operation.inputs

    inputs.each do |input|
      input input.name, type: input.type
    end
  end

  (@operations ||= []) << OperationFactory.new(operation, options)
end

Protected Class Methods

inherited(subclass) click to toggle source
Calls superclass method ActiveOperation::Base::inherited
# File lib/active_operation/pipeline.rb, line 59
def inherited(subclass)
  super

  subclass.define_singleton_method(:operations) do
    superclass.operations + Array(@operations)
  end
end

Protected Instance Methods

execute() click to toggle source
# File lib/active_operation/pipeline.rb, line 70
def execute
  values = ->(input) { self[input.name] }

  positional_arguments = self.class.inputs.select(&:positional?).map(&values)
  keyword_arguments = self.class.inputs.select(&:keyword?).each_with_object({}) do |input, kwargs|
    kwargs[input.name] = values.call(input)
  end
  arguments = positional_arguments.push(keyword_arguments)

  self.class.operations.inject(arguments) do |data, operation|
    operation = if data.respond_to?(:to_ary)
                  operation.new(self, *data)
                else
                  operation.new(self, data)
                end

    operation.perform

    output = operation.output

    halt output if operation.halted?

    output
  end
end