Can I check the validity of a single DataMapper property?
- by Nathan Long
In a custom DataMapper setter, I'd like to check whether the value I'm setting is valid or not.
For instance:
class ToastMitten
include DataMapper::Resource
property :id, Serial
property :wearer, Enum['Chuck Norris', 'Jon Skeet']
property :first_worn_at, DateTime
def wearer=(name)
super
if wearer.valid? # How can I do this?
first_worn_at = Time.now
end
end
end
t = ToastMitten.new
t.wearer = 'Nathan Long' # invalid value; do NOT set first_worn_at
t.wearer = 'Jon Skeet' # valid value; set first_worn_at
Can I check the validity of a single property like this without calling valid? on the object itself and looking through all the errors?