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, options = {}) ⇒ Parser

Returns a new instance of Parser.



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

def initialize(content, options = {})
  @hash = {}
  @visited_keys = []
  @current = @hash
  @symbolize_keys = options[: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_keygroup(keygroup) ⇒ Object



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

def visit_keygroup(keygroup)
  @current = keygroup.navigate_keys @hash, @visited_keys, @symbolize_keys
end

#visit_keyvalue(keyvalue) ⇒ Object



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

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

#visit_table_array(table_array) ⇒ Object

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



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

def visit_table_array(table_array)
  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