Class: ValidatesPhoneNumber::Validator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
Formats, Message
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

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 options
  @default = options.keys.size == 1
  @type = options.delete(:type) || :string
  @allow_nil = options.delete(:allow_nil)
  @allow_blank = options.delete(:allow_blank)
  @format = options.delete(:format)
  @message = options.delete(:message)
  @ten_digits = options.delete(:ten_digits)
  @seven_digits = options.delete(:seven_digits)
  @leading_1 = options.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
  message = error_message_for value
  record.errors.add attr_name, message
end