lib/dry/container/mixin.rb in dry-container-0.3.4 vs lib/dry/container/mixin.rb in dry-container-0.4.0
- old
+ new
@@ -1,7 +1,10 @@
module Dry
class Container
+ PREFIX_NAMESPACE = ->(namespace, key, config) do
+ [namespace, key].compact.join(config.namespace_separator)
+ end
# Mixin to expose Inversion of Control (IoC) container behaviour
#
# @example
#
# class MyClass
@@ -24,11 +27,11 @@
#
# @api public
module Mixin
# @private
def self.extended(base)
- hooks_mod = Module.new do
+ hooks_mod = ::Module.new do
def inherited(subclass)
subclass.instance_variable_set(:@_container, @_container.dup)
super
end
end
@@ -124,12 +127,16 @@
# The other container to merge in
#
# @return [Dry::Container::Mixin] self
#
# @api public
- def merge(other)
- _container.merge!(other._container)
+ def merge(other, namespace: nil)
+ _container.merge!(
+ other._container.each_with_object(::Concurrent::Hash.new) do |a, h|
+ h[PREFIX_NAMESPACE.call(namespace, a.first, config)] = a.last
+ end
+ )
self
end
# Check whether an items is registered under the given key
#
@@ -148,9 +155,21 @@
# @return [Array<String>]
#
# @api public
def keys
config.resolver.keys(_container)
+ end
+
+ # Calls block once for each key in container, passing the key as a parameter.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+ # @return [Dry::Container::Mixin] self
+ #
+ # @api public
+ def each_key(&block)
+ config.resolver.each_key(_container, &block)
+ self
end
# Evaluate block and register items in namespace
#
# @param [Mixed] namespace