solnic / virtus

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

Keys/Values for an extended hash are not updated #150

Closed vivien closed 11 years ago

vivien commented 11 years ago

As the Virtus attributes are a different set of properties, I don't know if this is a bug or a design choice, so I just report it.

require 'virtus'

hash = Hash.new.extend Virtus
hash.attribute :foo
hash.attributes = { "foo" => "Bar" }

hash.foo # => "Bar"
hash["foo"] # => "Bar"
hash[:foo] # => "Bar"
hash # => {}

It sounds like #[] is overridden but the keys/values are not updated. As I need to get a "smart hash", I ended up using:

hash.replace hash.attributes
hash.foo # => "Bar"
hash # => {:foo=>"Bar"}

To make things move forward, we may answer this: should Virtus be object-agnostic or should it care about the object it extends?

Cheers, Vivien

solnic commented 11 years ago

Oh Virtus can't handle such cases. I wouldn't recommend extending core ruby classes (esp if they have interfaces clashing with virtus).

You'd be better off wrapping a hash in your own virtus-extended object.

I'm going to close this one.