Module: Dry::Container::Mixin
- Included in:
- Dry::Container
- Defined in:
- lib/dry/container/mixin.rb,
lib/dry/container/stub.rb
Overview
Mixin to expose Inversion of Control (IoC) container behaviour
Defined Under Namespace
Modules: Initializer
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Mixed
Resolve an item from the container.
- #_container ⇒ Object
- #clone ⇒ Object
-
#decorate(key, with: nil, &block) ⇒ Dry::Container::Mixin
Decorates an item from the container with specified decorator.
- #dup ⇒ Object
-
#each(&block) ⇒ Enumerator
Calls block once for each key/value pair in the container, passing the key and the registered item parameters.
-
#each_key(&block) ⇒ Dry::Container::Mixin
Calls block once for each key in container, passing the key as a parameter.
-
#enable_stubs! ⇒ Object
Enable stubbing functionality into the current container.
-
#freeze ⇒ Object
Freeze the container.
-
#import(namespace) ⇒ Dry::Container::Mixin
Import a namespace.
-
#key?(key) ⇒ Bool
Check whether an item is registered under the given key.
-
#keys ⇒ Array<String>
An array of registered names for the container.
-
#merge(other, namespace: nil) ⇒ Dry::Container::Mixin
Merge in the items of the other container.
-
#namespace(namespace, &block) ⇒ Dry::Container::Mixin
Evaluate block and register items in namespace.
-
#register(key, contents = nil, options = EMPTY_HASH) { ... } ⇒ Dry::Container::Mixin
Register an item with the container to be resolved later.
-
#resolve(key) {|key| ... } ⇒ Mixed
Resolve an item from the container.
Class Method Details
.extended(base) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dry/container/mixin.rb', line 37 def self.extended(base) hooks_mod = ::Module.new do def inherited(subclass) subclass.instance_variable_set(:@_container, @_container.dup) super end end base.class_eval do extend ::Dry::Configurable extend hooks_mod setting :registry, ::Dry::Container::Registry.new setting :resolver, ::Dry::Container::Resolver.new setting :namespace_separator, '.' @_container = ::Concurrent::Hash.new end end |
.included(base) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dry/container/mixin.rb', line 66 def self.included(base) base.class_eval do extend ::Dry::Configurable prepend Initializer setting :registry, ::Dry::Container::Registry.new setting :resolver, ::Dry::Container::Resolver.new setting :namespace_separator, '.' def config self.class.config end end end |
Instance Method Details
#[](key) ⇒ Mixed
Resolve an item from the container
133 134 135 |
# File 'lib/dry/container/mixin.rb', line 133 def [](key) resolve(key) end |
#_container ⇒ Object
275 276 277 |
# File 'lib/dry/container/mixin.rb', line 275 def _container @_container end |
#clone ⇒ Object
287 288 289 290 291 292 293 |
# File 'lib/dry/container/mixin.rb', line 287 def clone copy = super unless copy.frozen? copy.instance_variable_set(:@_container, _container.dup) end copy end |
#decorate(key, with: nil, &block) ⇒ Dry::Container::Mixin
Decorates an item from the container with specified decorator
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/dry/container/mixin.rb', line 214 def decorate(key, with: nil, &block) key = key.to_s original = _container.delete(key) do raise Error, "Nothing registered with the key #{key.inspect}" end if with.is_a?(Class) decorator = with.method(:new) elsif block.nil? && !with.respond_to?(:call) raise Error, "Decorator needs to be a Class, block, or respond to the `call` method" else decorator = with || block end _container[key] = original.map(decorator) self end |
#dup ⇒ Object
280 281 282 283 284 |
# File 'lib/dry/container/mixin.rb', line 280 def dup copy = super copy.instance_variable_set(:@_container, _container.dup) copy end |
#each(&block) ⇒ Enumerator
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/value pair in the container, passing the key and the registered item parameters.
If no block is given, an enumerator is returned instead.
205 206 207 |
# File 'lib/dry/container/mixin.rb', line 205 def each(&block) config.resolver.each(_container, &block) end |
#each_key(&block) ⇒ Dry::Container::Mixin
Calls block once for each key in container, passing the key as a parameter.
If no block is given, an enumerator is returned instead.
189 190 191 192 |
# File 'lib/dry/container/mixin.rb', line 189 def each_key(&block) config.resolver.each_key(_container, &block) self end |
#enable_stubs! ⇒ Object
Enable stubbing functionality into the current container
48 49 50 |
# File 'lib/dry/container/stub.rb', line 48 def enable_stubs! extend ::Dry::Container::Stub end |
#freeze ⇒ Object
Freeze the container. Nothing can be registered after freezing
268 269 270 271 272 |
# File 'lib/dry/container/mixin.rb', line 268 def freeze super _container.freeze self end |
#import(namespace) ⇒ Dry::Container::Mixin
Import a namespace
259 260 261 262 263 |
# File 'lib/dry/container/mixin.rb', line 259 def import(namespace) namespace(namespace.name, &namespace.block) self end |
#key?(key) ⇒ Bool
Check whether an item is registered under the given key
169 170 171 |
# File 'lib/dry/container/mixin.rb', line 169 def key?(key) config.resolver.key?(_container, key) end |
#keys ⇒ Array<String>
An array of registered names for the container
178 179 180 |
# File 'lib/dry/container/mixin.rb', line 178 def keys config.resolver.keys(_container) end |
#merge(other, namespace: nil) ⇒ Dry::Container::Mixin
Merge in the items of the other container
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/dry/container/mixin.rb', line 147 def merge(other, namespace: nil) if namespace _container.merge!( other._container.each_with_object(::Concurrent::Hash.new) do |a, h| h[PREFIX_NAMESPACE.call(namespace, a.first, config)] = a.last end ) else _container.merge!(other._container) end self end |
#namespace(namespace, &block) ⇒ Dry::Container::Mixin
Evaluate block and register items in namespace
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/dry/container/mixin.rb', line 240 def namespace(namespace, &block) ::Dry::Container::NamespaceDSL.new( self, namespace, config.namespace_separator, &block ) self end |
#register(key, contents = nil, options = EMPTY_HASH) { ... } ⇒ Dry::Container::Mixin
Register an item with the container to be resolved later
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dry/container/mixin.rb', line 96 def register(key, contents = nil, = EMPTY_HASH, &block) if block_given? item = block = contents if contents.is_a?(::Hash) else item = contents end config.registry.call(_container, key, item, ) self end |
#resolve(key) {|key| ... } ⇒ Mixed
Resolve an item from the container
120 121 122 |
# File 'lib/dry/container/mixin.rb', line 120 def resolve(key, &block) config.resolver.call(_container, key, &block) end |