lib/dry/system/container.rb in dry-system-0.8.1 vs lib/dry/system/container.rb in dry-system-0.9.0

- old
+ new

@@ -13,10 +13,11 @@ require 'dry/system/auto_registrar' require 'dry/system/manual_registrar' require 'dry/system/importer' require 'dry/system/component' require 'dry/system/constants' +require 'dry/system/plugins' module Dry module System # Abstract container class to inherit from # @@ -64,10 +65,11 @@ # # @api public class Container extend Dry::Configurable extend Dry::Container::Mixin + extend Dry::System::Plugins setting :name setting :default_namespace setting(:root, Pathname.pwd.freeze) { |path| Pathname(path) } setting :system_dir, 'system'.freeze @@ -98,10 +100,11 @@ # # @api public def configure(&block) super(&block) load_paths!(config.system_dir) + hooks[:configure].each { |hook| instance_eval(&hook) } self end # Registers another container for import # @@ -439,23 +442,23 @@ # Requires one or more files relative to the container's root # # @example # # sinle file - # MyApp.require('lib/core') + # MyApp.require_from_root('lib/core') # # # glob - # MyApp.require('lib/**/*') + # MyApp.require_from_root('lib/**/*') # # @param *paths [Array<String>] one or more paths, supports globs too # # @api public - def require(*paths) + def require_from_root(*paths) paths.flat_map { |path| path.to_s.include?('*') ? Dir[root.join(path)] : root.join(path) }.each { |path| - Kernel.require path.to_s + require path.to_s } end # Returns container's root path # @@ -533,11 +536,11 @@ unless component.file_exists?(load_paths) raise FileNotFoundError, component end - Kernel.require(component.path) + require component.path yield end # @api private @@ -561,14 +564,37 @@ end self end + # @api private + def after(event, &block) + hooks[event] << block + end + + # @api private + def hooks + @__hooks__ ||= Hash.new { |h, k| h[k] = [] } + end + + # @api private + def inherited(klass) + new_hooks = Container.hooks.dup + + hooks.each do |event, blocks| + new_hooks[event].concat(blocks) + new_hooks[event].concat(klass.hooks[event]) + end + + klass.instance_variable_set(:@__hooks__, new_hooks) + super + end + private # @api private def load_local_component(component, default_namespace_fallback = false) - if component.bootable?(booter.path) || component.file_exists?(load_paths) + if booter.bootable?(component) || component.file_exists?(load_paths) booter.boot_dependency(component) unless finalized? require_component(component) do register(component.identifier) { component.instance } end