-
-
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 specify provider options
The one provider option currently supported is `namespace:`. Allowing a provider source to specify e.g. `provider_options: {namespace: true}` is useful if the source will register multiple components at different keys. In such a case, the provider will typically want to have a `namespace: true` provider option. Specifying this option at the time of provider source registration then makes for a simpler and less error-prone experience when the user eventually registers a provider using that source. The user now no longer needs to remember to provide their own explicit `namespace: true`.
- Loading branch information
Showing
4 changed files
with
86 additions
and
20 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
39 changes: 39 additions & 0 deletions
39
spec/integration/container/providers/provider_sources/provider_options_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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Providers / Provider sources / Provider options" do | ||
let(:container) { Class.new(Dry::System::Container) } | ||
|
||
specify "provider_options registered with provider sources are used when creating corresponding providers" do | ||
Dry::System.register_provider_source(:db, group: :my_framework, provider_options: {namespace: true}) do | ||
start do | ||
register "config", "db_config_here" | ||
end | ||
end | ||
|
||
# Note no `namespace:` option when registering provider | ||
container.register_provider :db, from: :my_framework | ||
|
||
# Also works when using a different name for the provider | ||
container.register_provider :my_db, from: :my_framework, source: :db | ||
|
||
container.start :db | ||
container.start :my_db | ||
|
||
expect(container["db.config"]).to eq "db_config_here" | ||
expect(container["my_db.config"]).to eq "db_config_here" | ||
end | ||
|
||
specify "provider source provider_options can be overridden" do | ||
Dry::System.register_provider_source(:db, group: :my_framework, provider_options: {namespace: true}) do | ||
start do | ||
register "config", "db_config_here" | ||
end | ||
end | ||
|
||
container.register_provider :db, from: :my_framework, namespace: "custom_db" | ||
|
||
container.start :db | ||
|
||
expect(container["custom_db.config"]).to eq "db_config_here" | ||
end | ||
end |