module Validatr::Luhn

Public Class Methods

check_digit(number, n = 10) click to toggle source
# File lib/validatr/luhn.rb, line 13
def self.check_digit(number, n = 10)
  digits = digits_from_number(number)
  digits.push(0)
  sum = sum_digits(digits)
  (sum * 9) % n
end
sum_digits(digits) click to toggle source
# File lib/validatr/luhn.rb, line 20
def self.sum_digits(digits)
  digits.reverse.map.with_index do |d, i|
    if i.even?
      d
    else
      (d < 5 ? (d * 2) : ((d * 2) % 10) + 1)
    end
  end.inject(:+)
end
valid?(number, n = 10) click to toggle source
# File lib/validatr/luhn.rb, line 7
def self.valid?(number, n = 10)
  digits = digits_from_number(number)
  sum = sum_digits(digits)
  (sum % n).zero?
end