Module: Tomlrb

Defined in:
lib/tomlrb.rb,
lib/tomlrb/handler.rb,
lib/tomlrb/scanner.rb,
lib/tomlrb/version.rb,
lib/tomlrb/local_date.rb,
lib/tomlrb/local_time.rb,
lib/tomlrb/string_utils.rb,
lib/tomlrb/local_date_time.rb,
lib/tomlrb/generated_parser.rb

Defined Under Namespace

Classes: GeneratedParser, Handler, Key, Keys, LocalDate, LocalDateTime, LocalTime, ParseError, Parser, Scanner, StringUtils

Constant Summary collapse

VERSION =
'2.0.1'.freeze

Class Method Summary collapse

Class Method Details

.load_file(path, **options) ⇒ Hash

Reads a file content and parses it into its Ruby data structure

Parameters:

  • path (String)

    the path to the file

  • options (Hash)

    the options hash

Options Hash (**options):

  • :symbolize_keys (Boolean) — default: false

    whether to return the keys as symbols or strings

Returns:

  • (Hash)

    the Ruby data structure represented by the input



40
41
42
43
44
45
46
# File 'lib/tomlrb.rb', line 40

def self.load_file(path, **options)
  # By default Ruby sets the external encoding of an IO object to the
  # default external encoding. The default external encoding is set by
  # locale encoding or the interpreter -E option.
  tmp = File.read(path, :encoding=>'utf-8')
  Tomlrb.parse(tmp, **options)
end

.parse(string_or_io, **options) ⇒ Hash

Parses a valid TOML string into its Ruby data structure

Parameters:

  • string_or_io (String, StringIO)

    the content

  • options (Hash)

    the options hash

Options Hash (**options):

  • :symbolize_keys (Boolean) — default: false

    whether to return the keys as symbols or strings

Returns:

  • (Hash)

    the Ruby data structure represented by the input



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tomlrb.rb', line 21

def self.parse(string_or_io, **options)
  io = string_or_io.is_a?(String) ? StringIO.new(string_or_io) : string_or_io
  scanner = Scanner.new(io)
  parser = Parser.new(scanner, **options)
  begin
    handler = parser.parse
  rescue Racc::ParseError => e
    raise ParseError, e.message
  end

  handler.output
end