Skip to content

Commit

Permalink
Fix #11: prevent serialization of empty collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Feb 24, 2018
1 parent 13f0cff commit 9ffa72d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/cff/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ def version=(version)

def to_yaml # :nodoc:
fields = @fields.dup
fields['authors'] = array_field_to_yaml(@authors)
fields['contact'] = array_field_to_yaml(@contact)
fields['keywords'] = @keywords.map { |k| k.to_s }
fields['authors'] = array_field_to_yaml(@authors) unless @authors.empty?
fields['contact'] = array_field_to_yaml(@contact) unless @contact.empty?
fields['keywords'] = @keywords.map { |k| k.to_s } unless @keywords.empty?

YAML.dump fields, :line_width => -1, :indentation => 2
end
Expand Down
9 changes: 9 additions & 0 deletions test/cff_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,13 @@ def test_keywords_set_and_output_correctly
assert_equal m.keywords.length, l
assert y.include? "keywords:\n- one\n- two\n- '3'\n"
end

def test_empty_collections_are_not_output
m = ::CFF::Model.new('title')
y = m.to_yaml

refute y.include? "authors: []\n"
refute y.include? "contact: []\n"
refute y.include? "keywords: []\n"
end
end

0 comments on commit 9ffa72d

Please sign in to comment.