virtus
virtus copied to clipboard
Get own value in "default" method
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 }
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.