class TomlRB::Dumper
Attributes
toml_str[R]
Public Class Methods
new(hash)
click to toggle source
# File lib/toml-rb/dumper.rb, line 7 def initialize(hash) @toml_str = '' visit(hash, []) end
Private Instance Methods
bare_key?(key)
click to toggle source
# File lib/toml-rb/dumper.rb, line 103 def bare_key?(key) !!key.to_s.match(/^[a-zA-Z0-9_-]*$/) end
dump_nested_pairs(nested_pairs, prefix)
click to toggle source
# File lib/toml-rb/dumper.rb, line 60 def dump_nested_pairs(nested_pairs, prefix) nested_pairs.each do |key, val| key = quote_key(key) unless bare_key? key visit val, prefix + [key], false end end
dump_pairs(simple, nested, table_array, prefix = [])
click to toggle source
# File lib/toml-rb/dumper.rb, line 46 def dump_pairs(simple, nested, table_array, prefix = []) # First add simple pairs, under the prefix dump_simple_pairs simple dump_nested_pairs nested, prefix dump_table_array_pairs table_array, prefix end
dump_simple_pairs(simple_pairs)
click to toggle source
# File lib/toml-rb/dumper.rb, line 53 def dump_simple_pairs(simple_pairs) simple_pairs.each do |key, val| key = quote_key(key) unless bare_key? key @toml_str << "#{key} = #{to_toml(val)}\n" end end
dump_table_array_pairs(table_array_pairs, prefix)
click to toggle source
# File lib/toml-rb/dumper.rb, line 68 def dump_table_array_pairs(table_array_pairs, prefix) table_array_pairs.each do |key, val| key = quote_key(key) unless bare_key? key aux_prefix = prefix + [key] val.each do |child| print_prefix aux_prefix, true args = sort_pairs(child) << aux_prefix dump_pairs(*args) end end end
print_prefix(prefix, extra_brackets = false)
click to toggle source
# File lib/toml-rb/dumper.rb, line 82 def print_prefix(prefix, extra_brackets = false) new_prefix = prefix.join('.') new_prefix = '[' + new_prefix + ']' if extra_brackets @toml_str += "[" + new_prefix + "]\n" end
quote_key(key)
click to toggle source
# File lib/toml-rb/dumper.rb, line 107 def quote_key(key) '"' + key.gsub('"', '\\"') + '"' end
sort_pairs(hash)
click to toggle source
# File lib/toml-rb/dumper.rb, line 25 def sort_pairs(hash) nested_pairs = [] simple_pairs = [] table_array_pairs = [] hash.keys.sort.each do |key| val = hash[key] element = [key, val] if val.is_a? Hash nested_pairs << element elsif val.is_a?(Array) && val.first.is_a?(Hash) table_array_pairs << element else simple_pairs << element end end [simple_pairs, nested_pairs, table_array_pairs] end
to_toml(obj)
click to toggle source
# File lib/toml-rb/dumper.rb, line 89 def to_toml(obj) if obj.is_a?(Time) || obj.is_a?(DateTime) obj.strftime('%Y-%m-%dT%H:%M:%SZ') elsif obj.is_a?(Date) obj.strftime('%Y-%m-%d') elsif obj.is_a? Regexp obj.inspect.inspect elsif obj.is_a? String obj.inspect.gsub(/\\(#[$@{])/, '\1') else obj.inspect end end
visit(hash, prefix, extra_brackets = false)
click to toggle source
# File lib/toml-rb/dumper.rb, line 15 def visit(hash, prefix, extra_brackets = false) simple_pairs, nested_pairs, table_array_pairs = sort_pairs hash if prefix.any? && (simple_pairs.any? || hash.empty?) print_prefix prefix, extra_brackets end dump_pairs simple_pairs, nested_pairs, table_array_pairs, prefix end