Sha256: 5b43c68199f56cf419c96ecff16c7c9eab66ac2064fc01a13c70dcdd4954836a
Contents?: true
Size: 794 Bytes
Versions: 5
Compression:
Stored size: 794 Bytes
Contents
class ImeiValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value.to_s) record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.imei')) end 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 |character, i| result = character.to_i result *= 2 if i.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
5 entries across 5 versions & 1 rubygems