lib/dry/container/mixin.rb in dry-container-0.2.1 vs lib/dry/container/mixin.rb in dry-container-0.2.2

- old
+ new

@@ -29,28 +29,30 @@ attr_reader :_container base.class_eval do extend ::Dry::Configurable - setting :registry, Registry.new - setting :resolver, Resolver.new + setting :registry, ::Dry::Container::Registry.new + setting :resolver, ::Dry::Container::Resolver.new + setting :namespace_separator, '.' - @_container = ThreadSafe::Cache.new + @_container = ::ThreadSafe::Cache.new end end # @private def self.included(base) base.class_eval do extend ::Dry::Configurable - setting :registry, Registry.new - setting :resolver, Resolver.new + setting :registry, ::Dry::Container::Registry.new + setting :resolver, ::Dry::Container::Resolver.new + setting :namespace_separator, '.' attr_reader :_container def initialize(*args, &block) - @_container = ThreadSafe::Cache.new + @_container = ::ThreadSafe::Cache.new super(*args, &block) end def config self.class.config @@ -67,11 +69,11 @@ # Options to pass to the registry when registering the item # @yield # If a block is given, contents will be ignored and the block # will be registered instead # - # @return [Dry::Container] self + # @return [Dry::Container::Mixin] self # # @api public def register(key, contents = nil, options = {}, &block) if block_given? item = block @@ -94,8 +96,39 @@ # @api public def resolve(key) config.resolver.call(_container, key) end alias_method :[], :resolve + # Evaluate block and register items in namespace + # + # @param [Mixed] namespace + # The namespace to register items in + # + # @return [Dry::Container::Mixin] self + # + # @api public + def namespace(namespace, &block) + ::Dry::Container::NamespaceDSL.new( + self, + namespace, + config.namespace_separator, + &block + ) + + self + end + # Import a namespace + # + # @param [Dry::Container::Namespace] namespace + # The namespace to import + # + # @return [Dry::Container::Mixin] self + # + # @api public + def import(namespace) + namespace(namespace.name, &namespace.block) + + self + end end end end