Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load providers when accessing via ProviderRegistrar#[] #273

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dry/system/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,14 @@ def inherited(klass)
def load_component(key)
return self if registered?(key)

if (provider = providers.find_and_load_provider(key))
if (provider = providers[key])
provider.start
return self
end

component = find_component(key)

providers.start_provider_dependency(component)
providers[component.root_key]&.start
return self if registered?(key)

if component.loadable?
Expand Down
31 changes: 8 additions & 23 deletions lib/dry/system/provider_registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,28 @@ def register_provider(name, from: nil, source: nil, if: true, **provider_options

# rubocop:enable Metrics/PerceivedComplexity

# Returns a provider for the given name, if it has already been loaded
#
# @api public
def [](provider_name)
providers[provider_name]
end
alias_method :provider, :[]

# @api private
def key?(provider_name)
providers.key?(provider_name)
end

# Returns a provider if it can be found or loaded, otherwise nil
#
# @return [Dry::System::Provider, nil]
#
# @api private
def find_and_load_provider(name)
name = name.to_sym
# @api public
def [](provider_name)
provider_name = provider_name.to_sym

if (provider = providers[name])
if (provider = providers[provider_name])
return provider
end

return if finalized?

require_provider_file(name)
require_provider_file(provider_name)

providers[name]
providers[provider_name]
end

# @api private
def start_provider_dependency(component)
if (provider = find_and_load_provider(component.root_key))
provider.start
end
def key?(provider_name)
providers.key?(provider_name)
end

# Returns all provider files within the configured provider_paths.
Expand Down
Loading