virtus icon indicating copy to clipboard operation
virtus copied to clipboard

Get own value in "default" method

Open lessless opened this issue 11 years ago • 1 comments

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 }

lessless avatar Oct 23 '14 16:10 lessless

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.

benmoss avatar Feb 02 '15 16:02 benmoss