Class: Trailblazer::Context::ContainerChain

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/container_chain.rb

Overview

used to be called Resolver.

Class Method Summary collapse

Instance Method Summary collapse

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?`, `#[]`.

Parameters:

  • containers

    Array of <Container> objects (splatted)



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

Parameters:

  • name

    Symbol or String to lookup a value stored in one of the containers.



14
15
16
# File 'lib/trailblazer/container_chain.rb', line 14

def [](name)
  self.class.find(@containers, name)
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/trailblazer/container_chain.rb', line 19

def key?(name)
  @containers.find { |container| container.key?(name) }
end

#to_hashObject



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