sparkapi / spark_api

Ruby client library for communication with the Spark API
http://sparkplatform.com/docs/overview/api
Other
55 stars 35 forks source link

Assigning a model attribute as nil doesn't create an attribute #155

Open mjobrien opened 5 years ago

mjobrien commented 5 years ago

The protected write_attribute method of SparkApi::Models::Base checks whether the value being set for an attribute is already set to the assigned value. This causes inconsistent behavior when the attribute is new and the assigned value is nil because attributes is a hash and returns nil when a key does not exist.

l = SparkApi::Models::Listing.new
l.attributes # {}
l.City       # NoMethodError: undefined method `include?' for nil:NilClass
l.State = 'ND'
l.City = nil
l.State      # "ND"
l.City       # NoMethodError: undefined method `include?' for nil:NilClass
l.attributes # {"State"=>"ND"}
l.attributes['City'] = nil
l.City       # nil
l.attributes # {"State"=>"ND", "City"=>nil}
l.changes    #{"State"=>[nil, "ND"]}

This can be partially worked around by writing to the attributes hash directly. However, as illustrated above, that isn't a full-featured approach with respect to the SparkApi::Models::Dirty module at least.

On second thought, l.changes isn't a great example of the workaround not being full-featured. However, the general point remains that this is inconsistent behavior.

bhornseth commented 5 years ago

This seems like it'd be a good candidate for the 2.0 milestone, yeah?