Sha256: 51a7219a6bfa4807b0cef0a1b58fd543a8ac456d22fe37e336c9220d84c4f0c8
Contents?: true
Size: 874 Bytes
Versions: 4
Compression:
Stored size: 874 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) unless 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
4 entries across 4 versions & 1 rubygems