Class: Bookland::EAN

Inherits:
Identifier show all
Defined in:
lib/bookland/ean.rb

Direct Known Subclasses

ISBN

Constant Summary collapse

WEIGHTS =
([1, 3] * 6).freeze

Instance Attribute Summary

Attributes inherited from Identifier

#digits

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Identifier

#==, #checksum_digit, #data_digits, #initialize, #to_s, valid?

Constructor Details

This class inherits a constructor from Bookland::Identifier

Class Method Details

.calculate_checksum_digit(digits) ⇒ Object



5
6
7
8
# File 'lib/bookland/ean.rb', line 5

def self.calculate_checksum_digit(digits)
  sum = digits.map(&:to_i).zip(WEIGHTS).reduce(0) { |a, (i, j)| a + i * j }
  ((10 - sum % 10) % 10).to_s
end

Instance Method Details

#valid?Boolean



10
11
12
# File 'lib/bookland/ean.rb', line 10

def valid?
  digits.size == 13 && super
end