class Pipeline::Builder

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/pipeline/builder.rb, line 3
def initialize(&block)
  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval(&block)
    end
  end
end

Public Instance Methods

call(env) click to toggle source

Calls the operations in the stack @param [Object] env - Optional initial input to the pipeline

# File lib/pipeline/builder.rb, line 24
def call(env)
  build_operation_chain(stack.dup).
    call(env.dup)
end
inspect_stack() click to toggle source

 Returns the internal stack array for reading as a frozen object

# File lib/pipeline/builder.rb, line 30
def inspect_stack
  stack.freeze
end
use(operation, *args, &block) click to toggle source

 Adds an operation to the internal stack

@param [Class] operation - The operation class
@param [Array] args - Arguments for the operation
@param [Proc] block - Optional block for the operation
# File lib/pipeline/builder.rb, line 17
def use(operation, *args, &block)
  stack << [operation, args, block]
  self
end

Private Instance Methods

build_operation_chain(stack) click to toggle source

Iterate through the stack and build a single callable object which consists of each operation referencing the next one in the chain

# File lib/pipeline/builder.rb, line 43
def build_operation_chain(stack)
  empty_op = EmptyOperation.new(nil)

  stack.reverse.reduce(empty_op) do |next_op, current_op|
    klass, args, block = current_op

    if Class === klass
      klass.new(next_op, *args, &block)
    elsif Proc === klass
      lambda do |env|
        next_op.call(klass.call(env, *args))
      end
    else
      raise StandardError, "Invalid operation, doesn't respond to `call`: #{klass.inspect}"
    end
  end
end
stack() click to toggle source
# File lib/pipeline/builder.rb, line 36
def stack
  @stack ||= []
end