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 end
def luhn_checksum(value) def luhn_checksum(value)
accum = 0 value.reverse.each_char.map(&:to_i).map.with_index do |digit, index|
value.reverse.each_char.map(&:to_i).each_with_index do |digit, index|
t = index.even? ? digit : digit * 2 t = index.even? ? digit : digit * 2
t = t - 9 if t >= 10 t < 10 ? t : t - 9
accum += t end.sum
end
accum
end end
end end