Class: Dinja::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/dinja/container.rb

Overview

Dependency injection container

Defined Under Namespace

Classes: DependencyAlreadyRegistered, DependencyNotRegistered

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContainer

Returns a new instance of Container.



8
9
10
# File 'lib/dinja/container.rb', line 8

def initialize
  @dependencies = {}
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



6
7
8
# File 'lib/dinja/container.rb', line 6

def dependencies
  @dependencies
end

Instance Method Details

#lookup(key) ⇒ Object

Look up a dependency without resolving it

Parameters:

  • key (String)

    The unique name under which the dependency was registered

Raises:



40
41
42
43
44
# File 'lib/dinja/container.rb', line 40

def lookup(key)
  raise DependencyNotRegistered, "Dependency not registered: #{key}" unless dependencies.key?(key)

  dependencies[key]
end

#lookup!(key) ⇒ Object

Look up a dependency without resolving it

Parameters:

  • key (String)

    The unique name under which the dependency was registered



50
51
52
# File 'lib/dinja/container.rb', line 50

def lookup!(key)
  dependencies[key]
end

#register(key, &block) ⇒ Object

Register a dependency

Parameters:

  • key (String)

    The unique key under which the dependency will be registered

  • &block (Proc)

    The proc to be called when resolving the dependency

Raises:



19
20
21
22
23
# File 'lib/dinja/container.rb', line 19

def register(key, &block)
  raise DependencyAlreadyRegistered, "Dependency already registered: #{key}" if dependencies.key?(key)

  register!(key, &block)
end

#register!(key, &block) ⇒ Object

Register or overwrite an existing dependency

Parameters:

  • key (String)

    The unique key under which the dependency will be registered

  • &block (Proc)

    The proc to be called when resolving the dependency



30
31
32
# File 'lib/dinja/container.rb', line 30

def register!(key, &block)
  dependencies[key] = block
end

#resolve(key, *args, **kwargs, &block) ⇒ Object

Resolve a dependency

Parameters:

  • key (String)

    The unique key under which the dependency was registered

  • *args (Object)

    The arguments to pass when resolving the dependency

  • &block (Proc)

    The block to pass when resolving the dependency

Raises:



62
63
64
# File 'lib/dinja/container.rb', line 62

def resolve(key, *args, **kwargs, &block)
  lookup(key).call(*args, **kwargs, &block)
end

#resolve!(key, *args, **kwargs, &block) ⇒ Object

Resolve a dependency

Parameters:

  • key (String)

    The unique key under which the dependency was registered

  • *args (Object)

    The arguments to pass when resolving the dependency

  • &block (Proc)

    The block to pass when resolving the dependency



72
73
74
# File 'lib/dinja/container.rb', line 72

def resolve!(key, *args, **kwargs, &block)
  lookup!(key)&.call(*args, **kwargs, &block)
end