module Dry::Core::Container::Mixin

def self.extended(base)

Other tags:
    Private: -
def self.extended(base)
  hooks_mod = ::Module.new do
    def inherited(subclass)
      subclass.instance_variable_set(:@_container, @_container.dup)
      super
    end
  end
  base.class_eval do
    extend Configuration
    extend hooks_mod
    @_container = ::Concurrent::Hash.new
  end
end

def self.included(base)

Other tags:
    Private: -
def self.included(base)
  base.class_eval do
    extend Configuration
    prepend Initializer
    def config
      self.class.config
    end
  end
end

def [](key)

Other tags:
    See: Dry::Core::Container::Mixin#resolve -

Other tags:
    Api: - public

Returns:
  • (Mixed) -

Parameters:
  • key (Mixed) --
def [](key)
  resolve(key)
end

def _container

Other tags:
    Private: - no, really
def _container
  @_container
end

def clone

Other tags:
    Api: - public
def clone
  copy = super
  unless copy.frozen?
    copy.instance_variable_set(:@_container, _container.dup)
  end
  copy
end

def config

def config
  self.class.config
end

def decorate(key, with: nil, &block)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self
def decorate(key, with: nil, &block)
  key = key.to_s
  original = _container.delete(key) do
    raise KeyError, "Nothing registered with the key #{key.inspect}"
  end
  if with.is_a?(Class)
    decorator = with.method(:new)
  elsif block.nil? && !with.respond_to?(:call)
    raise Error, "Decorator needs to be a Class, block, or respond to the `call` method"
  else
    decorator = with || block
  end
  _container[key] = original.map(decorator)
  self
end

def dup

Other tags:
    Api: - public
def dup
  copy = super
  copy.instance_variable_set(:@_container, _container.dup)
  copy
end

def each(&)

Other tags:
    Note: - In discussions with other developers, it was felt that being able to iterate

Other tags:
    Api: - public

Returns:
  • (Enumerator) -
def each(&)
  config.resolver.each(_container, &)
end

def each_key(&)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self
def each_key(&)
  config.resolver.each_key(_container, &)
  self
end

def enable_stubs!

Enable stubbing functionality into the current container
def enable_stubs!
  extend ::Dry::Core::Container::Stub
end

def freeze

Other tags:
    Api: - public
def freeze
  super
  _container.freeze
  self
end

def import(namespace)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self

Parameters:
  • namespace (Dry::Core::Container::Namespace) --
def import(namespace)
  namespace(namespace.name, &namespace.block)
  self
end

def inherited(subclass)

def inherited(subclass)
  subclass.instance_variable_set(:@_container, @_container.dup)
  super
end

def key?(key)

Other tags:
    Api: - public

Returns:
  • (Bool) -

Parameters:
  • key (Mixed) --
def key?(key)
  config.resolver.key?(_container, key)
end

def keys

Other tags:
    Api: - public

Returns:
  • (Array) -
def keys
  config.resolver.keys(_container)
end

def merge(other, namespace: nil, &block)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self

Parameters:
  • namespace (Symbol, nil) --
  • other (Dry::Core::Container) --
def merge(other, namespace: nil, &block)
  if namespace
    _container.merge!(
      other._container.each_with_object(::Concurrent::Hash.new) { |(key, item), hsh|
        hsh[PREFIX_NAMESPACE.call(namespace, key, config)] = item
      },
      &block
    )
  else
    _container.merge!(other._container, &block)
  end
  self
end

def namespace(namespace, &)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self

Parameters:
  • namespace (Mixed) --
def namespace(namespace, &)
  ::Dry::Core::Container::NamespaceDSL.new(
    self,
    namespace,
    config.namespace_separator,
    &
  )
  self
end

def register(key, contents = nil, options = EMPTY_HASH, &block)

Other tags:
    Api: - public

Returns:
  • (Dry::Core::Container::Mixin) - self

Other tags:
    Yield: -

Parameters:
  • options (Hash) --
  • contents (Mixed) --
  • key (Mixed) --
def register(key, contents = nil, options = EMPTY_HASH, &block)
  if block_given?
    item = block
    options = contents if contents.is_a?(::Hash)
  else
    item = contents
  end
  config.registry.call(_container, key, item, options)
  self
rescue ::FrozenError
  raise ::FrozenError,
        "can't modify frozen #{self.class} (when attempting to register '#{key}')"
end

def resolve(key, &)

Other tags:
    Api: - public

Returns:
  • (Mixed) -

Other tags:
    Yieldparam: key - Missing key

Other tags:
    Yield: -

Parameters:
  • key (Mixed) --
def resolve(key, &)
  config.resolver.call(_container, key, &)
end