lib/dry/system/container.rb in dry-system-0.20.0 vs lib/dry/system/container.rb in dry-system-0.21.0
- old
+ new
@@ -9,17 +9,19 @@
require "dry/core/constants"
require "dry/core/deprecations"
require "dry/system"
-require "dry/system/errors"
-require "dry/system/booter"
require "dry/system/auto_registrar"
-require "dry/system/manual_registrar"
-require "dry/system/importer"
+require "dry/system/booter"
require "dry/system/component"
require "dry/system/constants"
+require "dry/system/errors"
+require "dry/system/identifier"
+require "dry/system/importer"
+require "dry/system/indirect_component"
+require "dry/system/manual_registrar"
require "dry/system/plugins"
require_relative "component_dir"
require_relative "config/component_dirs"
@@ -47,11 +49,11 @@
# you have to do is to put your classes under configured directories and
# their instances will be automatically registered within a container.
#
# Every container needs to be configured with following settings:
#
- # * `:name` - a unique container identifier
+ # * `:name` - a unique container name
# * `:root` - a system root directory (defaults to `pwd`)
#
# @example
# class MyApp < Dry::System::Container
# configure do |config|
@@ -232,11 +234,11 @@
# require 'db'
# DB.configure(ENV['DB_URL'], logger: logger)
# persistence.register(:db, DB.new)
# end
#
- # @param name [Symbol] a unique identifier for a bootable component
+ # @param name [Symbol] a unique name for a bootable component
#
# @see Lifecycle
#
# @return [self]
#
@@ -260,21 +262,19 @@
components[name] = component
end
deprecate :finalize, :boot
# @api private
- def boot_external(identifier, from:, key: nil, namespace: nil, &block)
+ def boot_external(name, from:, key: nil, namespace: nil, &block)
System.providers[from].component(
- identifier, key: key, namespace: namespace, finalize: block, container: self
+ name, key: key, namespace: namespace, finalize: block, container: self
)
end
# @api private
- def boot_local(identifier, namespace: nil, &block)
- Components::Bootable.new(
- identifier, container: self, namespace: namespace, &block
- )
+ def boot_local(name, namespace: nil, &block)
+ Components::Bootable.new(name, container: self, namespace: namespace, &block)
end
# Return if a container was finalized
#
# @return [TrueClass, FalseClass]
@@ -489,11 +489,11 @@
alias_method :registered?, :key?
#
# @!method registered?(key)
# Whether a +key+ is registered (doesn't trigger loading)
- # @param [String,Symbol] key Identifier
+ # @param [String,Symbol] key The key
# @return [Boolean]
# @api public
#
# Check if identifier is registered.
@@ -579,21 +579,21 @@
# @api private
def load_component(key)
return self if registered?(key)
- component = component(key)
-
- if component.bootable?
- booter.start(component)
+ if (bootable_component = booter.find_component(key))
+ booter.start(bootable_component)
return self
end
+ component = find_component(key)
+
booter.boot_dependency(component)
return self if registered?(key)
- if component.file_exists?
+ if component.loadable?
load_local_component(component)
elsif manual_registrar.file_exists?(component)
manual_registrar.(component)
elsif importer.key?(component.identifier.root_key)
load_imported_component(component.identifier)
@@ -613,28 +613,24 @@
def load_imported_component(identifier)
import_namespace = identifier.root_key
container = importer[import_namespace]
- container.load_component(identifier.dequalified(import_namespace).key)
+ container.load_component(identifier.namespaced(from: import_namespace, to: nil).key)
importer.(import_namespace, container)
end
- def component(identifier)
- if (bootable_component = booter.find_component(identifier))
- return bootable_component
- end
-
+ def find_component(key)
# Find the first matching component from within the configured component dirs.
- # If no matching component is found, return a plain component instance with no
- # associated file path. This fallback is important because the component may
- # still be loadable via the manual registrar or an imported container.
+ # If no matching component is found, return a null component; this fallback is
+ # important because the component may still be loadable via the manual registrar
+ # or an imported container.
component_dirs.detect { |dir|
- if (component = dir.component_for_identifier(identifier))
+ if (component = dir.component_for_key(key))
break component
end
- } || Component.new(identifier)
+ } || IndirectComponent.new(Identifier.new(key, separator: config.namespace_separator))
end
end
# Default hooks
after :configure do