Class: TomlRB::TableArray

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

Instance Method Summary collapse

Constructor Details

#initialize(dotted_keys) ⇒ TableArray

Returns a new instance of TableArray.



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

def initialize(dotted_keys)
  @dotted_keys = dotted_keys
end

Instance Method Details

#accept_visitor(parser) ⇒ Object



35
36
37
# File 'lib/toml-rb/table_array.rb', line 35

def accept_visitor(parser)
  parser.visit_table_array self
end

#full_keyObject



39
40
41
# File 'lib/toml-rb/table_array.rb', line 39

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
27
28
29
30
31
32
33
# File 'lib/toml-rb/table_array.rb', line 7

def navigate_keys(hash, symbolize_keys = false)
  current = hash
  keys = symbolize_keys ? @dotted_keys.map(&:to_sym) : @dotted_keys
  last_key = keys.pop

  # Go over the parent keys
  keys.each do |key|
    current[key] = {} unless current[key]

    if current[key].is_a? Array
      current[key] << {} if current[key].empty?
      current = current[key].last
    else
      current = current[key]
    end
  end

  # Define Table Array
  if current[last_key].is_a? Hash
    fail TomlRB::ParseError,
      "#{last_key} was defined as hash but is now redefined as a table!"
  end
  current[last_key] = [] unless current[last_key]
  current[last_key] << {}

  current[last_key].last
end