class Dry::Configurable::Config
Config
exposes setting values through a convenient API
@api public
Attributes
_resolved[R]
@api private
_settings[R]
@api private
Public Class Methods
new(settings)
click to toggle source
@api private
# File lib/dry/configurable/config.rb, line 25 def initialize(settings) @_settings = settings.dup @_resolved = Concurrent::Map.new end
Public Instance Methods
[](name)
click to toggle source
Get config value by a key
@param [String,Symbol] name
@return Config
value
# File lib/dry/configurable/config.rb, line 35 def [](name) name = name.to_sym raise ArgumentError, "+#{name}+ is not a setting name" unless _settings.key?(name) _settings[name].value end
[]=(name, value)
click to toggle source
Set config value. Note that finalized configs cannot be changed.
@param [String,Symbol] name @param [Object] value
# File lib/dry/configurable/config.rb, line 47 def []=(name, value) public_send(:"#{name}=", value) end
finalize!()
click to toggle source
@api private
# File lib/dry/configurable/config.rb, line 85 def finalize! _settings.freeze freeze end
pristine()
click to toggle source
@api private
# File lib/dry/configurable/config.rb, line 91 def pristine self.class.new(_settings.pristine) end
respond_to_missing?(meth, include_private = false)
click to toggle source
@api private
Calls superclass method
# File lib/dry/configurable/config.rb, line 96 def respond_to_missing?(meth, include_private = false) super || _settings.key?(resolve(meth)) end
update(values)
click to toggle source
Update config with new values
@param values [Hash] A hash with new values
@return [Config]
@api public
# File lib/dry/configurable/config.rb, line 58 def update(values) values.each do |key, value| case value when Hash self[key].update(value) else self[key] = value end end self end
values()
click to toggle source
Dump config into a hash
@return [Hash]
@api public
# File lib/dry/configurable/config.rb, line 75 def values _settings .map { |setting| [setting.name, setting.value] } .map { |key, value| [key, value.is_a?(self.class) ? value.to_h : value] } .to_h end
Private Instance Methods
initialize_copy(source)
click to toggle source
@api private
Calls superclass method
# File lib/dry/configurable/config.rb, line 123 def initialize_copy(source) super @_settings = source._settings.dup end
method_missing(meth, *args)
click to toggle source
@api private
Calls superclass method
# File lib/dry/configurable/config.rb, line 103 def method_missing(meth, *args) setting = _settings[resolve(meth)] super unless setting if setting.writer?(meth) raise FrozenConfig, 'Cannot modify frozen config' if frozen? _settings << setting.with(input: args[0]) else setting.value end end
resolve(meth)
click to toggle source
@api private
# File lib/dry/configurable/config.rb, line 118 def resolve(meth) _resolved.fetch(meth) { _resolved[meth] = meth.to_s.tr('=', '').to_sym } end