lib/dry/system/config/component_dirs.rb in dry-system-0.20.0 vs lib/dry/system/config/component_dirs.rb in dry-system-0.21.0
- old
+ new
@@ -1,22 +1,16 @@
+# frozen_string_literal: true
+
require "concurrent/map"
-require "dry/configurable"
require "dry/system/constants"
require "dry/system/errors"
require_relative "component_dir"
module Dry
module System
module Config
class ComponentDirs
- include Dry::Configurable
-
- # Settings from ComponentDir are configured here as defaults for all added dirs
- ComponentDir._settings.each do |setting|
- _settings << setting.dup
- end
-
# @!group Settings
# @!method auto_register=(value)
#
# Sets a default `auto_register` for all added component dirs
@@ -41,23 +35,10 @@
#
# Returns the configured default `add_to_load_path`
#
# @see add_to_load_path=
- # @!method default_namespace=(value)
- #
- # Sets a default `default_namespace` value for all added component dirs
- #
- # @see ComponentDir.default_namespace
- # @see default_namespace
- #
- # @!method default_namespace
- #
- # Returns the configured default `default_namespace`
- #
- # @see default_namespace=
-
# @!method loader=(value)
#
# Sets a default `loader` value for all added component dirs
#
# @see ComponentDir.loader
@@ -80,21 +61,38 @@
#
# Returns the configured default `memoize`
#
# @see memoize=
+ # @!method namespaces
+ #
+ # Returns the default configured namespaces for all added component dirs
+ #
+ # Allows namespaces to added on the returned object via {Namespaces#add}.
+ #
+ # @see Namespaces#add
+ #
+ # @return [Namespaces] the namespaces
+
# @!endgroup
+ # A ComponentDir for configuring the default values to apply to all added
+ # component dirs
+ #
# @api private
+ attr_reader :defaults
+
+ # @api private
def initialize
@dirs = Concurrent::Map.new
+ @defaults = ComponentDir.new(nil)
end
# @api private
def initialize_copy(source)
- super
@dirs = source.dirs.dup
+ @defaults = source.defaults.dup
end
# Adds and configures a component dir
#
# @param path [String] the path for the component dir, relative to the configured
@@ -112,12 +110,12 @@
# @see ComponentDir
def add(path)
raise ComponentDirAlreadyAddedError, path if dirs.key?(path)
dirs[path] = ComponentDir.new(path).tap do |dir|
- apply_defaults_to_dir(dir)
yield dir if block_given?
+ apply_defaults_to_dir(dir)
end
end
# Returns the added component dirs, with default settings applied
#
@@ -141,43 +139,32 @@
to_a.each(&block)
end
private
- # Apply default settings to a component dir. This is run every time the dirs are
+ # Applies default settings to a component dir. This is run every time the dirs are
# accessed to ensure defaults are applied regardless of when new component dirs
# are added. This method must be idempotent.
#
# @return [void]
def apply_defaults_to_dir(dir)
- dir.config.values.each do |key, _value|
- if configured?(key) && !dir.configured?(key)
- dir.public_send(:"#{key}=", public_send(key))
+ defaults.config.values.each do |key, _|
+ if defaults.configured?(key) && !dir.configured?(key)
+ dir.public_send(:"#{key}=", defaults.public_send(key).dup)
end
end
end
- # Returns true if a setting has been explicitly configured and is not returning
- # just a default value.
- #
- # This is used to determine which settings should be applied to added component
- # dirs as additional defaults.
- #
- # @api private
- def configured?(key)
- config._settings[key].input_defined?
- end
-
def method_missing(name, *args, &block)
- if config.respond_to?(name)
- config.public_send(name, *args, &block)
+ if defaults.respond_to?(name)
+ defaults.public_send(name, *args, &block)
else
super
end
end
def respond_to_missing?(name, include_all = false)
- config.respond_to?(name) || super
+ defaults.respond_to?(name) || super
end
end
end
end
end