Class: DependencyManager::Container
- Inherits:
-
Object
- Object
- DependencyManager::Container
- Defined in:
- lib/dependency_manager/container.rb
Defined Under Namespace
Classes: AddedFactoryAfterBuildError, BuildOnceError
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
Instance Method Summary collapse
-
#build ⇒ Hash[Symbol, Any]
Builds all the dependencies from factories.
- #dependency_tree ⇒ Object
-
#fetch(dependency) ⇒ Any
Fetch a dependency by name.
-
#initialize(app_context:, configuration:, factories: Factory.factories) ⇒ Container
constructor
Creates a new Dependency Container.
-
#register(factory) ⇒ type
Register a factory explicitly.
-
#to_h ⇒ Hash[Symbol, Any]
Listing of all dependencies.
Constructor Details
#initialize(app_context:, configuration:, factories: Factory.factories) ⇒ Container
Creates a new Dependency Container
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
#dependencies ⇒ Object (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
#build ⇒ Hash[Symbol, Any]
Builds all the dependencies from factories
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_tree ⇒ Object
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
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
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_h ⇒ Hash[Symbol, Any]
Listing of all dependencies
97 98 99 |
# File 'lib/dependency_manager/container.rb', line 97 def to_h @dependencies end |