solnic / virtus

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

Allow to extend a Virtus model with per-instance attributes #376

Closed eregon closed 4 years ago

eregon commented 7 years ago

Hello, This is my first contribution to Virtus, please review.

Currently, creating an instance of a class with Virtus attributes, and then extending it with user.extend(Virtus.model) to add dynamic attributes breaks. This PR fixes it.

class User
  include Virtus.model
  attribute :name, String, default: 'John', lazy: true
end

user = User.new
user.extend(Virtus.model)
user.attribute(:active, Virtus::Attribute::Boolean, default: true, lazy: true)

p user.to_h # currently {:active=>true}
# But should be {:name=>"John", :active=>true}