Method: NotNaughty#validated_before

Defined in:
lib/not_naughty.rb

#validated_before(method) ⇒ Object

Prepends a call for validation before then given method. If, on call, the validation passes the method is called. Otherwise it raises an NotNaughty::ValidationException or returns false.

Example:

validated_before :save # raise ValidationException unless valid?


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/not_naughty.rb', line 73

def validated_before(method)
  __method = :"#{method}_without_validations"
  alias_method __method, method

  define_method method do |*params|
    begin
      if valid? then send __method else raise errors end
    rescue Exception => error
      self.class.validator.error_handler.raise error
    end
  end
end