-
-
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.
Use target_container alias to pass to providers (#270)
In `ProviderRegistrar`, create a new `target_container` (an alias of its `container` attribute), and use this `target_container` when creating providers. This makes it possible for users of dry-system to provide a subclass of the `ProviderRegistrar` where they can customise the target_container that their providers receive. We need this so that providers in Hanami can receive their respective slice as the `target`, making it possible for code inside the provider to interact naturally with the Hanami slice itself, as opposed to its internal container (which itself knows nothing about the broader Hanami app). As part of this change, make `ProviderRegistrar` and `ProviderRegistrar#target_container` both public API (with everything else about the provider registrar still remaining private).
- Loading branch information
Showing
2 changed files
with
69 additions
and
3 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
57 changes: 57 additions & 0 deletions
57
spec/integration/container/providers/custom_provider_registrar_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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe "Providers / Custom provider registrar" do | ||
specify "Customizing the target_container for providers" do | ||
# Create a provider registrar that exposes a container _wrapper_ (i.e. something resembling a | ||
# Hanami slice) as the target_container. | ||
provider_registrar = Class.new(Dry::System::ProviderRegistrar) do | ||
def self.for_wrapper(wrapper) | ||
Class.new(self) do | ||
define_singleton_method(:new) do |container| | ||
super(container, wrapper) | ||
end | ||
end | ||
end | ||
|
||
attr_reader :wrapper | ||
|
||
def initialize(container, wrapper) | ||
super(container) | ||
@wrapper = wrapper | ||
end | ||
|
||
def target_container | ||
wrapper | ||
end | ||
end | ||
|
||
# Create the wrapper, which has an internal Dry::System::Container (configured with our custom | ||
# provider_registrar) that it then delegates to. | ||
container_wrapper = Class.new do | ||
define_singleton_method(:container) do | ||
@container ||= Class.new(Dry::System::Container).tap do |container| | ||
container.config.provider_registrar = provider_registrar.for_wrapper(self) | ||
end | ||
end | ||
|
||
def self.register_provider(...) | ||
container.register_provider(...) | ||
end | ||
|
||
def self.start(...) | ||
container.start(...) | ||
end | ||
end | ||
|
||
# Create a provider to expose its given `target` so we can make expecations about it | ||
exposed_target = nil | ||
container_wrapper.register_provider(:my_provider) do | ||
start do | ||
exposed_target = target | ||
end | ||
end | ||
container_wrapper.start(:my_provider) | ||
|
||
expect(exposed_target).to be container_wrapper | ||
end | ||
end |