Class: Payload::Container

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

Overview

Used for configuring and resolving dependencies.

Instance Method Summary collapse

Constructor Details

#initialize(definitions = DefinitionList.new) ⇒ Container

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used internally by RailsLoader.

Parameters:

  • definitions (DefinitionList) (defaults to: DefinitionList.new)

    previously defined definitions.



18
19
20
# File 'lib/payload/container.rb', line 18

def initialize(definitions = DefinitionList.new)
  @definitions = definitions
end

Instance Method Details

#[](dependency) ⇒ Object

Resolves and returns dependency.

Parameters:

  • dependency (Symbol)

    the name of the dependency to resolve.

Raises:



60
61
62
# File 'lib/payload/container.rb', line 60

def [](dependency)
  @definitions.find(dependency).resolve(self)
end

#decorate(dependency) {|Container| ... } ⇒ Object

Extends or replaces an existing dependency definition.

Parameters:

  • dependency (Symbol)

    the name of the dependency to decorate.

Yields:

Yield Returns:

  • the decorated instance.



27
28
29
# File 'lib/payload/container.rb', line 27

def decorate(dependency, &block)
  self.class.new(@definitions.decorate(dependency, block))
end

#export(*names) ⇒ DependencyList

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Exports dependencies which can be imported into another container.

Used internally by MutableContainer. Use MutableContainer#export.

Parameters:

  • names (Array<Symbol>)

    dependencies to export.

Returns:

  • (DependencyList)

    exported dependencies.



71
72
73
# File 'lib/payload/container.rb', line 71

def export(*names)
  @definitions.export(names)
end

#factory(dependency) {|Container| ... } ⇒ Object

Defines a factory which can be used to instantiate the dependency. Useful if some dependencies come from the container but others come from runtime state.

Resolving the dependency will return an object which responds to ‘new`. The `new` method will accept remaining dependencies and return the fully resolved dependency from the given block.

Parameters:

  • dependency (Symbol)

    the name of the dependency to define.

Yields:

  • (Container)

    the resolved container; any arguments to ‘new` will be added to the container.

Yield Returns:

  • an instance of your dependency.



43
44
45
# File 'lib/payload/container.rb', line 43

def factory(dependency, &block)
  define dependency, FactoryResolver.new(block)
end

#import(definitions) ⇒ Container

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Import dependencies which were exported from another container.

Used internally by RailsLoader.

Parameters:

  • definitions (DependencyList)

    definitions to import.

Returns:

  • (Container)

    a new container with the imported definitions.



82
83
84
# File 'lib/payload/container.rb', line 82

def import(definitions)
  self.class.new @definitions.import(definitions)
end

#service(dependency) {|Container| ... } ⇒ Object

Defines a service which can be fully resolved from the container.

Parameters:

  • dependency (Symbol)

    the name of the dependency to define.

Yields:

Yield Returns:

  • the instantiated service.



52
53
54
# File 'lib/payload/container.rb', line 52

def service(dependency, &block)
  define dependency, ServiceResolver.new(block)
end