Sha256: 84f55a405373bc2423cc5e2f2fb8d1ffdea61ee5e7a760c97e3a7dcb3cde2710
Contents?: true
Size: 887 Bytes
Versions: 2
Compression:
Stored size: 887 Bytes
Contents
module ActiveModel module Validations class PostalCodeValidator < EachValidator def validate_each(record, attribute, value) @value = value country = options[:country] || :us @formats = PostalCodeValidator.known_formats[country] raise "No known postal code formats for country #{country}" unless @formats record.errors.add(attribute) if value.blank? || !matches_any? end def self.known_formats @@known_formats ||= { :us => ['#####', '#####-####'], } end def matches_any? false if @formats.nil? or not @formats.respond_to?(:detect) @formats.detect { |format| @value.match(PostalCodeValidator.regexp_from format) } end private def self.regexp_from(format) Regexp.new "^"+(Regexp.escape format).gsub('\#','\d')+"$" end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
activevalidators-1.7.1 | lib/active_model/validations/postal_code_validator.rb |
activevalidators-1.7.0 | lib/active_model/validations/postal_code_validator.rb |