class Dry::Container::Resolver
Default resolver for resolving items from container
@api public
Public Instance Methods
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 [KeyError]
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 KeyError.new(%(key not found: "#{key}"), key: key.to_s, receiver: container) end end item.call end
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 83 def each(container, &block) container.map { |key, value| [key, value.call] }.each(&block) end
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
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
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