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

#*, #==, 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.



43
44
45
# File 'lib/barcodevalidation/gtin/base.rb', line 43

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
# File 'lib/barcodevalidation/gtin/base.rb', line 31

def self.inherited(subclass)
  BarcodeValidation::GTIN.append_gtin_class(subclass)
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.

Raises:



36
37
38
39
40
# File 'lib/barcodevalidation/gtin/base.rb', line 36

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

  GTIN.reprioritize_before(self, other_gtin_class)
end

Instance Method Details

#check_digitObject



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

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

#to_all_validObject



62
63
64
65
66
67
68
69
# File 'lib/barcodevalidation/gtin/base.rb', line 62

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

#to_gtin_12Object



75
76
77
# File 'lib/barcodevalidation/gtin/base.rb', line 75

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

#to_gtin_13Object



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

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

#to_gtin_14Object



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

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

#to_gtin_8Object



71
72
73
# File 'lib/barcodevalidation/gtin/base.rb', line 71

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

#valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/barcodevalidation/gtin/base.rb', line 50

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

#valid_lengthObject



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

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