Refactor SiretFormatValidator#luhn_checksum

This commit is contained in:
gregoirenovel 2018-09-19 10:28:55 +02:00
parent 9d18f4187c
commit a0660d17eb

View file

@ -27,14 +27,9 @@ class SiretFormatValidator < ActiveModel::EachValidator
end
def luhn_checksum(value)
accum = 0
value.reverse.each_char.map(&:to_i).each_with_index do |digit, index|
value.reverse.each_char.map(&:to_i).map.with_index do |digit, index|
t = index.even? ? digit : digit * 2
t = t - 9 if t >= 10
accum += t
end
accum
t < 10 ? t : t - 9
end.sum
end
end