Skip to content

Commit

Permalink
prevent object representations from including properties without valu…
Browse files Browse the repository at this point in the history
…es (fixes digitalocean#4)
  • Loading branch information
activefx committed Oct 14, 2014
1 parent 0cf68d0 commit 26c7a15
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/kartograph/artist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
12 changes: 11 additions & 1 deletion spec/lib/kartograph/artist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -79,4 +89,4 @@
end
end
end
end
end

0 comments on commit 26c7a15

Please sign in to comment.