lib/dry/configurable/class_methods.rb in dry-configurable-0.15.0 vs lib/dry/configurable/class_methods.rb in dry-configurable-0.16.0
- old
+ new
@@ -1,25 +1,29 @@
# frozen_string_literal: true
require "set"
-require "dry/configurable/constants"
-require "dry/configurable/dsl"
-require "dry/configurable/methods"
-require "dry/configurable/settings"
-
module Dry
module Configurable
module ClassMethods
include Methods
# @api private
def inherited(subclass)
super
- subclass.instance_variable_set("@_settings", _settings.dup)
- subclass.instance_variable_set("@_config", config.dup) if respond_to?(:config)
+ subclass.instance_variable_set(:@__config_extension__, __config_extension__)
+
+ new_settings = _settings.dup
+ subclass.instance_variable_set(:@_settings, new_settings)
+
+ # Only classes **extending** Dry::Configurable have class-level config. When
+ # Dry::Configurable is **included**, the class-level config method is undefined because it
+ # resides at the instance-level instead (see `Configurable.included`).
+ if respond_to?(:config)
+ subclass.instance_variable_set(:@config, config.dup_for_settings(new_settings))
+ end
end
# Add a setting to the configuration
#
# @param [Mixed] name
@@ -50,11 +54,11 @@
#
# @return [Set<Symbol>]
#
# @api public
def settings
- @settings ||= Set[*_settings.map(&:name)]
+ Set[*_settings.map(&:name)]
end
# Return declared settings
#
# @return [Settings]
@@ -68,20 +72,28 @@
#
# @return [Config]
#
# @api public
def config
- # The _settings provided to the Config remain shared between the class and the
- # Config. This allows settings defined _after_ accessing the config to become
- # available in subsequent accesses to the config. The config is duped when
- # subclassing to ensure it remains distinct between subclasses and parent classes
- # (see `.inherited` above).
- @config ||= Config.new(_settings)
+ @config ||= __config_build__
end
# @api private
+ def __config_build__(settings = _settings)
+ __config_extension__.config_class.new(settings)
+ end
+
+ # @api private
+ def __config_extension__
+ @__config_extension__
+ end
+
+ # @api private
def __config_dsl__
- @__config_dsl__ ||= DSL.new
+ @__config_dsl__ ||= DSL.new(
+ config_class: __config_extension__.config_class,
+ default_undefined: __config_extension__.default_undefined
+ )
end
# @api private
def __config_reader__
@__config_reader__ ||=