solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Add handling of nil values for collection attributes #275

Closed edgibbs closed 9 years ago

edgibbs commented 10 years ago

A recent upgrade from Virtus 0.5.5 to 1.0.2 exposed in issue that had changed with nil handling and collection attributes. We had code similar to this:

atttribute :email_lists, Array[Integer]

This worked fine if the value was set to nil which was a reasonable value. After upgrading to 1.0.2 the unit tests where the value was set to nil blew up with the following error:

NoMethodError:
       undefined method `each_with_object' for nil:NilClass

It looks like an explicit check was removed from the coerce() method on Collection class quite a while ago as part of a larger refactoring. This pull request fixes this issue with an explicit check. I'm not sure if this is the desired behavior for Virtus, but it makes it more usable for us.

coveralls commented 10 years ago

Coverage Status

Coverage increased (+0.11%) when pulling 9a2a38962599c82cc8622e625a48b3321e2c60f0 on edgibbs:handle_nil_for_collections into b278c928aabad8f4d8ef3866cbe985fbe82d5d04 on solnic:master.