Class: Rubino::Config::Writer
- Inherits:
-
Object
- Object
- Rubino::Config::Writer
- Defined in:
- lib/rubino/config/writer.rb
Overview
Writes configuration changes back to the YAML file.
Instance Method Summary collapse
-
#get(key_path) ⇒ Object
Returns the value at a dot-notation key path.
-
#initialize(config_path:) ⇒ Writer
constructor
A new instance of Writer.
-
#set(key_path, value) ⇒ Object
Sets a single key (dot-notation) to a value and persists.
Constructor Details
#initialize(config_path:) ⇒ Writer
Returns a new instance of Writer.
10 11 12 |
# File 'lib/rubino/config/writer.rb', line 10 def initialize(config_path:) @config_path = config_path end |
Instance Method Details
#get(key_path) ⇒ Object
Returns the value at a dot-notation key path
35 36 37 38 39 40 41 42 43 |
# File 'lib/rubino/config/writer.rb', line 35 def get(key_path) raw = load_raw keys = key_path.split(".") # A scalar intermediate node (e.g. a String) has no #dig; treat such a # path as "not found" rather than crashing with a TypeError. raw.dig(*keys) rescue TypeError nil end |
#set(key_path, value) ⇒ Object
Sets a single key (dot-notation) to a value and persists
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubino/config/writer.rb', line 15 def set(key_path, value) raw = load_raw keys = key_path.split(".") hash = raw keys[0..-2].each_with_index do |k, i| hash[k] ||= {} hash = hash[k] next if hash.is_a?(Hash) traversed = keys[0..i].join(".") raise ConfigurationError, "cannot set '#{key_path}': '#{traversed}' is a scalar value, not a section" end hash[keys.last] = coerce_value(value) save(raw) end |