Sha256: 2122a52f690343052f27e5fbcb868a22d0a1422f19d32ec00535b55ee19d5a83

Contents?: true

Size: 723 Bytes

Versions: 27

Compression:

Stored size: 723 Bytes

Contents

class TelephoneNumberValidator < ActiveModel::EachValidator
  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

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

  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
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
telephone_number-1.4.22 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.21 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.20 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.19 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.18 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.16 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.15 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.14 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.13 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.12 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.11 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.10 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.9 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.8 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.7 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.6 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.5 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.4 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.3 lib/active_model/telephone_number_validator.rb
telephone_number-1.4.2 lib/active_model/telephone_number_validator.rb