Sha256: d00628a5ba8116b1ed51aa9533dcd7106fa26f4d5267099cc41b26a86ca5dc93

Contents?: true

Size: 864 Bytes

Versions: 1

Compression:

Stored size: 864 Bytes

Contents

module TOML
  class ParseError < StandardError; end

  class Parser
    attr_reader :hash

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

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

    # Read about the Visitor pattern
    # http://en.wikipedia.org/wiki/Visitor_pattern
    def visit_table_array(table_array)
      @current = table_array.navigate_keys @hash, @symbolize_keys
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toml-rb-0.3.6 lib/toml/parser.rb