Class: TomlRB::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/toml-rb/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(dotted_keys) ⇒ Table



3
4
5
# File 'lib/toml-rb/table.rb', line 3

def initialize(dotted_keys)
  @dotted_keys = dotted_keys
end

Instance Method Details

#accept_visitor(parser) ⇒ Object



28
29
30
# File 'lib/toml-rb/table.rb', line 28

def accept_visitor(parser)
  parser.visit_table self
end

#full_keyObject



32
33
34
# File 'lib/toml-rb/table.rb', line 32

def full_key
  @dotted_keys.join(".")
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/toml-rb/table.rb', line 7

def navigate_keys(hash, visited_keys, symbolize_keys = false)
  ensure_key_not_defined(visited_keys)
  current = hash
  keys = symbolize_keys ? @dotted_keys.map(&:to_sym) : @dotted_keys
  keys.each_with_index do |key, index|
    current[key] = {} unless current.key?(key)
    element = current[key]

    # If this is the final key and it's already an array (from [[key]]), that's invalid
    is_final_key = (index == keys.length - 1)
    if is_final_key && element.is_a?(Array)
      fail ValueOverwriteError.new(key)
    end

    current = element.is_a?(Array) ? element.last : element
    # check that key has not been defined before as a scalar value
    fail ValueOverwriteError.new(key) unless current.is_a?(Hash)
  end
  current
end