From 26c7a15aa8e4fbd8706a1f55f94f1dfab9b2ba56 Mon Sep 17 00:00:00 2001 From: Matt Solt Date: Tue, 14 Oct 2014 17:27:10 -0400 Subject: [PATCH] prevent object representations from including properties without values (fixes #4) --- lib/kartograph/artist.rb | 6 ++++-- spec/lib/kartograph/artist_spec.rb | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/kartograph/artist.rb b/lib/kartograph/artist.rb index 0291fb1..52a07b8 100644 --- a/lib/kartograph/artist.rb +++ b/lib/kartograph/artist.rb @@ -15,8 +15,10 @@ def draw(scope = nil) scoped_properties = scope ? properties.filter_by_scope(scope) : properties scoped_properties.each_with_object({}) do |property, mapped| raise ArgumentError, "#{object} does not respond to #{property.name}, so we can't map it" unless object.respond_to?(property.name) - mapped[property.key] = property.value_for(object, scope) + if value = property.value_for(object, scope) + mapped[property.key] = value + end end end end -end \ No newline at end of file +end diff --git a/spec/lib/kartograph/artist_spec.rb b/spec/lib/kartograph/artist_spec.rb index 967e72a..6990141 100644 --- a/spec/lib/kartograph/artist_spec.rb +++ b/spec/lib/kartograph/artist_spec.rb @@ -27,6 +27,16 @@ expect(masterpiece).to include(hello: 'world') end + it 'does not return a key if the value is not set' do + object = double('object', hello: nil) + properties << Kartograph::Property.new(:hello) + + artist = Kartograph::Artist.new(object, map) + masterpiece = artist.draw + + expect(masterpiece).not_to include(:hello) + end + it 'raises for a property that the object does not have' do object = double('object') properties << Kartograph::Property.new(:bunk) @@ -79,4 +89,4 @@ end end end -end \ No newline at end of file +end