Can I check the validity of a single DataMapper property?

Posted by Nathan Long on Stack Overflow See other posts from Stack Overflow or by Nathan Long
Published on 2012-02-29T17:28:09Z Indexed on 2012/04/09 23:30 UTC
Read the original article Hit count: 473

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-datamapper