Class: Carb::Inject::DependencyList

Inherits:
Module
  • Object
show all
Defined in:
lib/carb/inject/dependency_list.rb

Overview

Provides the list of dependencies required by the object to be initialized

Instance Method Summary collapse

Constructor Details

#initialize(container, auto_inject = true, **dependencies) ⇒ DependencyList

Uses the passed container to resolve listed dependencies

Parameters:

  • container (#[])

    resolve dependencies by using the value of ‘dependencies` hash passed to this initializer

  • auto_inject (Boolean) (defaults to: true)

    if true, includes AutoInjectable which provides an initializer that inject dependencies automatically

  • dependencies (Hash{Symbol => Object})

    a hash representing required dependencies for the object. The key represent the alias used to access the real dependency name, which is the value of the hash. If you want to access the dependency using its real name, just set the alias to the real name. Example: ‘{ alias: :real_name }`



28
29
30
31
32
33
34
# File 'lib/carb/inject/dependency_list.rb', line 28

def initialize(container, auto_inject = true, **dependencies)
  ensure_correct_types!(container, dependencies)

  @container    = container
  @auto_inject  = auto_inject
  @dependencies = dependencies
end

Instance Method Details

#each {|name, dependency| ... } ⇒ Object

Loops over each available dependency and yields it using its alias and the dependency itself

Yield Parameters:

  • name (Object)

    alias of the dependency for this dependency_list

  • dependency (Object)

    the dependency object for this container



46
47
48
49
50
# File 'lib/carb/inject/dependency_list.rb', line 46

def each
  dependencies.each do |key, value|
    yield(key, container[value])
  end
end

#included(klass) ⇒ Object



36
37
38
39
40
# File 'lib/carb/inject/dependency_list.rb', line 36

def included(klass)
  memoize_dependency_list(klass)
  include_injectable(klass)
  define_readers(klass)
end