Class: Dry::Core::Container::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/core/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.



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

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:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dry/core/container/registry.rb', line 33

def call(container, key, item, options)
  key = key.to_s.dup.freeze

  @_mutex.synchronize do
    if container.key?(key)
      raise KeyError, "There is already an item registered with the key #{key.inspect}"
    end

    container[key] = factory.call(item, options)
  end
end

#factoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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