solnic / virtus

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

combine Virtus classes #344

Open abaird opened 8 years ago

abaird commented 8 years ago

I want to do something like this:

  class UserModel
    include Virtus.model
    attribute(:first, String, default:Faker::Name.first_name )
    attribute(:last, String, default: Faker::Name.last_name )
    attribute(:email, String, default:lambda{|user, attribute| "#{user.first}.#{user.last}@devmail.company.com"} )
  end

  class OwnerModel
    include UserModel
    attribute(:email, String, default:lambda{|owner, attribute| "#{owner.first}.#{owner.last}@owner.company.com" })
    attribute(:address, String, default:'5210 Paseo de Pablo' )
  end

However, when I do that I get an error saying: wrong argument type Class (expected Module) (TypeError). I believe this is because Virtus wants me to combine things as Modules. However, if I change both of these above to Modules and then include them in my own class, I no longer get methods like attributes on my own class.

Is there a supported way to do this kind of composing?

neumachen commented 8 years ago

That would defeat of the coercion that virtus does. But if you must, you can always pass coerce: false and see if that would work.

And you can't include the your first class because it's a class and not a module.