Class: TomlRB::Keyvalue
- Inherits:
-
Object
- Object
- TomlRB::Keyvalue
- Defined in:
- lib/toml-rb/keyvalue.rb
Instance Attribute Summary collapse
-
#dotted_keys ⇒ Object
readonly
Returns the value of attribute dotted_keys.
-
#symbolize_keys ⇒ Object
readonly
Returns the value of attribute symbolize_keys.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #accept_visitor(parser) ⇒ Object
- #assign(hash, fully_defined_keys, symbolize_keys = false) ⇒ Object
- #dotted_key_merge(hash, update) ⇒ Object
-
#initialize(dotted_keys, value) ⇒ Keyvalue
constructor
A new instance of Keyvalue.
Constructor Details
#initialize(dotted_keys, value) ⇒ Keyvalue
Returns a new instance of Keyvalue.
7 8 9 10 11 |
# File 'lib/toml-rb/keyvalue.rb', line 7 def initialize(dotted_keys, value) @dotted_keys = dotted_keys @value = value @symbolize_keys = false end |
Instance Attribute Details
#dotted_keys ⇒ Object (readonly)
Returns the value of attribute dotted_keys.
5 6 7 |
# File 'lib/toml-rb/keyvalue.rb', line 5 def dotted_keys @dotted_keys end |
#symbolize_keys ⇒ Object (readonly)
Returns the value of attribute symbolize_keys.
5 6 7 |
# File 'lib/toml-rb/keyvalue.rb', line 5 def symbolize_keys @symbolize_keys end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/toml-rb/keyvalue.rb', line 5 def value @value end |
Instance Method Details
#accept_visitor(parser) ⇒ Object
39 40 41 |
# File 'lib/toml-rb/keyvalue.rb', line 39 def accept_visitor(parser) parser.visit_keyvalue self end |
#assign(hash, fully_defined_keys, symbolize_keys = false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/toml-rb/keyvalue.rb', line 13 def assign(hash, fully_defined_keys, symbolize_keys = false) @symbolize_keys = symbolize_keys dotted_keys_str = @dotted_keys.join(".") keys = symbolize_keys ? @dotted_keys.map(&:to_sym) : @dotted_keys update = keys.reverse.inject(visit_value(@value)) { |k1, k2| {k2 => k1} } if @value.is_a?(InlineTable) fully_defined_keys << dotted_keys_str hash.merge!(update) { |key, _, _| fail ValueOverwriteError.new(key) } elsif fully_defined_keys.find { |k| update.dig(*k) } hash.merge!(update) { |key, _, _| fail ValueOverwriteError.new(key) } else dotted_key_merge(hash, update) end end |
#dotted_key_merge(hash, update) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/toml-rb/keyvalue.rb', line 29 def dotted_key_merge(hash, update) hash.merge!(update) { |key, old, new| if old.is_a?(Hash) && new.is_a?(Hash) dotted_key_merge(old, new) else fail ValueOverwriteError.new(key) end } end |