Class: ROM::Configurable::Config
- Inherits:
-
Object
- Object
- ROM::Configurable::Config
- Includes:
- ConfigMethods
- Defined in:
- lib/rom/support/configurable/config.rb
Overview
Config exposes setting values through a convenient API
Instance Attribute Summary collapse
- #_resolved ⇒ Object readonly private
- #_settings ⇒ Object readonly private
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get config value by a key.
-
#[]=(name, value) ⇒ Object
Set config value.
- #_constructors ⇒ Object included from ConfigMethods private
- #each(&block) ⇒ Object included from ConfigMethods private
- #empty? ⇒ Boolean included from ConfigMethods private
- #fetch ⇒ Object included from ConfigMethods private
- #finalize! ⇒ Object private
- #freeze ⇒ Object included from ConfigMethods private
- #inherit(other) ⇒ Object included from ConfigMethods private
- #inherit!(other) ⇒ Object included from ConfigMethods private
-
#initialize(settings) ⇒ Config
constructor
private
A new instance of Config.
- #join(other, direction = :left) ⇒ Object included from ConfigMethods private
- #join!(other, direction = :left) ⇒ Object included from ConfigMethods private
- #key?(key) ⇒ Boolean included from ConfigMethods private
- #keys ⇒ Object included from ConfigMethods private
- #merge(other) ⇒ Object included from ConfigMethods private
- #pristine ⇒ Object private
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean private
-
#update(values) ⇒ Config
Update config with new values.
-
#values ⇒ Hash
(also: #to_h)
Dump config into a hash.
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
#_resolved ⇒ Object (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 |
#_settings ⇒ Object (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
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.
44 45 46 |
# File 'lib/rom/support/configurable/config.rb', line 44 def []=(name, value) public_send(:"#{name}=", value) end |
#_constructors ⇒ 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.
#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.
#fetch ⇒ 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.
#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 |
#freeze ⇒ 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.
#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.
#keys ⇒ 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.
#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.
#pristine ⇒ 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.
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.
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
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 |
#values ⇒ Hash Also known as: to_h
Dump config into a 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 |