From b80e73cfc84fbce1f989fb0d016c0d84da43c013 Mon Sep 17 00:00:00 2001 From: Tim Riley Date: Thu, 27 Jun 2024 22:06:47 +1000 Subject: [PATCH] Add configurable provider_source_class Also allow provider registrar to pass additional extra options to provider sources when initializing. --- lib/dry/system/container.rb | 1 + lib/dry/system/provider.rb | 7 +++---- lib/dry/system/provider/source.rb | 2 +- lib/dry/system/provider_registrar.rb | 14 ++++++++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/dry/system/container.rb b/lib/dry/system/container.rb index b324f8e4..b882b87e 100644 --- a/lib/dry/system/container.rb +++ b/lib/dry/system/container.rb @@ -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. diff --git a/lib/dry/system/provider.rb b/lib/dry/system/provider.rb index 6a5d0c0a..530f19c4 100644 --- a/lib/dry/system/provider.rb +++ b/lib/dry/system/provider.rb @@ -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 @@ -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 diff --git a/lib/dry/system/provider/source.rb b/lib/dry/system/provider/source.rb index 42ad58b1..04e2ec2b 100644 --- a/lib/dry/system/provider/source.rb +++ b/lib/dry/system/provider/source.rb @@ -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 diff --git a/lib/dry/system/provider_registrar.rb b/lib/dry/system/provider_registrar.rb index 6f078735..c7499929 100644 --- a/lib/dry/system/provider_registrar.rb +++ b/lib/dry/system/provider_registrar.rb @@ -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 @@ -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)