Class: TelephoneNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_model/telephone_number_validator.rb

Instance Method Summary collapse

Instance Method Details

#country(record) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_model/telephone_number_validator.rb', line 12

def country(record)
  country_option = options[:country]
  if country_option.is_a? Proc
    country_option.call(record)
  elsif country_option.is_a?(Symbol) || country_option.is_a?(String)
    # make sure its a lowercase symbol
    country_option.downcase.to_sym
  else
    raise ArgumentError.new('country option must be a Proc, Symbol or String')
  end
end

#messageObject



8
9
10
# File 'lib/active_model/telephone_number_validator.rb', line 8

def message
  options[:message] || :invalid
end

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
# File 'lib/active_model/telephone_number_validator.rb', line 2

def validate_each(record, attribute, value)
  valid_types = options.fetch(:types, [])
  args = [value, country(record), valid_types]
  record.errors.add(attribute, message) if TelephoneNumber.invalid?(*args)
end