class Substation::Chain::DSL

The DSL class used to define chains in an {Environment}

Constants

BASE_METHODS

All methods defined in this class body

Attributes

config[R]

The config for this instance

@return [Config]

@api private

definition[R]

The definition to be used within a {Chain}

@return [Definition]

@api private

name[R]

The chain's name

@return [#to_sym]

@api public

registry[R]

The registry used to build processors

@return [Hash<Symbol, Processor::Builder>]

@api private

Public Class Methods

build(registry, definition = Definition::EMPTY) click to toggle source

Build a new instance suitable for registry

@param [Hash<Symbol, Processor>] registry

the registry of processor builders to use in a {Chain}

@param [Definition] definition

the collection of processors to use in a {Chain}

@return [DSL]

@api private

# File lib/substation/chain/dsl.rb, line 20
def self.build(registry, definition = Definition::EMPTY)
  new(Config.build(registry), definition)
end
new(config, definition) click to toggle source

Initialize a new instance

Extends the new instance with methods defined in config.dsl_module. Only happens during boot, once for every instantiated {Environment}.

@param [Config] config

a config for this instance

@param [Definition] definition

the definition to use within a {Chain}

@return [undefined]

@api private

# File lib/substation/chain/dsl.rb, line 68
def initialize(config, definition)
  @config     = config
  @definition = definition
  @registry   = @config.registry
  @name       = @definition.name

  extend(@config.dsl_module)
end

Public Instance Methods

build(name, other, exception_chain, &block) click to toggle source

Build a new {Chain} based on other, a failure_chain and a block

@param [#to_sym] name

the name of the chain to build

@param [Enumerable<#call>] other

the processors to prepend

@param [Chain] exception_chain

the chain to invoke in case of an uncaught exception

@param [Proc] block

a block to be instance_eval'ed inside the new {DSL} instance

@return [Chain]

@api private

# File lib/substation/chain/dsl.rb, line 94
def build(name, other, exception_chain, &block)
  Chain.new(__call__(Definition.new(name, other), &block), exception_chain)
end
chain(processors, &block) click to toggle source

Append the given chain

@param [Enumerable<#call>] processors

other processors to be nested within a {Definition}

@return [self]

@api private

# File lib/substation/chain/dsl.rb, line 106
def chain(processors, &block)
  processors.each(&method(:use))
  instance_eval(&block) if block
  self
end
failure_chain(name, failure_chain) click to toggle source

Use chain as the failure chain for the processor identified by name

@param [Symbol] name

the processor's name

@param [Chain] failure_chain

the failure chain to use for the processor identified by +name+

@return [self]

@api private

# File lib/substation/chain/dsl.rb, line 142
def failure_chain(name, failure_chain)
  definition.replace_failure_chain(name, failure_chain)
  self
end
nest(name, chain, executor) click to toggle source

Nest the given chain at the end of the current one

@param [#to_sym] name

the name of the nested chain

@param [Enumerable<#call>] chain

the chain to nest

@param [Processor::Executor] executor

the executor used for nesting

@return [Processor::Nest::Incoming]

@api private

# File lib/substation/chain/dsl.rb, line 126
def nest(name, chain, executor)
  config = Processor::Config.new(executor, EMPTY_ARRAY, EMPTY_ARRAY)
  use(Processor::Nest::Incoming.new(name, chain, config))
end

Private Instance Methods

__call__(other, &block) click to toggle source

Return a new definition

@param [Definition] other

the definition to prepend

@param [Proc] block

a block to be instance_eval'd inside a new {DSL} instance

@return [Definition]

the definition to be used with a {Chain}

@api private

# File lib/substation/chain/dsl.rb, line 175
def __call__(other, &block)
  instance = new(other)
  instance.instance_eval(&block) if block
  instance.definition
end
new(other) click to toggle source

Instantiate a new instance

@param [Definition] other

the definition to prepend

@param [Proc] block

a block to be instance_eval'd inside a new {DSL} instance

@return [DSL]

@api private

# File lib/substation/chain/dsl.rb, line 192
def new(other)
  self.class.new(config, other.prepend(definition))
end
use(processor) click to toggle source

Use the given processor within a chain

@param [#call] processor

a processor to use within a chain

@return [#call]

the added processor

@api private

# File lib/substation/chain/dsl.rb, line 158
def use(processor)
  definition << processor
  processor
end