Class: Dry::Container::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/container/registry.rb

Overview

Default registry for registering items with the container

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



12
13
14
# File 'lib/dry/container/registry.rb', line 12

def initialize
  @_mutex = ::Mutex.new
end

Instance Method Details

#call(container, key, item, options) ⇒ Mixed

Register an item with the container to be resolved later

Parameters:

  • container (Concurrent::Hash)

    The container

  • key (Mixed)

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

  • item (Mixed)

    The item to register with the container

  • options (Hash)

Options Hash (options):

  • :call (Symbol)

    Whether the item should be called when resolved

Returns:

  • (Mixed)

Raises:



34
35
36
37
38
39
40
41
42
43
# 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

#factoryObject



45
46
47
# File 'lib/dry/container/registry.rb', line 45

def factory
  @factory ||= ::Dry::Container::Item::Factory.new
end