Skip to content

Commit

Permalink
Merge pull request #28 from rjocoleman/nil
Browse files Browse the repository at this point in the history
convert nil to empty string
  • Loading branch information
parkr committed Feb 17, 2014
2 parents b47f4dc + 3bf22ea commit deeb624
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/toml/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ def visit(hash, path = "")
if key.include? '.'
raise SyntaxError, "Periods are not allowed in keys (failed on key: #{key.inspect})"
end
@body += "#{key} = #{format(val)}\n"
unless val.nil?
@body += "#{key} = #{format(val)}\n"
end
end
@body += "\n" unless other_pairs.empty?

# Then deal with sub-hashes
hash_pairs.each do |pair|
key, hash = pair
visit(hash, (path.empty? ? key : [path, key].join(".")))
if hash.empty?
@body += "[#{path.empty? ? key : [path, key].join(".")}]\n"
else
visit(hash, (path.empty? ? key : [path, key].join(".")))
end
end
end#visit

Expand Down
9 changes: 7 additions & 2 deletions test/test_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ def setup
"key" => {
"group" => {
"value" => "lol"
}
},
"nil_table" => {}
},
"date" => DateTime.now
"date" => DateTime.now,
"nil" => nil
}

end
Expand All @@ -34,6 +36,9 @@ def test_generator
original_date = doc.delete "date"
parsed_date = doc_parsed.delete "date"

# removing the nil value
remove_nil = doc.delete "nil"

assert_equal doc, doc_parsed
assert_equal original_date.to_time.to_s, parsed_date.to_time.to_s
end
Expand Down

0 comments on commit deeb624

Please sign in to comment.