4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/barcodevalidation.rb', line 4
def self.valid?(barcode)
barcode = barcode.to_s
return false unless [8, 10, 12, 13].include?(barcode.length)
parts = barcode.rjust(18, '0').chars.map(&:to_i)
checksum = parts.pop
calculated_checksum = 0
parts.each_with_index do |part, index|
if index % 2 == 0
calculated_checksum += (part * 3)
else
calculated_checksum += part
end
end
calculated_checksum = ((calculated_checksum.to_f / 10).ceil * 10) - calculated_checksum
checksum == calculated_checksum
rescue ArgumentError
false
end
|