Class: ROM::Configurable::Config

Inherits:
Object
  • Object
show all
Includes:
ConfigMethods
Defined in:
lib/rom/support/configurable/config.rb

Overview

Config exposes setting values through a convenient API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Config

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Config.



22
23
24
25
# File 'lib/rom/support/configurable/config.rb', line 22

def initialize(settings)
  @_settings = settings
  @_resolved = Concurrent::Map.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rom/support/configurable/config.rb', line 99

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

Instance Attribute Details

#_resolvedObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/rom/support/configurable/config.rb', line 19

def _resolved
  @_resolved
end

#_settingsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/rom/support/configurable/config.rb', line 16

def _settings
  @_settings
end

Instance Method Details

#[](name) ⇒ Object

Get config value by a key

Parameters:

  • name (String, Symbol)

Returns:

  • Config value

Raises:

  • (ArgumentError)


32
33
34
35
36
37
# File 'lib/rom/support/configurable/config.rb', line 32

def [](name)
  name = name.to_sym
  raise ArgumentError, "+#{name}+ is not a setting name" unless _settings.key?(name)

  _settings[name].value
end

#[]=(name, value) ⇒ Object

Set config value. Note that finalized configs cannot be changed.

Parameters:

  • name (String, Symbol)
  • value (Object)


44
45
46
# File 'lib/rom/support/configurable/config.rb', line 44

def []=(name, value)
  public_send(:"#{name}=", value)
end

#_constructorsObject Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#each(&block) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#empty?Boolean Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

#fetchObject Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#finalize!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
# File 'lib/rom/support/configurable/config.rb', line 81

def finalize!
  _settings.freeze
  freeze
end

#freezeObject Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#inherit(other) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#inherit!(other) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#join(other, direction = :left) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#join!(other, direction = :left) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#key?(key) ⇒ Boolean Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

#keysObject Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#merge(other) ⇒ Object Originally defined in module ConfigMethods

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#pristineObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



87
88
89
# File 'lib/rom/support/configurable/config.rb', line 87

def pristine
  self.class.new(_settings.pristine)
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


92
93
94
# File 'lib/rom/support/configurable/config.rb', line 92

def respond_to_missing?(meth, include_private = false)
  super || _settings.key?(resolve(meth))
end

#update(values) ⇒ Config

Update config with new values

Parameters:

  • values (Hash)

    A hash with new values

Returns:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rom/support/configurable/config.rb', line 55

def update(values)
  values.each do |key, value|
    case value
    when Hash
      self[key].update(value)
    else
      self[key] = value
    end
  end
  self
end

#valuesHash Also known as: to_h

Dump config into a hash

Returns:

  • (Hash)


72
73
74
75
76
77
# File 'lib/rom/support/configurable/config.rb', line 72

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