class Dio::Container

Container stores dependency objects.

Public Class Methods

new(factories = {}) click to toggle source
# File lib/dio/container.rb, line 6
def initialize(factories = {})
  @factories = factories
end

Public Instance Methods

factory(key) click to toggle source

Returns a factory associated with the given key.

@param key [Object] @return [Proc]

# File lib/dio/container.rb, line 44
def factory(key)
  @factories[key]
end
load(key, *args) click to toggle source

Executes a factory of the given key and loads an object.

@param key [Object] @param args [Array] They are passed to the factory proc as arguments. @return [Object]

# File lib/dio/container.rb, line 53
def load(key, *args)
  @factories[key]&.call(*args)
end
register(key, &factory) click to toggle source

Registers a dependency factory with a given key. If the key is already used, the previous factory is removed. @param key [Object] @param factory [Block] @return [Dio::Container]

# File lib/dio/container.rb, line 15
def register(key, &factory)
  @factories[key] = factory
  self
end
register_all(deps) click to toggle source

Registers all key value pairs of a given hash as dependencies.

@param deps [Hash] @return [Dio::Container] @see Dio::Container#register

# File lib/dio/container.rb, line 25
def register_all(deps)
  deps.each do |key, factory|
    register(key, &factory)
  end
  self
end
registered?(key) click to toggle source

Return a given key is taken or not.

@param key [Object] @return [boolean]

# File lib/dio/container.rb, line 36
def registered?(key)
  @factories.key?(key)
end