class Substation::Environment

The environment holding all registered {Chain} processors

Attributes

actions[R]

The mutable action registry

@return [Dispatcher::Registry]

@api private

app_env[R]

The application environment

@return [Object]

@api private

registry[R]

The registry used by this {Environment}

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

@api private

Public Class Methods

build(app_env, actions = Dispatcher.new_registry, &block) click to toggle source

Build a new {Environment} instance

@param [Object] app_env

the application environment

@param [Dispatcher::Registry] actions

a mutable action registry

@param [Proc] block

a block to be instance_eval'ed with {DSL}

@return [Environment]

@api private

# File lib/substation/environment.rb, line 25
def self.build(app_env, actions = Dispatcher.new_registry, &block)
  new(app_env, actions, chain_dsl(&block))
end
new(app_env, actions, chain_dsl) click to toggle source

Initialize a new instance

@param [Chain::DSL] chain_dsl

the chain dsl tailored for the environment

@return [undefined]

@api private

# File lib/substation/environment.rb, line 73
def initialize(app_env, actions, chain_dsl)
  @app_env   = app_env
  @actions   = actions
  @chain_dsl = chain_dsl
  @registry  = chain_dsl.registry
end

Private Class Methods

chain_dsl(&block) click to toggle source

Build a new {Chain::DSL} instance

@param [Proc] block

a block to be instance_eval'ed in {DSL}

@return {Chain::DSL}

@api private

# File lib/substation/environment.rb, line 37
def self.chain_dsl(&block)
  Chain::DSL.build(DSL.registry(&block))
end

Public Instance Methods

[](name) click to toggle source

Return the chain identified by name or raise an error

@param [name]

the name of the chain to retrieve

@return [Chain]

@raise [UnknownActionError]

if no chain is registered under +name+

@api private

# File lib/substation/environment.rb, line 147
def [](name)
  actions.fetch(name)
end
chain(name = nil, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block) click to toggle source

Build a new {Chain} instance

@param [#to_sym] name

the new chain's name

@param [Chain] other

the optional chain to build on top of

@param [Chain] exception_chain

the chain to invoke in case of an uncaught exceptions in handlers

@param [Proc] block

a block to be instance_eval'ed in {Chain::DSL}

@return [Chain]

@api private

# File lib/substation/environment.rb, line 112
def chain(name = nil, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block)
  @chain_dsl.build(name, other, exception_chain, &block)
end
dispatcher() click to toggle source

Build a new {Dispatcher} instance

@see Dispatcher.new

@param [Object] env

the application environment

@return [Dispatcher]

@api private

# File lib/substation/environment.rb, line 161
def dispatcher
  Dispatcher.new(actions, app_env)
end
inherit(app_env = app_env, actions = Dispatcher.new_registry, &block) click to toggle source

Inherit a new instance from self, merging the {Chain::DSL}

@param [Dispatcher::Registry] actions

the mutable action registry

@param [Proc] block

a block to instance_eval inside a {DSL} instance

@return [Environment]

@api private

# File lib/substation/environment.rb, line 91
def inherit(app_env = app_env, actions = Dispatcher.new_registry, &block)
  self.class.new(app_env, actions, merged_chain_dsl(&block))
end
register(name, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block) click to toggle source

Register a new chain under the given name

@param [#to_sym] name

the new chain's name

@param [Chain] other

the chain to build on top of

@param [Chain] exception_chain

the chain to invoke in case of uncaught exceptions in handlers

@return [Chain]

the registered chain

@api private

# File lib/substation/environment.rb, line 131
def register(name, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block)
  actions[name] = chain(name, other, exception_chain, &block)
  self
end

Private Instance Methods

merged_chain_dsl(&block) click to toggle source

Return a new {Chain::DSL} by merging in other.registry

@param [Environment] other

the other environment providing the registry to merge

@return [Chain::DSL]

@api private

# File lib/substation/environment.rb, line 175
def merged_chain_dsl(&block)
  Chain::DSL.build(registry.merge(DSL.registry(&block)))
end