class BarcodeValidation::GTIN::Base
Constants
- ConversionError
- MODULUS
Attributes
input[R]
Public Class Methods
new(input)
click to toggle source
Calls superclass method
BarcodeValidation::DigitSequence::new
# File lib/barcodevalidation/gtin/base.rb, line 14 def initialize(input) @input = input super rescue BarcodeValidation::Error => e BarcodeValidation::InvalidGTIN.new(input, error: e) end
Public Instance Methods
to_all_valid()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 34 def to_all_valid [ to_gtin_8, to_gtin_12, to_gtin_13, to_gtin_14, ].select(&:valid?) end
to_gtin_12()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 47 def to_gtin_12 is_a?(GTIN12) ? self : transcode_to(GTIN12) end
to_gtin_13()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 51 def to_gtin_13 is_a?(GTIN13) ? self : transcode_to(GTIN13) end
to_gtin_14()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 55 def to_gtin_14 is_a?(GTIN14) ? self : transcode_to(GTIN14) end
to_gtin_8()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 43 def to_gtin_8 is_a?(GTIN8) ? self : transcode_to(GTIN8) end
valid?()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 22 def valid? valid_length == length && check_digit.valid? end
valid_length()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 26 def valid_length raise(AbstractMethodError, "Concrete classes must define the VALID_LENGTH constant") unless self.class.const_defined?(:VALID_LENGTH) self.class::VALID_LENGTH end
Private Instance Methods
check_digit()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 61 def check_digit CheckDigit.new(last, expected: expected_check_digit) end
checkable_digits()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 76 def checkable_digits take(length - 1).reverse.map(&:to_i) end
expected_check_digit()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 65 def expected_check_digit (MODULUS - weighted_checkable_digit_sum) % MODULUS end
transcode_to(klass)
click to toggle source
Instantiates the given class with the string value of this instance with any leading zeros stripped out, and then zero-padded up to the valid length of the given class. If the resulting string is <> the valid length of the target format, the returned object will be a BarcodeValidation::InvalidGTIN
with valid? = false and a meaningful error message.
# File lib/barcodevalidation/gtin/base.rb, line 86 def transcode_to(klass) gtin = klass.new(format("%0#{klass::VALID_LENGTH}d", to_s.gsub(/^0+/, ""))) if gtin.valid? gtin else BarcodeValidation::InvalidGTIN.new( input, error: klass::ConversionError.new(klass).exception(input), ) end end
weighted_checkable_digit_sum()
click to toggle source
# File lib/barcodevalidation/gtin/base.rb, line 69 def weighted_checkable_digit_sum checkable_digits .zip([3, 1].cycle) .map { |digit, factor| digit * factor } .reduce(&:+) end