Hello,
I have a hard time understanding how I can change container settings before the container finalization.
Let me illustrate my point with a short single file code snippet:
# frozen_string_literal: true
require "dry/system"
class Container < Dry::System::Container
require "dry/system/provider_sources"
configure do |config|
use :env
config.root = "./"
register_provider(:settings, from: :dry_system) do
settings do
setting :name, default: "Ruby"
end
end
register :my_component do
MyComponent.new
end
end
end
Import = Container.injector
class MyComponent
include Import[:settings]
def say_hi
puts "Hello, #{settings.name}"
end
end
# I want to change the Container[:settings].name setting here.
Container.finalize!
Container["my_component"].say_hi
So here we have a setting Container[:settings].name
with a default value Ruby
, and the component MyComponent
that uses that setting. Now, what should I do in order to change the setting’s value before the container finalization? I have tried to re-configure container, but got the Cannot modify frozen config (Dry::Configurable::FrozenConfigError) error.
The other minor problem is that my code fails without these two lines:
use :env
and
require "dry/system/provider_sources"
The documentation page does not have these, so I am a bit confused. Is this a problem with my environment or it is a lack of details in the documentation?
Any help would be highly appreciated.