-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Provider Sources to choose a custom superclass
Relates to hanami/hanami#1417 The motivation for this is to allow consuming frameworks of dry-system to define their own superclass for providers, in order to add their own method apis to the class. The is only used for user-defined providers with a block implementation. External providers in a group do not allow this, because their superclass is defined ahead of time when they are added to the source registry. If an external provider source wants to use a different superclass, they can define a concrete class of their own instead. The custom superclass is assumed to be a child of Dry::System::Provider::Source. In addition to `provider_source_class`, ProviderRegistrar contains `provider_source_options` as an extension point for subclasses to send custom intialization params to the source class.
- Loading branch information
Showing
4 changed files
with
141 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
spec/integration/container/providers/custom_provider_superclass_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Providers / Custom provider superclass" do | ||
let!(:custom_superclass) do | ||
module Test | ||
class CustomSource < Dry::System::Provider::Source | ||
attr_reader :custom_setting | ||
|
||
def initialize(custom_setting:, **options, &block) | ||
super(**options, &block) | ||
@custom_setting = custom_setting | ||
end | ||
end | ||
end | ||
|
||
Test::CustomSource | ||
end | ||
|
||
let!(:custom_registrar) do | ||
module Test | ||
class CustomRegistrar < Dry::System::ProviderRegistrar | ||
def provider_source_class = Test::CustomSource | ||
def provider_source_options = {custom_setting: "hello"} | ||
end | ||
end | ||
|
||
Test::CustomRegistrar | ||
end | ||
|
||
subject(:system) do | ||
module Test | ||
class Container < Dry::System::Container | ||
configure do |config| | ||
config.root = SPEC_ROOT.join("fixtures/app").realpath | ||
config.provider_registrar = Test::CustomRegistrar | ||
end | ||
end | ||
end | ||
|
||
Test::Container | ||
end | ||
|
||
it "overrides the default Provider Source base class" do | ||
system.register_provider(:test) {} | ||
|
||
provider_source = system.providers[:test].source | ||
|
||
expect(provider_source.class).to be < custom_superclass | ||
expect(provider_source.class.name).to eq "Test::CustomSource[test]" | ||
expect(provider_source.custom_setting).to eq "hello" | ||
end | ||
|
||
context "Source class != provider_source_class" do | ||
let!(:custom_source) do | ||
module Test | ||
class OtherSource < Dry::System::Provider::Source | ||
attr_reader :options | ||
|
||
def initialize(provider_container:, target_container:, **options, &block) | ||
@options = options | ||
super | ||
end | ||
end | ||
end | ||
|
||
Test::OtherSource | ||
end | ||
|
||
specify "External source doesn't use provider_source_options" do | ||
Dry::System.register_provider_source(:test, group: :custom, source: custom_source) | ||
system.register_provider(:test, from: :custom) {} | ||
|
||
expect { | ||
provider_source = system.providers[:test].source | ||
expect(provider_source.class).to be < Dry::System::Provider::Source | ||
expect(provider_source.options).to be_empty | ||
}.to_not raise_error | ||
end | ||
|
||
specify "Class-based source doesn't use provider_source_options" do | ||
system.register_provider(:test, source: custom_source) | ||
|
||
expect { | ||
provider_source = system.providers[:test].source | ||
expect(provider_source.class).to be < Dry::System::Provider::Source | ||
expect(provider_source.options).to be_empty | ||
}.to_not raise_error | ||
end | ||
end | ||
end |