Class: Trailblazer::Context::ContainerChain
- Inherits:
-
Object
- Object
- Trailblazer::Context::ContainerChain
- Defined in:
- lib/trailblazer/container_chain.rb
Overview
used to be called Resolver.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#initialize(*containers) ⇒ ContainerChain
constructor
Keeps a list of containers.
- #key?(name) ⇒ Boolean
- #to_hash ⇒ Object
Constructor Details
#initialize(*containers) ⇒ ContainerChain
Note:
ContainerChain is an immutable data structure, it does not support writing.
Keeps a list of containers. When looking up a key/value, containers are traversed in the order they were added until key is found.
Required Container interface: ‘#key?`, `#[]`.
9 10 11 |
# File 'lib/trailblazer/container_chain.rb', line 9 def initialize(*containers) @containers = containers end |
Class Method Details
.find(containers, name) ⇒ Object
23 24 25 |
# File 'lib/trailblazer/container_chain.rb', line 23 def self.find(containers, name) containers.find { |container| container.key?(name) && (return container[name]) } end |
Instance Method Details
#[](name) ⇒ Object
14 15 16 |
# File 'lib/trailblazer/container_chain.rb', line 14 def [](name) self.class.find(@containers, name) end |
#key?(name) ⇒ Boolean
19 20 21 |
# File 'lib/trailblazer/container_chain.rb', line 19 def key?(name) @containers.find { |container| container.key?(name) } end |
#to_hash ⇒ Object
31 32 33 34 |
# File 'lib/trailblazer/container_chain.rb', line 31 def to_hash # FIXME: dry container, etc? @containers.each_with_object({}) { |container, hash| hash.merge!(container.to_hash) } end |