Sha256: 0892692901150779126f41e92beb09dfb2f7b3001999c75b6c27bb23d63f5a43

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# @private
class Trailblazer::V2_1::Context::ContainerChain # used to be called Resolver.
  # 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?`, `#[]`.
  #
  # @note ContainerChain is an immutable data structure, it does not support writing.
  # @param containers Array of <Container> objects (splatted)
  def initialize(containers, to_hash: nil)
    @containers = containers
    @to_hash    = to_hash
  end

  # @param name Symbol or String to lookup a value stored in one of the containers.
  def [](name)
    self.class.find(@containers, name)
  end

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

  def self.find(containers, name)
    containers.find { |container| container.key?(name) && (return container[name]) }
  end

  def keys
    @containers.collect(&:keys).flatten
end

  # @private
  def to_hash
    return @to_hash.(@containers) if @to_hash # FIXME: introduce pattern matching so we can have different "transformers" for each container type.
    @containers.each_with_object({}) { |container, hash| hash.merge!(container.to_hash) }
  end
end

# alternative implementation:
# containers.reverse.each do |container| @mutable_options.merge!(container) end
#
# benchmark, merging in #initialize vs. this resolver.
#                merge     39.678k (± 9.1%) i/s -    198.700k in   5.056653s
#             resolver     68.928k (± 6.4%) i/s -    342.836k in   5.001610s

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-future-2.1.0.rc1 lib/trailblazer/v2_1/container_chain.rb