1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
module Validation
module Rule
# Phone rule
class Phone
# params can be any of the following:
#
# - :format - the phone number format
#
# Example:
#
# {:format => :america}
def initialize(params = {:format => :america})
@params = params
end
# returns the params given in the constructor
def params
@params
end
# determines if value is valid according to the constructor params
def valid_value?(value)
send(@params[:format], value)
end
def error_key
:phone
end
protected
def america(value)
digits = value.gsub(/\D/, '').split(//)
digits.length == 10 || digits.length == 11
end
end
end
end
|