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

Examples:


class MyClass
  extend Dry::Container::Mixin
end

MyClass.register(:item, 'item')
MyClass.resolve(:item)
=> 'item'

class MyObject
  include Dry::Container::Mixin
end

container = MyObject.new
container.register(:item, 'item')
container.resolve(:item)
=> 'item'

Defined Under Namespace

Modules: Initializer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dry/container/mixin.rb', line 34

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dry/container/mixin.rb', line 63

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

Parameters:

  • key (Mixed)

    The key for the item you wish to resolve

Returns:

  • (Mixed)

See Also:



127
128
129
# File 'lib/dry/container/mixin.rb', line 127

def [](key)
  resolve(key)
end

#_containerObject



265
266
267
# File 'lib/dry/container/mixin.rb', line 265

def _container
  @_container
end

#decorate(key, with:) ⇒ Dry::Container::Mixin

Decorates an item from the container with specified decorator

Returns:



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/dry/container/mixin.rb', line 208

def decorate(key, with:)
  original = _container.delete(key.to_s) do
    raise Error, "Nothing registered with the key #{key.inspect}"
  end

  decorator = with

  if decorator.is_a?(Class)
    register(key, decorator.new(original.call))
  else
    register(key, decorator)
  end
end

#each(&block) ⇒ Enumerator

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.

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.

Returns:

  • (Enumerator)


199
200
201
# File 'lib/dry/container/mixin.rb', line 199

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.

Returns:



183
184
185
186
# File 'lib/dry/container/mixin.rb', line 183

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

#freezeObject

Freeze the container. Nothing can be registered after freezing



258
259
260
261
262
# File 'lib/dry/container/mixin.rb', line 258

def freeze
  super
  _container.freeze
  self
end

#import(namespace) ⇒ Dry::Container::Mixin

Import a namespace

Parameters:

Returns:



249
250
251
252
253
# File 'lib/dry/container/mixin.rb', line 249

def import(namespace)
  namespace(namespace.name, &namespace.block)

  self
end

#key?(key) ⇒ Bool

Check whether an item is registered under the given key

Parameters:

  • key (Mixed)

    The key you wish to check for registration with

Returns:

  • (Bool)


163
164
165
# File 'lib/dry/container/mixin.rb', line 163

def key?(key)
  config.resolver.key?(_container, key)
end

#keysArray<String>

An array of registered names for the container

Returns:

  • (Array<String>)


172
173
174
# File 'lib/dry/container/mixin.rb', line 172

def keys
  config.resolver.keys(_container)
end

#merge(other, namespace: nil) ⇒ Dry::Container::Mixin

Merge in the items of the other container

Parameters:

  • other (Dry::Container)

    The other container to merge in

  • namespace (Symbol, nil) (defaults to: nil)

    Namespace to prefix other container items with, defaults to nil

Returns:



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/dry/container/mixin.rb', line 141

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

Parameters:

  • namespace (Mixed)

    The namespace to register items in

Returns:



230
231
232
233
234
235
236
237
238
239
# File 'lib/dry/container/mixin.rb', line 230

def namespace(namespace, &block)
  ::Dry::Container::NamespaceDSL.new(
    self,
    namespace,
    config.namespace_separator,
    &block
  )

  self
end

#register(key, contents = nil, options = {}) { ... } ⇒ Dry::Container::Mixin

Register an item with the container to be resolved later

Parameters:

  • key (Mixed)

    The key to register the container item with (used to resolve)

  • contents (Mixed) (defaults to: nil)

    The item to register with the container (if no block given)

  • options (Hash) (defaults to: {})

    Options to pass to the registry when registering the item

Yields:

  • If a block is given, contents will be ignored and the block will be registered instead

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dry/container/mixin.rb', line 93

def register(key, contents = nil, options = {}, &block)
  if block_given?
    item = block
    options = contents if contents.is_a?(::Hash)
  else
    item = contents
  end

  config.registry.call(_container, key, item, options)

  self
end

#resolve(key) ⇒ Mixed

Resolve an item from the container

Parameters:

  • key (Mixed)

    The key for the item you wish to resolve

Returns:

  • (Mixed)


114
115
116
# File 'lib/dry/container/mixin.rb', line 114

def resolve(key)
  config.resolver.call(_container, key)
end