Class: TomlRB::TableArray

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

Instance Method Summary collapse

Constructor Details

#initialize(nested_keys) ⇒ TableArray

Returns a new instance of TableArray.



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

def initialize(nested_keys)
  @nested_keys = nested_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
  @nested_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)
  last_key = @nested_keys.pop
  last_key = last_key.to_sym if symbolize_keys

  # Go over the parent keys
  @nested_keys.each do |key|
    key = symbolize_keys ? key.to_sym : key
    hash[key] = {} unless hash[key]

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

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

  hash[last_key].last
end