Class: Dry::Configurable::Config

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(settings) ⇒ Config

Returns a new instance of Config.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dry/configurable/config.rb', line 24

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
22
# 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|
      raise_frozen_config if frozen?
      @config[setting.name] = setting.processor.call(value)
    end
  end

  klass.new(settings)
end

Instance Method Details

#[](name) ⇒ Object



67
68
69
70
# File 'lib/dry/configurable/config.rb', line 67

def [](name)
  raise_unknown_setting_error(name) unless setting?(name)
  public_send(name)
end

#[]=(name, value) ⇒ Object



72
73
74
75
# File 'lib/dry/configurable/config.rb', line 72

def []=(name, value)
  raise_unknown_setting_error(name) unless setting?(name)
  public_send("#{name}=", value)
end

#cloneObject



42
43
44
45
46
# File 'lib/dry/configurable/config.rb', line 42

def clone
  clone = super
  clone.instance_variable_set(:@config, @config.clone)
  clone
end

#dupObject



36
37
38
39
40
# File 'lib/dry/configurable/config.rb', line 36

def dup
  dup = super
  dup.instance_variable_set(:@config, @config.dup)
  dup
end

#finalize!Object



48
49
50
51
# File 'lib/dry/configurable/config.rb', line 48

def finalize!
  @config.freeze
  freeze
end

#to_hObject Also known as: to_hash



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dry/configurable/config.rb', line 53

def to_h
  @config.each_with_object({}) do |tuple, hash|
    key, value = tuple

    case value
    when ::Dry::Configurable::Config, ::Dry::Configurable::NestedConfig
      hash[key] = value.to_h
    else
      hash[key] = value
    end
  end
end