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

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

Instance Method Details

#check_digitObject



59
60
61
# File 'lib/barcodevalidation/gtin/base.rb', line 59

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

#to_all_validObject



34
35
36
37
38
39
40
41
# 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_12Object



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

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

#to_gtin_13Object



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

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

#to_gtin_14Object



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

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

#to_gtin_8Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/barcodevalidation/gtin/base.rb', line 22

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

#valid_lengthObject



26
27
28
29
30
# 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