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