lib/dry/system/container.rb in dry-system-0.24.0 vs lib/dry/system/container.rb in dry-system-0.25.0

- old
+ new

@@ -1,12 +1,12 @@ # frozen_string_literal: true require "pathname" -require "dry-auto_inject" -require "dry-configurable" -require "dry-container" +require "dry/configurable" +require "dry/auto_inject" +require "dry/container" require "dry/core/deprecations" require "dry/inflector" require "dry/system/auto_registrar" require "dry/system/component" @@ -68,16 +68,15 @@ # add_dirs_to_load_paths!('lib') # end # # @api public class Container - extend Dry::Configurable extend Dry::Container::Mixin extend Dry::System::Plugins setting :name - setting :root, default: Pathname.pwd.freeze, constructor: -> path { Pathname(path) } + setting :root, default: Pathname.pwd.freeze, constructor: ->(path) { Pathname(path) } setting :provider_dirs, default: ["system/providers"] setting :bootable_dirs # Deprecated for provider_dirs, see .provider_paths below setting :registrations_dir, default: "system/registrations" setting :component_dirs, default: Config::ComponentDirs.new, cloneable: true setting :exports, reader: true @@ -191,11 +190,11 @@ # end # # @param other [Hash, Dry::Container::Namespace] # # @api public - def import(keys: nil, from: nil, as: nil, **deprecated_import_hash) + def import(keys: nil, from: Undefined, as: Undefined, **deprecated_import_hash) if deprecated_import_hash.any? Dry::Core::Deprecations.announce( "Dry::System::Container.import with {namespace => container} hash", "Use Dry::System::Container.import(from: container, as: namespace) instead", tag: "dry-system", @@ -204,11 +203,11 @@ deprecated_import_hash.each do |namespace, container| importer.register(container: container, namespace: namespace) end return self - elsif from.nil? || as.nil? + elsif from == Undefined || as == Undefined # These keyword arguments can become properly required in the params list once # we remove the deprecation shim above raise ArgumentError, "required keyword arguments: :from, :as" end @@ -374,14 +373,14 @@ configured! hooks[:before_finalize].each { |hook| instance_eval(&hook) } yield(self) if block - importer.finalize! providers.finalize! - manifest_registrar.finalize! auto_registrar.finalize! + manifest_registrar.finalize! + importer.finalize! @__finalized__ = true self.freeze if freeze hooks[:after_finalize].each { |hook| instance_eval(&hook) } @@ -657,10 +656,11 @@ super end protected + # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity # @api private def load_component(key) return self if registered?(key) if (provider = providers.find_and_load_provider(key)) @@ -676,31 +676,32 @@ if component.loadable? load_local_component(component) elsif manifest_registrar.file_exists?(component) manifest_registrar.(component) elsif importer.namespace?(component.identifier.root_key) - load_imported_component(component.identifier) + load_imported_component(component.identifier, namespace: component.identifier.root_key) + elsif importer.namespace?(nil) + load_imported_component(component.identifier, namespace: nil) end self end + # rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity private def load_local_component(component) if component.auto_register? register(component.identifier, memoize: component.memoize?) { component.instance } end end - def load_imported_component(identifier) - import_namespace = identifier.root_key + def load_imported_component(identifier, namespace:) + return unless importer.namespace?(namespace) - return unless importer.namespace?(import_namespace) + import_key = identifier.namespaced(from: namespace, to: nil).key - import_key = identifier.namespaced(from: import_namespace, to: nil).key - - importer.import(import_namespace, keys: [import_key]) + importer.import(namespace, keys: [import_key]) end def find_component(key) # Find the first matching component from within the configured component dirs. # If no matching component is found, return a null component; this fallback is