Class: Substation::Environment

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/substation/environment.rb,
lib/substation/environment/dsl.rb

Overview

The environment holding all registered Chain processors

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_env, actions, chain_dsl) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance

Parameters:

  • chain_dsl (Chain::DSL)

    the chain dsl tailored for the environment



73
74
75
76
77
78
# 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

Instance Attribute Details

#actionsDispatcher::Registry (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The mutable action registry

Returns:

  • (Dispatcher::Registry)


55
56
57
# File 'lib/substation/environment.rb', line 55

def actions
  @actions
end

#app_envObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The application environment

Returns:

  • (Object)


48
49
50
# File 'lib/substation/environment.rb', line 48

def app_env
  @app_env
end

Class Method Details

.build(app_env, actions = Dispatcher.new_registry, &block) ⇒ Environment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a new Substation::Environment instance

Parameters:

  • app_env (Object)

    the application environment

  • actions (Dispatcher::Registry) (defaults to: Dispatcher.new_registry)

    a mutable action registry

  • block (Proc)

    a block to be instance_eval'ed with DSL

Returns:



25
26
27
# 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

Instance Method Details

#[](name) ⇒ Chain

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the chain identified by name or raise an error

Parameters:

  • the (name)

    name of the chain to retrieve

Returns:

Raises:



147
148
149
# File 'lib/substation/environment.rb', line 147

def [](name)
  actions.fetch(name)
end

#chain(name = nil, other = Chain::EMPTY, exception_chain = Chain::EMPTY, &block) ⇒ Chain

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a new Chain instance

Parameters:

  • name (#to_sym) (defaults to: nil)

    the new chain's name

  • other (Chain) (defaults to: Chain::EMPTY)

    the optional chain to build on top of

  • exception_chain (Chain) (defaults to: Chain::EMPTY)

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

  • block (Proc)

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

Returns:



112
113
114
# 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

#dispatcherDispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a new Dispatcher instance

Parameters:

  • env (Object)

    the application environment

Returns:

See Also:

  • Dispatcher.new


161
162
163
# 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) ⇒ Environment

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • actions (Dispatcher::Registry) (defaults to: Dispatcher.new_registry)

    the mutable action registry

  • block (Proc)

    a block to instance_eval inside a DSL instance

Returns:



91
92
93
# 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) ⇒ Chain

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a new chain under the given name

Parameters:

  • name (#to_sym)

    the new chain's name

  • other (Chain) (defaults to: Chain::EMPTY)

    the chain to build on top of

  • exception_chain (Chain) (defaults to: Chain::EMPTY)

    the chain to invoke in case of uncaught exceptions in handlers

Returns:

  • (Chain)

    the registered chain



131
132
133
134
# 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