Class: DependencyManager::Container

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

Defined Under Namespace

Classes: AddedFactoryAfterBuildError, BuildOnceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_context:, configuration:, factories: Factory.factories) ⇒ Container

Creates a new Dependency Container

Parameters:

  • app_context: (Any)

    Contextual information for the currently running application

  • configuration: (Hash[Symbol, Any])

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

  • factories: (defaults to: Factory.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.



27
28
29
30
31
32
# 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

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



11
12
13
# File 'lib/dependency_manager/container.rb', line 11

def dependencies
  @dependencies
end

Instance Method Details

#buildHash[Symbol, Any]

Builds all the dependencies from factories

Returns:

  • (Hash[Symbol, Any])

    Built resources

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_treeObject



101
102
103
# File 'lib/dependency_manager/container.rb', line 101

def dependency_tree
  DependencyTree.new(dependency_hash)
end

#fetch(dependency) ⇒ Any

Fetch a dependency by name

Parameters:

  • dependency (Symbol)

Returns:

  • (Any)


90
91
92
# File 'lib/dependency_manager/container.rb', line 90

def fetch(dependency)
  @dependencies.fetch(dependency)
end

#register(factory) ⇒ type

Register a factory explicitly

Parameters:

  • factory (type)
    description

Returns:

  • (type)
    description

Raises:



39
40
41
42
43
# 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_hHash[Symbol, Any]

Listing of all dependencies

Returns:

  • (Hash[Symbol, Any])


97
98
99
# File 'lib/dependency_manager/container.rb', line 97

def to_h
  @dependencies
end