Class: SenroUsecaser::ProviderBooter

Inherits:
Object
  • Object
show all
Defined in:
lib/senro_usecaser/configuration.rb

Overview

Provider boot manager

Resolves provider dependencies and boots them in correct order.

Defined Under Namespace

Classes: CircularDependencyError

Instance Method Summary collapse

Constructor Details

#initialize(provider_classes, container) ⇒ ProviderBooter

: (Array, Container) -> void



75
76
77
78
79
80
# File 'lib/senro_usecaser/configuration.rb', line 75

def initialize(provider_classes, container)
  @provider_classes = provider_classes
  @container = container
  @booted_providers = [] #: Array[singleton(Provider)]
  @provider_instances = {} #: Hash[singleton(Provider), Provider]
end

Instance Method Details

#boot! ⇒ Object

Boots all providers in dependency order

: () -> void



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/senro_usecaser/configuration.rb', line 85

def boot!
  sorted = topological_sort(@provider_classes)

  sorted.each do |provider_class|
    next unless provider_class.enabled?

    instance = provider_class.new
    @provider_instances[provider_class] = instance
    instance.register_to(@container)
    @booted_providers << provider_class
  end

  # Call after_boot on all providers
  @booted_providers.each do |provider_class|
    @provider_instances[provider_class].after_boot(@container)
  end
end

#shutdown! ⇒ Object

Shuts down all providers in reverse order

: () -> void



106
107
108
109
110
# File 'lib/senro_usecaser/configuration.rb', line 106

def shutdown!
  @booted_providers.reverse_each do |provider_class|
    @provider_instances[provider_class].shutdown(@container)
  end
end