solnic / virtus

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

Get own value in "default" method #298

Open lessless opened 9 years ago

lessless commented 9 years ago

Hello I would like to make a constrain for an attribute, e.g. #role can be either :admin, :manager or :user only, and it must be set to the :user in any other case.

attribute :role,  Symbol,  default: ->(user, role) { [:admin, :manager].include? role ? role : :user }

benmoss commented 9 years ago

I think this is beyond what it means to have a "default value", this is really a kind of data validation, which I think is beyond the scope of Virtus. I'm not the maintainer of this project but I'd say you should either just override the setter method or find/build another library to do this kind of thing.