Sha256: 6ff016b3d4ec008b33b18332aed457b0bd3c2f8ba5350a31504c78ec1de6e208
Contents?: true
Size: 772 Bytes
Versions: 1
Compression:
Stored size: 772 Bytes
Contents
class ImeiValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) unless valid?(value.to_s) record.errors[attribute] << options.fetch(: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 |c, i| result = c.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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_validation-3.0.0 | lib/active_validation/validators/imei_validator.rb |