lib/dry/container/mixin.rb in dry-container-0.6.0 vs lib/dry/container/mixin.rb in dry-container-0.7.0

- old
+ new

@@ -1,8 +1,11 @@ + +require 'concurrent/hash' + module Dry class Container - PREFIX_NAMESPACE = ->(namespace, key, config) do + PREFIX_NAMESPACE = lambda do |namespace, key, config| [namespace, key].join(config.namespace_separator) end # Mixin to expose Inversion of Control (IoC) container behaviour # # @example @@ -127,12 +130,11 @@ # Merge in the items of the other container # # @param [Dry::Container] other # The other container to merge in - # @param [Hash] options - # @option options [Symbol] :namespace + # @param [Symbol, nil] namespace # Namespace to prefix other container items with, defaults to nil # # @return [Dry::Container::Mixin] self # # @api public @@ -148,11 +150,11 @@ end self end - # Check whether an items is registered under the given key + # Check whether an item is registered under the given key # # @param [Mixed] key # The key you wish to check for registration with # # @return [Bool] @@ -196,10 +198,29 @@ # toward doing that. def each(&block) config.resolver.each(_container, &block) end + # Decorates an item from the container with specified decorator + # + # @return [Dry::Container::Mixin] self + # + # @api public + def decorate(key, with:) + original = _container.delete(key.to_s) do + raise Error, "Nothing registered with the key #{key.inspect}" + end + + decorator = with + + if decorator.is_a?(Class) + register(key, decorator.new(original.call)) + else + register(key, decorator) + end + end + # Evaluate block and register items in namespace # # @param [Mixed] namespace # The namespace to register items in # @@ -226,9 +247,18 @@ # # @api public def import(namespace) namespace(namespace.name, &namespace.block) + self + end + + # Freeze the container. Nothing can be registered after freezing + # + # @api public + def freeze + super + _container.freeze self end # @private no, really def _container