solnic / virtus

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

Value objects do not have equality when using Virtus.value_object #313

Closed betamatt closed 9 years ago

betamatt commented 9 years ago

Given:

class Value
  include Virtus.value_object
  attribute :number, Integer
end

We expect: Value.new(:number => 7) == Value.new(:number => 7)

This behavior works as expected when using the deprecated include Virtus::ValueObject but is not preserved with the newer include. The equality behavior seems to be provided by ValueObject::ClassMethods which does not get extended when a value object class is constructed using the builder.

I took a quick crack at correcting the issue but I'm pretty sure I'm going down the wrong path. Including the class methods fixes equality but seems to introduce conflicting ideas about how to set options on attributes. Can you provide some guidance?

solnic commented 9 years ago

You need to wrap attribute definitions in a values do .. end block. That's horrible I have to admit so it's going away in Virtus 2.0. Sorry about that.

betamatt commented 9 years ago

Oof. Thanks.