Class: Dry::System::Provider
- Inherits:
-
Object
- Object
- Dry::System::Provider
- Defined in:
- lib/dry/system/provider.rb,
lib/dry/system/provider/source.rb,
lib/dry/system/provider/source_dsl.rb
Overview
Providers can prepare and register one or more objects and typically work with third party code. A typical provider might be for a database library, or an API client.
The particular behavior for any provider is defined in a Source, which is a subclass created when you run Container.register_provider or register_provider_source. The Source provides this behavior through methods for each of the steps in the provider lifecycle: ‘prepare`, `start`, and `run`. These methods typically create and configure various objects, then register them with the #provider_container.
The Provider manages this lifecycle by implementing common behavior around the lifecycle steps, such as running step callbacks, and only running steps when appropriate for the current status of the lifecycle.
Providers can be registered via Container.register_provider.
Defined Under Namespace
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Returns the provider’s unique name.
-
#namespace ⇒ Symbol, String
readonly
Returns the default namespace for the provider’s container keys.
-
#provider_container ⇒ Dry::Core::Container
(also: #container)
readonly
Returns the container for the provider.
-
#source ⇒ Dry::System::Provider::Source
readonly
private
Returns the provider’s source.
-
#statuses ⇒ Array<Symbol>
readonly
Returns an array of lifecycle steps that have been run.
-
#target_container ⇒ Dry::System::Container
(also: #target)
readonly
Returns the target container for the provider.
Instance Method Summary collapse
-
#initialize(name:, namespace: nil, target_container:, source_class:, source_options: {}, &block) ⇒ Provider
constructor
private
rubocop:disable Layout/LineLength, Style/KeywordParametersOrder.
-
#prepare ⇒ self
Runs the ‘prepare` lifecycle step.
-
#prepared? ⇒ Boolean
Returns true if the provider’s ‘prepare` lifecycle step has run.
-
#start ⇒ self
Runs the ‘start` lifecycle step.
-
#started? ⇒ Boolean
Returns true if the provider’s ‘start` lifecycle step has run.
-
#stop ⇒ self
Runs the ‘stop` lifecycle step.
-
#stopped? ⇒ Boolean
Returns true if the provider’s ‘stop` lifecycle step has run.
Constructor Details
#initialize(name:, namespace: nil, target_container:, source_class:, source_options: {}, &block) ⇒ Provider
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.
rubocop:disable Layout/LineLength, Style/KeywordParametersOrder
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dry/system/provider.rb', line 131 def initialize(name:, namespace: nil, target_container:, source_class:, source_options: {}, &block) @name = name @namespace = namespace @target_container = target_container @provider_container = build_provider_container @statuses = [] @step_running = nil @source = source_class.new( **, provider_container: provider_container, target_container: target_container, &block ) end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Returns the provider’s unique name.
60 61 62 |
# File 'lib/dry/system/provider.rb', line 60 def name @name end |
#namespace ⇒ Symbol, String (readonly)
Returns the default namespace for the provider’s container keys.
67 68 69 |
# File 'lib/dry/system/provider.rb', line 67 def namespace @namespace end |
#provider_container ⇒ Dry::Core::Container (readonly) Also known as: container
Returns the container for the provider.
This is where the provider’s source will register its components, which are then later marged into the target container after the ‘prepare` and `start` lifecycle steps.
96 97 98 |
# File 'lib/dry/system/provider.rb', line 96 def provider_container @provider_container end |
#source ⇒ Dry::System::Provider::Source (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.
Returns the provider’s source
The source provides the specific behavior for the provider via methods implementing the lifecycle steps.
The provider’s source is defined when registering a provider with the container, or an external provider source.
127 128 129 |
# File 'lib/dry/system/provider.rb', line 127 def source @source end |
#statuses ⇒ Array<Symbol> (readonly)
Returns an array of lifecycle steps that have been run.
77 78 79 |
# File 'lib/dry/system/provider.rb', line 77 def statuses @statuses end |
#target_container ⇒ Dry::System::Container (readonly) Also known as: target
Returns the target container for the provider.
This is the container with which the provider is registered (via Container.register_provider).
Registered components from the provider’s container will be merged into this container after the ‘prepare` and `start` lifecycle steps.
110 111 112 |
# File 'lib/dry/system/provider.rb', line 110 def target_container @target_container end |
Instance Method Details
#prepare ⇒ self
Runs the ‘prepare` lifecycle step.
Also runs any callbacks for the step, and then merges any registered components from the provider container into the target container.
157 158 159 |
# File 'lib/dry/system/provider.rb', line 157 def prepare run_step(:prepare) end |
#prepared? ⇒ Boolean
Returns true if the provider’s ‘prepare` lifecycle step has run
190 191 192 |
# File 'lib/dry/system/provider.rb', line 190 def prepared? statuses.include?(:prepare) end |
#start ⇒ self
Runs the ‘start` lifecycle step.
Also runs any callbacks for the step, and then merges any registered components from the provider container into the target container.
169 170 171 172 |
# File 'lib/dry/system/provider.rb', line 169 def start run_step(:prepare) run_step(:start) end |
#started? ⇒ Boolean
Returns true if the provider’s ‘start` lifecycle step has run
197 198 199 |
# File 'lib/dry/system/provider.rb', line 197 def started? statuses.include?(:start) end |
#stop ⇒ self
Runs the ‘stop` lifecycle step.
Also runs any callbacks for the step.
181 182 183 184 185 |
# File 'lib/dry/system/provider.rb', line 181 def stop return self unless started? run_step(:stop) end |
#stopped? ⇒ Boolean
Returns true if the provider’s ‘stop` lifecycle step has run
204 205 206 |
# File 'lib/dry/system/provider.rb', line 204 def stopped? statuses.include?(:stop) end |