virtus icon indicating copy to clipboard operation
virtus copied to clipboard

attributes hash messed up on extending a Model instance with a Virtus.module?

Open k3rni opened this issue 10 years ago • 5 comments

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)".

k3rni avatar Apr 23 '14 15:04 k3rni

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?

tjstankus avatar Jan 07 '15 19:01 tjstankus

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.

solnic avatar Jan 07 '15 21:01 solnic

Cool, thanks for the clarification.

tjstankus avatar Jan 07 '15 22:01 tjstankus

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 avatar Jan 08 '15 01:01 k3rni

@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.

elskwid avatar Jan 08 '15 01:01 elskwid