Class: Dry::Configurable::Config
- Inherits:
-
Object
- Object
- Dry::Configurable::Config
- Defined in:
- lib/dry/configurable/config.rb,
lib/dry/configurable/config/value.rb
Defined Under Namespace
Classes: Value
Constant Summary collapse
- DEFAULT_PROCESSOR =
->(v) { v }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #clone ⇒ Object
- #dup ⇒ Object
-
#initialize(settings) ⇒ Config
constructor
A new instance of Config.
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize(settings) ⇒ Config
Returns a new instance of Config.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dry/configurable/config.rb', line 23 def initialize(settings) @config = ::Concurrent::Hash.new settings.each do |setting| if setting.none? @config[setting.name] = nil else public_send("#{setting.name}=", setting.value) end end end |
Class Method Details
.create(settings) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dry/configurable/config.rb', line 7 def self.create(settings) klass = ::Class.new(self) settings.each do |setting| klass.__send__(:define_method, setting.name) do @config[setting.name] end klass.__send__(:define_method, "#{setting.name}=") do |value| @config[setting.name] = setting.processor.call(value) end end klass.new(settings) end |
Instance Method Details
#[](name) ⇒ Object
60 61 62 63 |
# File 'lib/dry/configurable/config.rb', line 60 def [](name) raise_unknown_setting_error(name) unless setting?(name) public_send(name) end |
#[]=(name, value) ⇒ Object
65 66 67 68 |
# File 'lib/dry/configurable/config.rb', line 65 def []=(name, value) raise_unknown_setting_error(name) unless setting?(name) public_send("#{name}=", value) end |
#clone ⇒ Object
41 42 43 44 45 |
# File 'lib/dry/configurable/config.rb', line 41 def clone clone = super clone.instance_variable_set(:@config, @config.clone) clone end |
#dup ⇒ Object
35 36 37 38 39 |
# File 'lib/dry/configurable/config.rb', line 35 def dup dup = super dup.instance_variable_set(:@config, @config.dup) dup end |
#to_h ⇒ Object Also known as: to_hash
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dry/configurable/config.rb', line 47 def to_h @config.each_with_object({}) do |tuple, hash| key, value = tuple if value.kind_of?(::Dry::Configurable::Config) hash[key] = value.to_h else hash[key] = value end end end |