lib/dry/system/container.rb in dry-system-0.6.0 vs lib/dry/system/container.rb in dry-system-0.7.0

- old
+ new

@@ -6,10 +6,11 @@ require 'dry/system/errors' require 'dry/system/loader' require 'dry/system/booter' require 'dry/system/auto_registrar' +require 'dry/system/manual_registrar' require 'dry/system/importer' require 'dry/system/component' require 'dry/system/constants' module Dry @@ -65,14 +66,16 @@ setting :name setting :default_namespace setting :root, Pathname.pwd.freeze setting :system_dir, 'system'.freeze + setting :registrations_dir, 'container'.freeze setting :auto_register, [] setting :loader, Dry::System::Loader setting :booter, Dry::System::Booter setting :auto_registrar, Dry::System::AutoRegistrar + setting :manual_registrar, Dry::System::ManualRegistrar setting :importer, Dry::System::Importer class << self # Configures the container # @@ -99,39 +102,35 @@ # @example # # system/container.rb # class Core < Dry::System::Container # configure do |config| # config.root = Pathname("/path/to/app") - # config.name = :core # config.auto_register = %w(lib/apis lib/core) # end # end # # # apps/my_app/system/container.rb # require 'system/container' # # class MyApp < Dry::System::Container # configure do |config| # config.root = Pathname("/path/to/app") - # config.name = :core # config.auto_register = %w(lib/apis lib/core) # end # # import core: Core # end # - # @param other [Hash,Dry::Container::Namespace,Dry::System::Container] + # @param other [Hash,Dry::Container::Namespace] # # @api public def import(other) case other when Hash then importer.register(other) when Dry::Container::Namespace then super else - if other < System::Container - importer.register(other.config.name => other) - end + raise ArgumentError, "+other+ must be a hash of names and systems, or a Dry::Container namespace" end end # Registers finalization function for a bootable component # @@ -245,10 +244,11 @@ yield(self) if block importer.finalize! booter.finalize! + manual_registrar.finalize! auto_registrar.finalize! freeze end @@ -310,15 +310,22 @@ $LOAD_PATH.unshift(path.to_s) end self end + # @api public + def load_registrations!(name) + manual_registrar.(name) + self + end + # Auto-registers components from the provided directory # # Typically you want to configure auto_register directories, and it will # work automatically. Use this method in cases where you want to have an - # explicit way where some components are auto-registered. + # explicit way where some components are auto-registered, or if you want + # to exclude some components from been auto-registered # # @example # class MyApp < Dry::System::Container # configure do |config| # # ... @@ -326,19 +333,25 @@ # # # with a dir # auto_register!('lib/core') # # # with a dir and a custom registration block - # auto_register!('lib/core') do |component| - # # custom way of initializing a component + # auto_register!('lib/core') do |config| + # config.instance do |component| + # # custom way of initializing a component + # end + # + # config.exclude do |component| + # # return true to exclude component from auto-registration + # end # end # end # # @param [String] dir The dir name relative to the root dir # - # @yield [Component] - # @see [Component] + # @yield AutoRegistrar::Configuration + # @see AutoRegistrar::Configuration # # @return [self] # # @api public def auto_register!(dir, &block) @@ -434,21 +447,27 @@ def auto_registrar @auto_registrar ||= config.auto_registrar.new(self) end # @api private + def manual_registrar + @manual_registrar ||= config.manual_registrar.new(self) + end + + # @api private def importer @importer ||= config.importer.new(self) end # @api private - def component(key) + def component(key, **options) Component.new( key, loader: config.loader, namespace: config.default_namespace, - separator: config.namespace_separator + separator: config.namespace_separator, + **options, ) end # @api private def require_component(component) @@ -481,18 +500,20 @@ end private # @api private - def load_local_component(component, fallback = false) + def load_local_component(component, default_namespace_fallback = false) if component.bootable?(booter.path) || component.file_exists?(load_paths) booter.boot_dependency(component) unless frozen? require_component(component) do register(component.identifier) { component.instance } end - elsif !fallback + elsif !default_namespace_fallback load_local_component(component.prepend(config.default_namespace), true) + elsif manual_registrar.file_exists?(component) + manual_registrar.(component) else raise ComponentLoadError, component end end