Class: FakerExtensionFr::Luhn
- Inherits:
-
Object
- Object
- FakerExtensionFr::Luhn
- Defined in:
- lib/faker_extension_fr.rb
Class Method Summary collapse
- .checksum(number) ⇒ Object
- .luhn_doubled(number) ⇒ Object
- .split_digits(number) ⇒ Object
- .sum_of(number) ⇒ Object
- .valid?(number) ⇒ Boolean
Class Method Details
.checksum(number) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/faker_extension_fr.rb', line 16 def self.checksum(number) products = luhn_doubled(number) sum = products.inject { |t,p| t + sum_of(p) } checksum = 10 - (sum % 10) checksum == 10 ? 0 : checksum end |
.luhn_doubled(number) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/faker_extension_fr.rb', line 22 def self.luhn_doubled(number) numbers = split_digits(number).reverse numbers.map.with_index do |n,i| i.even? ? n*2 : n*1 end.reverse end |
.split_digits(number) ⇒ Object
35 36 37 |
# File 'lib/faker_extension_fr.rb', line 35 def self.split_digits(number) number.to_s.split(//).map(&:to_i) end |
.sum_of(number) ⇒ Object
28 29 30 |
# File 'lib/faker_extension_fr.rb', line 28 def self.sum_of(number) split_digits(number).inject(:+) end |
.valid?(number) ⇒ Boolean
31 32 33 34 |
# File 'lib/faker_extension_fr.rb', line 31 def self.valid?(number) numbers = split_digits(number) numbers.pop == checksum(numbers.join) end |