solnic / virtus

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

How to reset an attribute to the default value? #154

Closed rgrinberg closed 11 years ago

rgrinberg commented 11 years ago

Say I have an attribute defined like attribute :foo, Symbol, default: :bar and I'd like to have a method to reset foo to the default value :bar in this case. What's the clean way of doing this?

Sorry if this isn't the right place to ask..

solnic commented 11 years ago

There's no public interface for that (yet?) however you can access default attribute value like this:

YourModel.attribute_set[:foo].default.call

In current master I introduced a new method set_defaults, it's not a public method and it sets defaults on ALL attributes but I could consider making it public and accepting attribute names.

rgrinberg commented 11 years ago

Thanks, that works. +1 for having a clean way to do this in the future.