Sha256: ca4b8adbcc505b1deb57161949a6301dc506ef6d8efd1f783f02b557b916825f
Contents?: true
Size: 1.15 KB
Versions: 1
Compression:
Stored size: 1.15 KB
Contents
# https://en.wikipedia.org/wiki/Vehicle_identification_number#Check-digit_calculation # GB 16735-2004 module Unidom module ArticleNumber class VehicleIdentificationNumberValidator < ActiveModel::EachValidator WEIGHTS = '8765432X098765432'.freeze TRANSLITERATION = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ'.freeze CHECK_DIGITS = '0123456789X'.freeze def validate_each(record, attribute, value) value = value.to_s.upcase if value.length>17 record.errors[attribute] << (options[:message]||'The length should be 17') elsif value.match /I|O|Q/ record.errors[attribute] << (options[:message]||'I, O, Q should be excluded') else record.errors[attribute] << (options[:message]||'is invalid') unless check_digit(value)==value[8] end end def check_digit(value) checksum = 0 value.chars.each_with_index do |char, index| checksum += self.class::TRANSLITERATION.index(char)%10*self.class::CHECK_DIGITS.index(self.class::WEIGHTS.at(index)) end self.class::CHECK_DIGITS.at checksum%11 end private :check_digit end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
unidom-article_number-3.0.1 | app/validators/unidom/article_number/vehicle_identification_number_validator.rb |