class Dry::Container::Resolver

Default resolver for resolving items from container

@api public

Public Instance Methods

call(container, key) { |key| ... } click to toggle source

Resolve an item from the container

@param [Concurrent::Hash] container

The container

@param [Mixed] key

The key for the item you wish to resolve

@yield

Fallback block to call when a key is missing. Its result will be returned

@yieldparam [Mixed] key Missing key

@raise [Dry::Container::Error]

If the given key is not registered with the container (and no block provided)

@return [Mixed]

@api public

# File lib/dry/container/resolver.rb, line 26
def call(container, key)
  item = container.fetch(key.to_s) do
    if block_given?
      return yield(key)
    else
      raise Error, "Nothing registered with the key #{key.inspect}"
    end
  end

  item.call
end
each(container, &block) click to toggle source

Calls block once for each key in container, passing the key and the registered item parameters.

If no block is given, an enumerator is returned instead.

@return Key, Value

@api public @note In discussions with other developers, it was felt that being able to iterate over not just

the registered keys, but to see what was registered would be very helpful. This is a step
toward doing that.
# File lib/dry/container/resolver.rb, line 82
def each(container, &block)
  container.map { |key, value| [key, value.call] }.each(&block)
end
each_key(container, &block) click to toggle source

Calls block once for each key in container, passing the key as a parameter.

If no block is given, an enumerator is returned instead.

@return Hash

@api public

# File lib/dry/container/resolver.rb, line 68
def each_key(container, &block)
  container.each_key(&block)
end
key?(container, key) click to toggle source

Check whether an items is registered under the given key

@param [Concurrent::Hash] container

The container

@param [Mixed] key

The key you wish to check for registration with

@return [Bool]

@api public

# File lib/dry/container/resolver.rb, line 48
def key?(container, key)
  container.key?(key.to_s)
end
keys(container) click to toggle source

An array of registered names for the container

@return [Array]

@api public

# File lib/dry/container/resolver.rb, line 57
def keys(container)
  container.keys
end