Class: ValidatesPhoneNumber::Validator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidatesPhoneNumber::Validator
- Defined in:
- lib/validates_phone_number/validator.rb
Constant Summary
Constants included from Formats
Formats::LEADING_1, Formats::SEVEN_DIGITS, Formats::TEN_DIGITS
Constants included from Message
Message::DEFAULT_ERROR_MESSAGE
Instance Method Summary collapse
-
#initialize(options) ⇒ Validator
constructor
A new instance of Validator.
- #validate_each(record, attr_name, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ Validator
Returns a new instance of Validator.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/validates_phone_number/validator.rb', line 8 def initialize @default = .keys.size == 1 @type = .delete(:type) || :string @allow_nil = .delete(:allow_nil) @allow_blank = .delete(:allow_blank) @format = .delete(:format) @message = .delete(:message) @ten_digits = .delete(:ten_digits) @seven_digits = .delete(:seven_digits) @leading_1 = .delete(:leading_1) super end |
Instance Method Details
#validate_each(record, attr_name, value) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/validates_phone_number/validator.rb', line 21 def validate_each record, attr_name, value return if @allow_nil && value.nil? return if @allow_blank && value.blank? return if value =~ @format return if value =~ TEN_DIGITS if @ten_digits return if value =~ SEVEN_DIGITS if @seven_digits return if value =~ LEADING_1 if @leading_1 return if value =~ TEN_DIGITS || value =~ SEVEN_DIGITS if @default = value record.errors.add attr_name, end |