11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/toml/monkey_patch.rb', line 11
def to_toml(path = "")
return "" if self.empty?
tables = {}
values = {}
self.keys.sort.each do |key|
val = self[key]
if val.kind_of?(NilClass)
next
elsif val.toml_table? || val.toml_table_array?
tables[key] = val
else
values[key] = val
end
end
toml = ""
values.each do |key, val|
toml << "#{key} = #{val.to_toml(key)}\n"
end
tables.each do |key, val|
key = "#{path}.#{key}" unless path.empty?
toml_val = val.to_toml(key)
if toml_val.empty?
toml << "\n[#{key}]\n"
else
if val.toml_table?
non_table_vals = val.values.reject do |v|
v.toml_table? || v.toml_table_array?
end
if non_table_vals.length > 0
toml << "\n[#{key}]\n"
end
end
toml << toml_val
end
end
toml
end
|