Class: TomlRB::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, symbolize_keys: false) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/toml-rb/parser.rb', line 5

def initialize(content, symbolize_keys: false)
  @hash = {}
  @visited_keys = []
  @fully_defined_keys = []
  @current = @hash
  @symbolize_keys = symbolize_keys

  begin
    parsed = TomlRB::Document.parse(content)
    parsed.matches.map(&:value).compact.each { |m| m.accept_visitor(self) }
  rescue Citrus::ParseError => e
    raise TomlRB::ParseError.new(e.message)
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

Instance Method Details

#visit_keyvalue(keyvalue) ⇒ Object



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

def visit_keyvalue(keyvalue)
  keyvalue.assign @current, @fully_defined_keys, @symbolize_keys
end

#visit_table(table) ⇒ Object



30
31
32
33
# File 'lib/toml-rb/parser.rb', line 30

def visit_table(table)
  @fully_defined_keys = []
  @current = table.navigate_keys @hash, @visited_keys, @symbolize_keys
end

#visit_table_array(table_array) ⇒ Object

Read about the Visitor pattern en.wikipedia.org/wiki/Visitor_pattern



22
23
24
25
26
27
28
# File 'lib/toml-rb/parser.rb', line 22

def visit_table_array(table_array)
  @fully_defined_keys = []
  table_array_key = table_array.full_key
  @visited_keys.reject! { |k| k.start_with? table_array_key }

  @current = table_array.navigate_keys @hash, @symbolize_keys
end