Skip to content

Commit

Permalink
Add configurable provider_source_class
Browse files Browse the repository at this point in the history
Also allow provider registrar to pass additional extra options to provider sources when initializing.
  • Loading branch information
timriley committed Jun 27, 2024
1 parent f826656 commit b80e73c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/dry/system/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Container
setting :auto_registrar, default: Dry::System::AutoRegistrar
setting :manifest_registrar, default: Dry::System::ManifestRegistrar
setting :provider_registrar, default: Dry::System::ProviderRegistrar
setting :provider_source_class, default: Dry::System::Provider::Source
setting :importer, default: Dry::System::Importer

# Expect "." as key namespace separator. This is not intended to be user-configurable.
Expand Down
7 changes: 3 additions & 4 deletions lib/dry/system/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Provider
attr_reader :source

# @api private
def initialize(name:, namespace: nil, target_container:, source_class:, &block) # rubocop:disable Style/KeywordParametersOrder
def initialize(name:, namespace: nil, target_container:, build_source:) # rubocop:disable Style/KeywordParametersOrder
@name = name
@namespace = namespace
@target_container = target_container
Expand All @@ -136,10 +136,9 @@ def initialize(name:, namespace: nil, target_container:, source_class:, &block)
@statuses = []
@step_running = nil

@source = source_class.new(
@source = build_source.call(
provider_container: provider_container,
target_container: target_container,
&block
target_container: target_container
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dry/system/provider/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def inspect
alias_method :target, :target_container

# @api private
def initialize(provider_container:, target_container:, &block)
def initialize(provider_container:, target_container:, **, &block)
super()
@callbacks = {before: CALLBACK_MAP.dup, after: CALLBACK_MAP.dup}
@provider_container = provider_container
Expand Down
14 changes: 10 additions & 4 deletions lib/dry/system/provider_registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ def provider_paths
end

def build_provider(name, options:, source: nil, &block)
source_class = source || Provider::Source.for(name: name, &block)
source_class = source || container.config.provider_source_class.for(name: name, &block)

Provider.new(
**options,
name: name,
target_container: target_container,
source_class: source_class
build_source: -> **opts {
source_class.new(**provider_source_options, **opts)
}
)
end

Expand All @@ -214,11 +216,15 @@ def build_provider_from_source(name, source:, group:, options:, &block)
**options,
name: name,
target_container: target_container,
source_class: provider_source.source,
&block
build_source: -> **opts {
provider_source.source.new(**provider_source_options, **opts, &block)
}
)
end

# Extension point for subclasses
def provider_source_options = {}

def with_provider(provider_name)
require_provider_file(provider_name) unless providers.key?(provider_name)

Expand Down

0 comments on commit b80e73c

Please sign in to comment.