Class: Dry::Core::Container::Resolver
- Inherits:
-
Object
- Object
- Dry::Core::Container::Resolver
- Defined in:
- lib/dry/core/container/resolver.rb
Overview
Default resolver for resolving items from container
Instance Method Summary collapse
-
#call(container, key) {|key| ... } ⇒ Mixed
Resolve an item from the container.
-
#each(container, &block) ⇒ Object
Calls block once for each key in container, passing the key and the registered item parameters.
-
#each_key(container, &block) ⇒ Object
Calls block once for each key in container, passing the key as a parameter.
-
#key?(container, key) ⇒ Bool
Check whether an items is registered under the given key.
-
#keys(container) ⇒ Array
An array of registered names for the container.
Instance Method Details
#call(container, key) {|key| ... } ⇒ Mixed
Resolve an item from the container
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dry/core/container/resolver.rb', line 27 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 |
#each(container, &block) ⇒ Object
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.
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.
84 85 86 |
# File 'lib/dry/core/container/resolver.rb', line 84 def each(container, &block) container.map { |key, value| [key, value.call] }.each(&block) end |
#each_key(container, &block) ⇒ Object
Calls block once for each key in container, passing the key as a parameter.
If no block is given, an enumerator is returned instead.
69 70 71 |
# File 'lib/dry/core/container/resolver.rb', line 69 def each_key(container, &block) container.each_key(&block) end |
#key?(container, key) ⇒ Bool
Check whether an items is registered under the given key
49 50 51 |
# File 'lib/dry/core/container/resolver.rb', line 49 def key?(container, key) container.key?(key.to_s) end |
#keys(container) ⇒ Array
An array of registered names for the container
58 59 60 |
# File 'lib/dry/core/container/resolver.rb', line 58 def keys(container) container.keys end |