Sha256: b4d2e0cf9e34b426914136ad13526037f37c080ea84088c0df146f6e42dfe7e5

Contents?: true

Size: 415 Bytes

Versions: 2

Compression:

Stored size: 415 Bytes

Contents

module Bookland
  # An International Article Number.
  class EAN < Identifier
    # Calculates the checksum for the 12-digit payload of an EAN.
    def self.calculate_checksum(payload)
      payload.map! &:to_i
      weights = [1, 3] * 6
      sum = payload.zip(weights).inject(0) { |a, (i, j)| a + i * j }

      ((10 - sum % 10) % 10).to_s
    end

    def valid?
      @raw.size == 13 && super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bookland-2.0.0 lib/bookland/ean.rb
bookland-2.0.0.beta lib/bookland/ean.rb