solnic / virtus

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

Avoid override of previously assigned attributes #364

Open chinshr opened 7 years ago

chinshr commented 7 years ago

I am having an issue to assign nested attributes avoiding to override previous ones. Here is an example:

class Product
  include Virtus.model
  attribute :values, Values
end

class Values
  include Virtus.model
  attributes :name
  attributes :description
end

Now let's say we want to assign attribute of values in two separate assigns:

product = Product.new
product.values = {name: "a product"}
product.values.name   # => "a product"
product.values.description   # => nil

# now assign description
product.values = {description: "a description"}
product.values.description  # =>  "a description"
product.values.name  # => nil, I don't want name to be nil

Can anyone show me a way how to solve this problem without overriding the elements, considering, there could potentially be deeper nested structures.