Skip to content

Commit

Permalink
Load providers when accessing via ProviderRegistrar#[] (#273)
Browse files Browse the repository at this point in the history
Move the logic from `ProviderRegistrar#find_and_load_provider` straght into `#[]` for a more convenient and reliable experience when interacting with providers.

Having `#[]` either return a provider or nil without attempting to _load_ the provider was a potential source of confusion. Now it can work as the one and only interface for fetching a provider.

Remove the `#provider` alias for `#[]` since this was private and unused (and unnecessary).

Remove `#start_provider_dependency` (which was the only thing in `ProviderRegistrar` dealing with components instead of providers and instead invoke its internal logic (a one-liner, anyway!) directly in `Container#load_component`.
  • Loading branch information
timriley authored Jun 13, 2024
1 parent c510ede commit f826656
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
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

0 comments on commit f826656

Please sign in to comment.