Class: BarcodeValidation::GTIN::Base

Inherits:
DigitSequence show all
Extended by:
Forwardable
Defined in:
lib/barcodevalidation/gtin/base.rb

Direct Known Subclasses

GTIN12, GTIN13, GTIN14, GTIN8

Defined Under Namespace

Classes: AbstractMethodError

Constant Summary collapse

MODULUS =
10

Constants inherited from DigitSequence

DigitSequence::ArgumentError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DigitSequence

#*, #==, #all_zeros?, cast

Methods included from Mixin::ValueObject

#eql?, included, #inspect, #pretty_print

Constructor Details

#initialize(input) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
20
# 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

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



12
13
14
# File 'lib/barcodevalidation/gtin/base.rb', line 12

def input
  @input
end

Class Method Details

.abstract_classObject

This class is abstract and should not be included in the list of GTIN classes that actually implement a GTIN.



47
48
49
# File 'lib/barcodevalidation/gtin/base.rb', line 47

def self.abstract_class
  BarcodeValidation::GTIN.remove_gtin_class(self)
end

.handles?(input) ⇒ Boolean

Does this class (potentially) handle a GTIN that matches the input? Subclasses can choose to implement their own logic. The default is to look at VALID_LENGTH and use that to match the length of the input the class handles.

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/barcodevalidation/gtin/base.rb', line 24

def self.handles?(input)
  return false unless const_defined?(:VALID_LENGTH)

  input.length == self::VALID_LENGTH
end

.inherited(subclass) ⇒ Object

Upon inheritance, register the subclass so users of the library can dynamically add more GTINs in their own code.



31
32
33
34
# File 'lib/barcodevalidation/gtin/base.rb', line 31

def self.inherited(subclass)
  BarcodeValidation::GTIN.append_gtin_class(subclass)
  super
end

.prioritize_before(other_gtin_class) ⇒ Object

Ensure this class is earlier in the GTIN classes list than other_gtin_class and thus will get asked earlier if it handles a GTIN.



37
38
39
40
41
42
43
44
# File 'lib/barcodevalidation/gtin/base.rb', line 37

def self.prioritize_before(other_gtin_class)
  unless GTIN.gtin_class?(other_gtin_class)
    raise ArgumentError,
      "The class you want to prioritize before is not a registered prioritized GTIN class."
  end

  GTIN.reprioritize_before(self, other_gtin_class)
end

Instance Method Details

#check_digitObject



96
97
98
# File 'lib/barcodevalidation/gtin/base.rb', line 96

def check_digit
  CheckDigit.new(last, expected: expected_check_digit)
end

#to_all_validObject



71
72
73
74
75
76
77
78
# File 'lib/barcodevalidation/gtin/base.rb', line 71

def to_all_valid
  [
    to_gtin_8,
    to_gtin_12,
    to_gtin_13,
    to_gtin_14
  ].select(&:valid?)
end

#to_gtin_12Object



84
85
86
# File 'lib/barcodevalidation/gtin/base.rb', line 84

def to_gtin_12
  is_a?(GTIN12) ? self : transcode_to(GTIN12)
end

#to_gtin_13Object



88
89
90
# File 'lib/barcodevalidation/gtin/base.rb', line 88

def to_gtin_13
  is_a?(GTIN13) ? self : transcode_to(GTIN13)
end

#to_gtin_14Object



92
93
94
# File 'lib/barcodevalidation/gtin/base.rb', line 92

def to_gtin_14
  is_a?(GTIN14) ? self : transcode_to(GTIN14)
end

#to_gtin_8Object



80
81
82
# File 'lib/barcodevalidation/gtin/base.rb', line 80

def to_gtin_8
  is_a?(GTIN8) ? self : transcode_to(GTIN8)
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/barcodevalidation/gtin/base.rb', line 54

def valid?
  valid_length == length && !all_zeros? && check_digit.valid?
end

#valid_lengthObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/barcodevalidation/gtin/base.rb', line 58

def valid_length
  unless self.class.const_defined?(:VALID_LENGTH)
    raise(
      AbstractMethodError,
      "Concrete classes must define the VALID_LENGTH constant"
    )
  end

  self.class::VALID_LENGTH
end