Module: TomlRB

Defined in:
lib/toml-rb.rb,
lib/toml-rb/array.rb,
lib/toml-rb/dumper.rb,
lib/toml-rb/errors.rb,
lib/toml-rb/parser.rb,
lib/toml-rb/string.rb,
lib/toml-rb/keygroup.rb,
lib/toml-rb/keyvalue.rb,
lib/toml-rb/table_array.rb,
lib/toml-rb/inline_table.rb

Defined Under Namespace

Modules: ArrayParser, BasicString, InlineTableArrayParser, InlineTableParser, KeygroupParser, KeyvalueParser, LiteralString, MultilineLiteral, MultilineString, TableArrayParser Classes: Dumper, InlineTable, InlineTableArray, Keygroup, Keyvalue, Parser, TableArray, ValueOverwriteError

Constant Summary collapse

Error =

Parent class for all TomlRB errors

Class.new(StandardError)
ParseError =

Error related to parsing.

Class.new(Error)

Class Method Summary collapse

Class Method Details

.dump(hash) ⇒ Object

Public: Returns a TomlRB string from a Ruby Hash.

hash - Ruby Hash to be dumped into TomlRB

Examples

TomlRB.dump(title: 'TomlRB dump')
# => "simple = true\n"

hash = {
  "title"=>"wow!",
  "awesome"=> {
    "you"=>true,
    "others"=>false
  }
}

TomlRB.dump(hash)
# => "title = \"wow!\"\n[awesome]\nothers = false\nyou = true\n"

Returns a TomlRB string representing the hash.



97
98
99
# File 'lib/toml-rb.rb', line 97

def self.dump(hash)
  Dumper.new(hash).toml_str
end

.load_file(path, options = {}) ⇒ Object

Public: Returns a hash from a TomlRB file.

path - TomlRB File path options - The Hash options used to refine the parser (default: {}):

:symbolize_keys - true|false (optional).

Examples

TomlRB.load_file('/tmp/simple.toml')
# => {"group"=>{}}

TomlRB.load_file('/tmp/simple.toml', symbolize_keys: true)
# => {group: {}}

Returns a Ruby hash representation of the content. Raises ValueOverwriteError if a key is overwritten. Raises ParseError if the content has invalid TomlRB. Raises Errno::ENOENT if the file cannot be found. Raises Errno::EACCES if the file cannot be accessed.



70
71
72
# File 'lib/toml-rb.rb', line 70

def self.load_file(path, options = {})
  TomlRB.parse(File.read(path), options)
end

.parse(content, options = {}) ⇒ Object

Public: Returns a hash from TomlRB content.

content - TomlRB string to be parsed. options - The Hash options used to refine the parser (default: {}):

:symbolize_keys - true|false (optional).

Examples

TomlRB.parse('[group]')
# => {"group"=>{}}

TomlRB.parse('title = "TomlRB parser"')
# => {"title"=>"TomlRB parser"}

TomlRB.parse('[group]', symbolize_keys: true)
# => {group: {}}

TomlRB.parse('title = "TomlRB parser"', symbolize_keys: true)
# => {title: "TomlRB parser"}

Returns a Ruby hash representation of the content according to TomlRB spec. Raises ValueOverwriteError if a key is overwritten. Raises ParseError if the content has invalid TomlRB.



45
46
47
# File 'lib/toml-rb.rb', line 45

def self.parse(content, options = {})
  Parser.new(content, options).hash
end