lib/dry/system/auto_registrar.rb in dry-system-0.6.0 vs lib/dry/system/auto_registrar.rb in dry-system-0.7.0
- old
+ new
@@ -1,14 +1,16 @@
require 'dry/system/constants'
+require 'dry/system/magic_comments_parser'
+require 'dry/system/auto_registrar/configuration'
module Dry
module System
# Default auto-registration implementation
#
# This is currently configured by default for every System::Container.
# Auto-registrar objects are responsible for loading files from configured
- # auto-register paths and registering components automatically withing the
+ # auto-register paths and registering components automatically within the
# container.
#
# @api private
class AutoRegistrar
attr_reader :container
@@ -24,42 +26,50 @@
def finalize!
Array(config.auto_register).each { |dir| call(dir) }
end
# @api private
- def call(dir, &block)
+ def call(dir)
+ registration_config = Configuration.new
+ yield(registration_config) if block_given?
components(dir).each do |component|
+ next if !component.auto_register? || registration_config.exclude.(component)
+
container.require_component(component) do
- if block
- register(component.identifier, yield(component))
- else
- register(component.identifier) { component.instance }
- end
+ register(component.identifier) { registration_config.instance.(component) }
end
end
end
private
# @api private
def components(dir)
- paths(dir).
- map { |path| component(path) }.
+ files(dir).
+ map { |file_name| [file_name, file_options(file_name)] }.
+ map { |(file_name, options)| component(relative_path(dir, file_name), **options) }.
reject { |component| key?(component.identifier) }
end
# @api private
- def paths(dir)
+ def files(dir)
+ Dir["#{root}/#{dir}/**/*.rb"]
+ end
+
+ # @api private
+ def relative_path(dir, file_path)
dir_root = root.join(dir.to_s.split('/')[0])
+ file_path.to_s.sub("#{dir_root}/", '').sub(RB_EXT, EMPTY_STRING)
+ end
- Dir["#{root}/#{dir}/**/*.rb"].map { |path|
- path.to_s.sub("#{dir_root}/", '').sub(RB_EXT, EMPTY_STRING)
- }
+ # @api private
+ def file_options(file_name)
+ MagicCommentsParser.(file_name)
end
# @api private
- def component(path)
- container.component(path)
+ def component(path, options)
+ container.component(path, options)
end
# @api private
def root
container.root