Class: DependencyManager::Resolver

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

Overview

Resolves a factories dependencies against the currently loaded dependency list. Checks for potential missing dependencies and reconciles against optional and required dependencies.

Defined Under Namespace

Classes: MissingDependencies

Instance Method Summary collapse

Constructor Details

#initialize(factory:, loaded_dependencies:) ⇒ Resolver

Creates a new Resolver

Parameters:

  • factory: (Factory)

    Factory instance

  • loaded_dependencies: (Hash[Symbol, Any])

    Map of currently loaded dependencies



18
19
20
21
# File 'lib/dependency_manager/resolver.rb', line 18

def initialize(factory:, loaded_dependencies:)
  @factory = factory
  @loaded_dependencies = loaded_dependencies
end

Instance Method Details

#resolveHash[Symbol, Any]

Resolves dependencies from the currently loaded dependencies

Returns:

  • (Hash[Symbol, Any])

    Dependencies necessary to build the factory

Raises:

  • (MissingDependencies)

    When there are missing dependencies required by the current factory that will prevent the factory from building



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependency_manager/resolver.rb', line 31

def resolve
  # Resolve required dependencies from our current list
  resolved_dependencies = @loaded_dependencies.slice(*@factory.dependencies)
  required_dependencies = @factory.required_dependencies

  # But check if there are a few missing. Optional dependencies not included in this
  missing_dependencies = required_dependencies.reject do |dependency_name|
    !!resolved_dependencies[dependency_name]
  end

  if missing_dependencies.any?
    error = missing_dependencies.join(', ')
    raise MissingDependencies, "Dependencies for `#{@factory.const_name}` are not present: #{error}"
  end

  resolved_dependencies
end