class Dry::Container::Registry

Default registry for registering items with the container

@api public

Public Class Methods

new() click to toggle source

@private

# File lib/dry/container/registry.rb, line 12
def initialize
  @_mutex = ::Mutex.new
end

Public Instance Methods

call(container, key, item, options) click to toggle source

Register an item with the container to be resolved later

@param [Concurrent::Hash] container

The container

@param [Mixed] key

The key to register the container item with (used to resolve)

@param [Mixed] item

The item to register with the container

@param [Hash] options @option options [Symbol] :call

Whether the item should be called when resolved

@raise [Dry::Container::Error]

If an item is already registered with the given key

@return [Mixed]

@api public

# File lib/dry/container/registry.rb, line 34
def call(container, key, item, options)
  key = key.to_s.dup.freeze
  @_mutex.synchronize do
    if container.key?(key)
      raise Error, "There is already an item registered with the key #{key.inspect}"
    end

    container[key] = factory.call(item, options)
  end
end
factory() click to toggle source
# File lib/dry/container/registry.rb, line 45
def factory
  @factory ||= ::Dry::Container::Item::Factory.new
end