Class: ValidatesPhoneNumber::Validator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validates_phone_number/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Validator

Returns a new instance of Validator.



5
6
7
8
9
10
# File 'lib/validates_phone_number/validator.rb', line 5

def initialize options
  @type = options.delete(:type) || :string
  @allow_nil = options.delete(:allow_nil)
  @format = options.delete(:format)
  super
end

Instance Method Details

#validate_each(record, attr_name, value) ⇒ Object



12
13
14
15
16
17
# File 'lib/validates_phone_number/validator.rb', line 12

def validate_each record, attr_name, value
  return if @allow_nil && value.nil?
  return if Phoner::Phone.valid? value
  return if value =~ @format
  record.errors.add attr_name, "'#{value}' is not a valid"
end