lib/aws/record/validations.rb in aws-sdk-1.1.4 vs lib/aws/record/validations.rb in aws-sdk-1.2.0
- old
+ new
@@ -19,10 +19,11 @@
require 'aws/record/validators/format'
require 'aws/record/validators/inclusion'
require 'aws/record/validators/length'
require 'aws/record/validators/numericality'
require 'aws/record/validators/presence'
+require 'aws/record/validators/method'
module AWS
module Record
# Validation methods to be used with subclasses of AWS::Record::Base.
@@ -98,21 +99,61 @@
#
module Validations
def self.extended base
- base.send(:define_method, :validate) do
+ base.send(:define_method, :run_validations) do
errors.clear!
self.class.send(:validators).each do |validator|
validator.validate(self)
end
end
- base.send(:private, :validate)
+ base.send(:private, :run_validations)
end
+ # Registers a validation method.
+ #
+ # validate :ensure_age_is_greater_than_shoe_size
+ #
+ # def ensure_age_is_greater_than_shoe_size
+ # unless age > shoe_size
+ # errors.add(:age, 'should be greater than your shoe size')
+ # end
+ # end
+ #
+ # You can also pass a list of method names that should be called during
+ # validation.
+ #
+ # validate :some_complex_validation, :some_other_validation
+ #
+ # As with most other validation methods you can also pass a hash of
+ # options that affect when the named validation methods get called.
+ #
+ # validate :my_custom_validation, :unless => :new_record?
+ #
+ # @overload validate(*method_names, options = {})
+ # @param [Array<Symbol>] method_names A list of methods to call
+ # during validation.
+ # @param [Hash] options
+ # @option options [Symbol] :on (:save) When this validation is run.
+ # Valid values include:
+ # * +:save+
+ # * +:create+
+ # * +:update+
+ # @option options [Symbol,String,Proc] :if Specifies a method or proc
+ # to call. The validation will only be run if the return value is
+ # of the method/proc is true (e.g. +:if => :name_changed?+ or
+ # +:if => lambda{|book| book.in_stock? }+).
+ # @option options [Symbol,String,Proc] :unless Specifies a method or
+ # proc to call. The validation will *not* be run if the return value
+ # is of the method/proc is false.
+ def validate *args
+ validators << MethodValidator.new(self, *args)
+ end
+
# This validation method is primariliy intended for ensuring a form
# checkbox (like an EULA agreement or terms of service acknowledgement)
# is checked.
#
# class User < AWS::Record::Base
@@ -176,13 +217,13 @@
# +:message+ is "must be accepted".
# @option options [Boolean] :allow_nil (true) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -231,13 +272,13 @@
# @param [Hash] options
# @option options [String] :message A custom error message. The defualt
# +:message+ is "doesn't match confirmation".
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -317,13 +358,13 @@
# added when the number of attribute values does not match
# the +:exactly+ option. Defaults to <code>"has the wrong
# number of values (should have exactly %{exactly}"</code>
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -352,13 +393,13 @@
# @param [Hash] options
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -398,13 +439,13 @@
# +:message+ is "is reserved".
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -447,13 +488,13 @@
# +:message+ is "is reserved".
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -484,13 +525,13 @@
# +:message+ is "is not included in the list".
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -545,13 +586,13 @@
# length (should be %{exactly} characters"</code>
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -595,13 +636,13 @@
# +:message+ is "is not a number".
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or
# +:if => lambda{|book| book.in_stock? }+).
# @option options [Symbol,String,Proc] :unless Specifies a method or
@@ -624,12 +665,12 @@
# @param [Hash] options
# @option options [String] :message A custom error message. The defualt
# +:message+ is "may not be blank".
# @option options [Symbol] :on (:save) When this validation is run.
# Valid values include:
- # * +:save:+
- # * +:create:+
- # * +:update:+
+ # * +:save+
+ # * +:create+
+ # * +:update+
# @option options [Boolean] :allow_nil (false) Skip validation if the
# attribute value is +nil+.
# @option options [Symbol,String,Proc] :if Specifies a method or proc
# to call. The validation will only be run if the return value is
# of the method/proc is true (e.g. +:if => :name_changed?+ or