Class: Dry::System::ProviderRegistrar
- Inherits:
-
Object
- Object
- Dry::System::ProviderRegistrar
- Defined in:
- lib/dry/system/provider_registrar.rb
Overview
Default provider registrar implementation
This is currently configured by default for every Dry::System::Container. The provider registrar is responsible for loading provider files and exposing an API for running the provider lifecycle steps.
Instance Attribute Summary collapse
- #container ⇒ Object (also: #target_container) readonly private
- #providers ⇒ Object readonly private
Instance Method Summary collapse
-
#[](provider_name) ⇒ Dry::System::Provider?
Returns a provider if it can be found or loaded, otherwise nil.
- #finalize! ⇒ Object private
- #freeze ⇒ Object private
-
#initialize(container) ⇒ ProviderRegistrar
constructor
private
A new instance of ProviderRegistrar.
- #key?(provider_name) ⇒ Boolean private
- #prepare(provider_name) ⇒ Object private
-
#provider_files ⇒ Array<Pathname>
Returns all provider files within the configured provider_paths.
-
#provider_source_class ⇒ Object
Extension point for subclasses to customize their provider source superclass.
-
#provider_source_options ⇒ Object
Extension point for subclasses to customize initialization params for provider_source_class.
- #register_provider(name, from: nil, source: nil, if: true, **provider_options, &block) ⇒ Object private
- #shutdown ⇒ Object private
- #start(provider_name) ⇒ Object private
- #stop(provider_name) ⇒ Object private
Constructor Details
#initialize(container) ⇒ ProviderRegistrar
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ProviderRegistrar.
34 35 36 37 |
# File 'lib/dry/system/provider_registrar.rb', line 34 def initialize(container) @providers = {} @container = container end |
Instance Attribute Details
#container ⇒ Object (readonly) Also known as: target_container
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 |
# File 'lib/dry/system/provider_registrar.rb', line 23 def container @container end |
#providers ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 21 22 |
# File 'lib/dry/system/provider_registrar.rb', line 20 def providers @providers end |
Instance Method Details
#[](provider_name) ⇒ Dry::System::Provider?
Returns a provider if it can be found or loaded, otherwise nil
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dry/system/provider_registrar.rb', line 92 def [](provider_name) provider_name = provider_name.to_sym if (provider = providers[provider_name]) return provider end return if finalized? require_provider_file(provider_name) providers[provider_name] end |
#finalize! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 156 157 158 159 160 161 162 163 |
# File 'lib/dry/system/provider_registrar.rb', line 155 def finalize! provider_files.each do |path| load_provider(path) end providers.each_value(&:start) freeze end |
#freeze ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
40 41 42 43 |
# File 'lib/dry/system/provider_registrar.rb', line 40 def freeze providers.freeze super end |
#key?(provider_name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
107 108 109 |
# File 'lib/dry/system/provider_registrar.rb', line 107 def key?(provider_name) providers.key?(provider_name) end |
#prepare(provider_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
179 180 181 182 |
# File 'lib/dry/system/provider_registrar.rb', line 179 def prepare(provider_name) with_provider(provider_name, &:prepare) self end |
#provider_files ⇒ Array<Pathname>
Returns all provider files within the configured provider_paths.
Searches for files in the order of the configured provider_paths. In the case of multiple identically-named boot files within different provider_paths, the file found first will be returned, and other matching files will be discarded.
This method is public to allow other tools extending dry-system (like dry-rails) to access a canonical list of real, in-use provider files.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/dry/system/provider_registrar.rb', line 124 def provider_files @provider_files ||= provider_paths.each_with_object([[], []]) { |path, (provider_files, loaded)| # rubocop:disable Layout/LineLength files = Dir["#{path}/#{RB_GLOB}"].sort files.each do |file| basename = File.basename(file) unless loaded.include?(basename) provider_files << Pathname(file) loaded << basename end end }.first end |
#provider_source_class ⇒ Object
Extension point for subclasses to customize their provider source superclass. Expected to be a subclass of Dry::System::Provider::Source
145 |
# File 'lib/dry/system/provider_registrar.rb', line 145 def provider_source_class = Dry::System::Provider::Source |
#provider_source_options ⇒ Object
Extension point for subclasses to customize initialization params for provider_source_class
152 |
# File 'lib/dry/system/provider_registrar.rb', line 152 def = {} |
#register_provider(name, from: nil, source: nil, if: true, **provider_options, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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/dry/system/provider_registrar.rb', line 49 def register_provider(name, from: nil, source: nil, if: true, **, &block) raise ProviderAlreadyRegisteredError, name if providers.key?(name) if from && source.is_a?(Class) raise ArgumentError, "You must supply a block when using a provider source" end if block && source.is_a?(Class) raise ArgumentError, "You must supply only a `source:` option or a block, not both" end return self unless binding.local_variable_get(:if) provider = if from build_provider_from_source( name, source: source || name, group: from, options: , &block ) else build_provider( name, source: source, options: , &block ) end providers[provider.name] = provider self end |
#shutdown ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
173 174 175 176 |
# File 'lib/dry/system/provider_registrar.rb', line 173 def shutdown providers.each_value(&:stop) self end |
#start(provider_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
185 186 187 188 |
# File 'lib/dry/system/provider_registrar.rb', line 185 def start(provider_name) with_provider(provider_name, &:start) self end |
#stop(provider_name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
191 192 193 194 |
# File 'lib/dry/system/provider_registrar.rb', line 191 def stop(provider_name) with_provider(provider_name, &:stop) self end |