Sha256: e72c76b78b04968ecc48ef8e1ea52b812015e1508e639919a6089c3bf39ab0ae

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module ActiveModel
  module Validations
    class TrackingNumberValidator < EachValidator
      def validate_each(record, attribute, value)
        @value = value
        carrier = options[:carrier]
        raise "Carrier option required" unless carrier
        @formats = TrackingNumberValidator.known_formats[carrier]
        raise "No known tracking number formats for carrier #{carrier}" unless @formats
        record.errors.add(attribute) unless matches_any?
      end

      def self.known_formats
        @@known_formats ||= {
          # see https://www.ups.com/content/us/en/tracking/help/tracking/tnh.html
          :ups => ['1Z################', '############', 'T##########', '#########'],
        }
      end

      def matches_any?
        false if @formats.nil? or not @formats.respond_to?(:detect)
        @formats.detect { |format| @value.match(TrackingNumberValidator.regexp_from format) }
      end

      private

      def self.regexp_from(format)
        Regexp.new "^"+(Regexp.escape format).gsub('\#','\d')+"$"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activevalidators-1.5.0 lib/active_model/validations/tracking_number_validator.rb