class ActiveModel::Validations::PostalCodeValidator

Public Class Methods

known_formats() click to toggle source
# File lib/active_validators/active_model/validations/postal_code_validator.rb, line 17
def self.known_formats
  @@known_formats ||= {
    'ad' => ['AD###', '###'],
    'ar' => ['####', '@####@@@'],
    'at' => ['####'],
    'au' => ['####'],
    'be' => ['####'],
    'bg' => ['####'],
    'br' => ['#####-###', '########'],
    'ca' => ['@#@ #@#', '@#@#@#'],
    'ch' => ['####'],
    'cz' => ['### ##', '#####'],
    'de' => ['#####'],
    'dk' => ['####'],
    'dr' => ['#####'],
    'sp' => ['#####'],
    'fi' => ['#####'],
    'fr' => ['#####'],
    'uk' => ['@# #@@', '@## #@@', '@@# #@@', '@@## #@@', '@#@ #@@', '@@#@ #@@'],
    'gb' => ['@# #@@', '@## #@@', '@@# #@@', '@@## #@@', '@#@ #@@', '@@#@ #@@'],
    'gf' => ['#####'],
    'gl' => ['####'],
    'gp' => ['#####'],
    'gt' => ['#####'],
    'hr' => ['HR-#####', 'H#####'],
    'hu' => ['####'],
    'in' => ['######'],
    'ic' => ['###'],
    'it' => ['#####'],
    'jp' => ['###-####', '#######'],
    'ky' => ['KY#-####'],
    'li' => ['####'],
    'lk' => ['#####'],
    'lt' => ['LT-#####', '#####'],
    'lu' => ['####'],
    'mc' => ['#####'],
    'md' => ['MD-####'],
    'mk' => ['####'],
    'mq' => ['#####'],
    'mx' => ['#####'],
    'my' => ['#####'],
    'nl' => ['#### @@', '####@@'],
    'no' => ['####'],
    'nz' => ['####'],
    'ph' => ['####'],
    'pk' => ['#####'],
    'pl' => ['##-###', '#####'],
    'pm' => ['#####'],
    'pt' => ['####', '####-###'],
    'ru' => ['######'],
    'se' => ['SE-#### ##', '#### ##', '######'],
    'sg' => ['######'],
    'si' => ['SI- ####', 'SI-####', '####'],
    'sk' => ['### ##', '#####'],
    'sm' => ['4789#', '#'],
    'th' => ['#####'],
    'tr' => ['#####'],
    'us' => ['#####', '#####-####'],
    'wf' => ['#####'],
    'za' => ['####']
  }
end

Private Class Methods

regexp_from(format) click to toggle source
# File lib/active_validators/active_model/validations/postal_code_validator.rb, line 87
def self.regexp_from(format)
  Regexp.new '\A' + ActiveValidators::OneNineShims::OneNineString.new(Regexp.escape format).gsub(/[@#]/, '@' => '[[:alpha:]]', '#' => 'd') + '\z'
end

Public Instance Methods

matches_any?() click to toggle source
# File lib/active_validators/active_model/validations/postal_code_validator.rb, line 80
def matches_any?
  return true if @formats.nil? or not @formats.respond_to?(:detect)
  @formats.detect { |format| @value.match(PostalCodeValidator.regexp_from format) }
end
validate_each(record, attribute, value) click to toggle source
# File lib/active_validators/active_model/validations/postal_code_validator.rb, line 4
def validate_each(record, attribute, value)
  @value = value.to_s
  unless country = options[:country]
    if country_method = options[:country_method]
      country = record.send(country_method)
    else
      country = 'us'
    end
  end
  @formats = PostalCodeValidator.known_formats[country.to_s.downcase]
  record.errors.add(attribute) if value.blank? || !matches_any?
end