-
-
Notifications
You must be signed in to change notification settings - Fork 69
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.
- Loading branch information
Showing
4 changed files
with
109 additions
and
13 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
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Providers / Custom provider superclass" do | ||
let(:custom_superclass) do | ||
module Test | ||
class CustomSource < Dry::System::Provider::Source | ||
def custom_api = :hello | ||
end | ||
end | ||
|
||
Test::CustomSource | ||
end | ||
|
||
let(:source) do | ||
module Test | ||
class LoggerProvider < Dry::System::Provider::Source | ||
def prepare | ||
require "logger" | ||
end | ||
|
||
def start | ||
register(:logger, Logger.new(STDOUT)) | ||
end | ||
end | ||
end | ||
|
||
Test::LoggerProvider | ||
end | ||
|
||
subject(:system) do | ||
module Test | ||
class Container < Dry::System::Container | ||
configure finalize_config: false do |config| | ||
config.root = SPEC_ROOT.join("fixtures/app").realpath | ||
end | ||
end | ||
end | ||
|
||
Test::Container | ||
end | ||
|
||
before do | ||
Dry::System.register_provider_source(:logger, group: :custom_group, source: source) | ||
end | ||
|
||
describe "superclass override" do | ||
subject(:provider_source) { system.providers[:test].source } | ||
|
||
specify "explicit superclass" do | ||
system.register_provider(:test, superclass: custom_superclass) {} | ||
|
||
expect(provider_source.class).to be < custom_superclass | ||
expect(provider_source.class.name).to eq "Test::CustomSource[test]" | ||
expect(provider_source.custom_api).to eq :hello | ||
end | ||
|
||
specify "configured superclass" do | ||
system.config.provider_superclass = custom_superclass | ||
|
||
system.register_provider(:test) {} | ||
|
||
expect(provider_source.class).to be < custom_superclass | ||
expect(provider_source.class.name).to eq "Test::CustomSource[test]" | ||
expect(provider_source.custom_api).to eq :hello | ||
end | ||
end | ||
|
||
describe "invalid arguments" do | ||
it "doesn't accept a superclass for a concrete source" do | ||
source = Class.new(Dry::System::Provider::Source) | ||
|
||
expect { | ||
system.register_provider(:invalid, source: source, superclass: custom_superclass) | ||
}.to raise_error(ArgumentError, /superclass/) | ||
end | ||
|
||
it "doesn't accept a superclass for an external provider" do | ||
expect { | ||
system.register_provider(:test, from: :custom_group, source: :logger, superclass: custom_superclass) | ||
}.to raise_error(ArgumentError, /superclass/) | ||
end | ||
end | ||
end |