class PhonyPlausibleValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source

Validates a String using Phony.plausible? method.

# File lib/validators/phony_validator.rb, line 10
def validate_each(record, attribute, value)
  return if value.blank?

  @record = record
  value = PhonyRails.normalize_number(value.dup, default_country_code: normalized_country_code) if normalized_country_code
  value = PhonyRails.extract_extension(value).first
  @record.errors.add(attribute, error_message) unless Phony.plausible?(value, cc: country_number)
  @record.public_send("#{attribute}=", @record.public_send("#{attribute}_original")) if @record.respond_to?("#{attribute}_original") &&
                                                                                        !Phony.plausible?(value, cc: country_number)
end

Private Instance Methods

country_code() click to toggle source
# File lib/validators/phony_validator.rb, line 39
def country_code
  options_value(:country_code) || record_country_code
end
country_number() click to toggle source
# File lib/validators/phony_validator.rb, line 27
def country_number
  options_value(:country_number) || record_country_number || country_number_from_country_code
end
country_number_from_country_code() click to toggle source
# File lib/validators/phony_validator.rb, line 35
def country_number_from_country_code
  PhonyRails.country_number_for(country_code)
end
error_message() click to toggle source
# File lib/validators/phony_validator.rb, line 23
def error_message
  options[:message] || :improbable_phone
end
normalized_country_code() click to toggle source
# File lib/validators/phony_validator.rb, line 47
def normalized_country_code
  options_value(:normalized_country_code)
end
options_value(option) click to toggle source
# File lib/validators/phony_validator.rb, line 51
def options_value(option)
  option_value = options[option]

  return option_value unless option_value.is_a?(Symbol)

  @record.send(option_value)
end
record_country_code() click to toggle source
# File lib/validators/phony_validator.rb, line 43
def record_country_code
  @record.country_code if @record.respond_to?(:country_code) && !options_value(:ignore_record_country_code)
end
record_country_number() click to toggle source
# File lib/validators/phony_validator.rb, line 31
def record_country_number
  @record.country_number if @record.respond_to?(:country_number) && !options_value(:ignore_record_country_number)
end