solnic / virtus

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

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

Open k3rni opened 10 years ago

k3rni commented 10 years ago
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)".

tjstankus commented 9 years ago

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?

solnic commented 9 years ago

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.

tjstankus commented 9 years ago

Cool, thanks for the clarification.

k3rni commented 9 years ago

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

elskwid commented 9 years ago

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