lib/anyway/config.rb in anyway_config-2.0.6 vs lib/anyway/config.rb in anyway_config-2.1.0
- old
+ new
@@ -17,11 +17,11 @@
# Base config class
# Provides `attr_config` method to describe
# configuration parameters and set defaults
class Config
- PARAM_NAME = /^[a-z_]([\w]+)?$/
+ PARAM_NAME = /^[a-z_](\w+)?$/
# List of names that couldn't be used as config names
# (the class instance methods we use)
RESERVED_NAMES = %i[
config_name
@@ -38,16 +38,18 @@
option_parser
pretty_print
raise_validation_error
reload
resolve_config_path
+ tap
to_h
to_source_trace
write_config_attr
].freeze
class Error < StandardError; end
+
class ValidationError < Error; end
include OptparseConfig
include DynamicConfig
@@ -104,25 +106,25 @@
end
def defaults
return @defaults if instance_variable_defined?(:@defaults)
- if superclass < Anyway::Config
+ @defaults = if superclass < Anyway::Config
superclass.defaults.deep_dup
else
new_empty_config
- end => @defaults
+ end
end
def config_attributes
return @config_attributes if instance_variable_defined?(:@config_attributes)
- if superclass < Anyway::Config
+ @config_attributes = if superclass < Anyway::Config
superclass.config_attributes.dup
else
[]
- end => @config_attributes
+ end
end
def required(*names)
unless (unknown_names = (names - config_attributes)).empty?
raise ArgumentError, "Unknown config param: #{unknown_names.join(",")}"
@@ -132,35 +134,35 @@
end
def required_attributes
return @required_attributes if instance_variable_defined?(:@required_attributes)
- if superclass < Anyway::Config
+ @required_attributes = if superclass < Anyway::Config
superclass.required_attributes.dup
else
[]
- end => @required_attributes
+ end
end
def on_load(*names, &block)
- raise ArgumentError, "Either methods or block should be specified, not both" if block_given? && !names.empty?
+ raise ArgumentError, "Either methods or block should be specified, not both" if block && !names.empty?
- if block_given?
+ if block
load_callbacks << BlockCallback.new(block)
else
load_callbacks.push(*names.map { NamedCallback.new(_1) })
end
end
def load_callbacks
return @load_callbacks if instance_variable_defined?(:@load_callbacks)
- if superclass <= Anyway::Config
+ @load_callbacks = if superclass <= Anyway::Config
superclass.load_callbacks.dup
else
[]
- end => @load_callbacks
+ end
end
def config_name(val = nil)
return (@explicit_config_name = val.to_s) unless val.nil?
@@ -183,15 +185,15 @@
def env_prefix(val = nil)
return (@env_prefix = val.to_s.upcase) unless val.nil?
return @env_prefix if instance_variable_defined?(:@env_prefix)
- if superclass < Anyway::Config && superclass.explicit_config_name?
+ @env_prefix = if superclass < Anyway::Config && superclass.explicit_config_name?
superclass.env_prefix
else
config_name.upcase
- end => @env_prefix
+ end
end
def new_empty_config() = {}
private
@@ -199,12 +201,11 @@
def define_config_accessor(*names)
names.each do |name|
accessors_module.module_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{name}=(val)
__trace__&.record_value(val, \"#{name}\", **Tracing.current_trace_source)
- # DEPRECATED: instance variable set will be removed in 2.1
- @#{name} = values[:#{name}] = val
+ values[:#{name}] = val
end
def #{name}
values[:#{name}]
end
@@ -213,13 +214,13 @@
end
def accessors_module
return @accessors_module if instance_variable_defined?(:@accessors_module)
- Module.new.tap do |mod|
+ @accessors_module = Module.new.tap do |mod|
include mod
- end => @accessors_module
+ end
end
def build_config_name
unless name
raise "Please, specify config name explicitly for anonymous class " \
@@ -227,11 +228,11 @@
end
# handle two cases:
# - SomeModule::Config => "some_module"
# - SomeConfig => "some"
- unless name =~ /^(\w+)(\:\:)?Config$/
+ unless name =~ /^(\w+)(::)?Config$/
raise "Couldn't infer config name, please, specify it explicitly" \
"via `config_name :my_config`"
end
Regexp.last_match[1].tap(&:downcase!)
@@ -283,26 +284,23 @@
end
def load(overrides = nil)
base_config = self.class.defaults.deep_dup
- Tracing.capture do
+ trace = Tracing.capture do
Tracing.trace!(:defaults) { base_config }
- load_from_sources(
- base_config,
- name: config_name,
- env_prefix: env_prefix,
- config_path: resolve_config_path(config_name, env_prefix)
- )
+ config_path = resolve_config_path(config_name, env_prefix)
+ load_from_sources(base_config, name: config_name, env_prefix:, config_path:)
+
if overrides
Tracing.trace!(:load) { overrides }
- base_config.deep_merge!(overrides)
+ Utils.deep_merge!(base_config, overrides)
end
- end => trace
+ end
base_config.each do |key, val|
write_config_attr(key.to_sym, val)
end
@@ -319,10 +317,10 @@
self
end
def load_from_sources(base_config, **options)
Anyway.loaders.each do |(_id, loader)|
- base_config.deep_merge!(loader.call(**options))
+ Utils.deep_merge!(base_config, loader.call(**options))
end
base_config
end
def dig(*keys) = values.dig(*keys)