virtus
virtus copied to clipboard
attributes hash messed up on extending a Model instance with a Virtus.module?
require 'virtus'
class OneField
include Virtus.model
attribute :one, String
end
module OtherField
include Virtus.module
attribute :two, Integer
end
obj = OneField.new one: 13
puts obj.attributes # (1)
obj.extend OtherField
obj.two = 123
puts obj.attributes # (2)
puts obj.one # (3)
puts obj[:two] # (4)
The puts marked with "(2)" should print out {:one => "13", :two => 123}
, but this is not the case, as it only prints {:two => 123}
. However, the attributes are still accessible, as shown by "(3)" and "(4)".
I looked into this and it seems related to the fact that attribute_set
is class scope. Changing attribute_set
to instance scope seems fraught with backwards incompatibilities. @solnic do you have any thoughts about how this issue might be fixed in a backwards compatible way?
Sorry but this will not be fixed. It's a mis-use of the library. Should be documented. Maybe we could tackle this in 2.0 if somebody can find a simple way to support this. Personally I would advocate against such a mix.
Cool, thanks for the clarification.
Fair enough, though it's a bit disappointing to have to use the attribute
syntax instead of a single extend
with a module definition. Needs to be documented somewhere next to 'Dynamically extending instances'.
@k3rni - can you come up with some example language that would have clarified the issue for you? I am happy to add it to the README.