Sha256: 1d8a6ab2b077df58a83d6e5fc28d07cfecbfaf413f4b5ab77e3b5441c91a2e67
Contents?: true
Size: 818 Bytes
Versions: 2
Compression:
Stored size: 818 Bytes
Contents
# frozen_string_literal: true class ImeiValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if valid?(value.to_s) record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.imei')) end private def valid_format?(value) value =~ /\A[\d\.\:\-\s]+\z/i end def valid_length?(value) value.present? end def valid_luhn?(value) number = value.gsub(/\D/, '').reverse total = 0 number.chars.each_with_index do |chr, idx| result = chr.to_i result *= 2 if idx.odd? result = (1 + (result - 10)) if result >= 10 total += result end (total % 10).zero? end def valid?(value) valid_length?(value) && valid_format?(value) && valid_luhn?(value) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_validation-5.0.0 | lib/active_validation/validators/imei_validator.rb |
active_validation-4.1.0 | lib/active_validation/validators/imei_validator.rb |