class DependencyManager::Container

Attributes

dependencies[R]

Public Class Methods

new(app_context:, configuration:, factories: Factory.factories) click to toggle source

Creates a new Dependency Container

@param app_context: [Any]

Contextual information for the currently running application

@param configuration: [Hash[Symbol, Any]]

Hash of configuration values, typically loaded from a YAML or JSON file

@param factories: Factory.factories [Array]

All factories to build dependency chain from. This will default to the
`Factory.factories` method, which will grab all children of the base
`Factory` class.

@return [Container]

# File lib/dependency_manager/container.rb, line 27
def initialize(app_context:, configuration:, factories: Factory.factories)
  @app_context = app_context
  @configuration = configuration
  @factories = factories.is_a?(Set) ? factories : Set[*factories]
  @built = false
end

Public Instance Methods

build() click to toggle source

Builds all the dependencies from factories

@return [Hash[Symbol, Any]]

Built resources
# File lib/dependency_manager/container.rb, line 49
def build
  raise BuildOnceError, "Cannot build more than once" if @built

  @dependency_tree = dependency_tree
  @ordered_factory_dependencies = dependency_tree.tsort

  @dependencies = {}

  # Take the ordered factories
  @ordered_factory_dependencies.each do |factory_name|
    # Get their associated class
    factory = DependencyManager::Factory.get(factory_name)

    # Figure out which dependencies we need, which are optional, and which
    # will break the factory build coming up
    resolved_dependencies = Resolver.new(
      factory: factory,
      loaded_dependencies: dependencies
    ).resolve

    # Create an instance of the factory including its resolved dependencies.
    factory_instance = factory.new(
      app_context:    @app_context,
      factory_config: get_config(factory),
      **resolved_dependencies
    )

    # ...and build the dependency based on the provided configuration options.
    @dependencies[factory.dependency_name] = factory_instance.build
  end

  @built = true

  @dependencies
end
dependency_tree() click to toggle source
# File lib/dependency_manager/container.rb, line 101
def dependency_tree
  DependencyTree.new(dependency_hash)
end
fetch(dependency) click to toggle source

Fetch a dependency by name

@param dependency [Symbol]

@return [Any]

# File lib/dependency_manager/container.rb, line 90
def fetch(dependency)
  @dependencies.fetch(dependency)
end
register(factory) click to toggle source

Register a factory explicitly

@param factory [type] [description]

@return [type] [description]

# File lib/dependency_manager/container.rb, line 39
def register(factory)
  raise AddedFactoryAfterBuildError, "Cannot add Factories after Container has been built" if @built

  @factories.add factory
end
to_h() click to toggle source

Listing of all dependencies

@return [Hash[Symbol, Any]]

# File lib/dependency_manager/container.rb, line 97
def to_h
  @dependencies
end

Private Instance Methods

dependency_hash() click to toggle source
# File lib/dependency_manager/container.rb, line 105
        def dependency_hash
  @factories.map { |k| [k.name, k.factory_dependencies] }.to_h
end
get_config(klass) click to toggle source

Gets the dependencies configuration from the master configuration.

@param klass [Class]

Class to get configuration for

@return [Hash[Symbol, Any]]

# File lib/dependency_manager/container.rb, line 115
        def get_config(klass)
  @configuration.fetch(klass.dependency_name, {})
end