Class: BarcodeValidation::GTIN::Base
- Inherits:
-
DigitSequence
- Object
- Array
- DigitSequence
- BarcodeValidation::GTIN::Base
- Extended by:
- Forwardable
- Defined in:
- lib/barcodevalidation/gtin/base.rb
Defined Under Namespace
Classes: AbstractMethodError
Constant Summary collapse
- MODULUS =
10
Constants inherited from DigitSequence
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Class Method Summary collapse
-
.abstract_class ⇒ Object
This class is abstract and should not be included in the list of GTIN classes that actually implement a GTIN.
-
.handles?(input) ⇒ Boolean
Does this class (potentially) handle a GTIN that matches the input? Subclasses can choose to implement their own logic.
-
.inherited(subclass) ⇒ Object
Upon inheritance, register the subclass so users of the library can dynamically add more GTINs in their own code.
-
.prioritize_before(other_gtin_class) ⇒ Object
Ensure this class is earlier in the GTIN classes list than
other_gtin_classand thus will get asked earlier if it handles a GTIN.
Instance Method Summary collapse
- #check_digit ⇒ Object
-
#initialize(input) ⇒ Base
constructor
A new instance of Base.
- #to_all_valid ⇒ Object
- #to_gtin_12 ⇒ Object
- #to_gtin_13 ⇒ Object
- #to_gtin_14 ⇒ Object
- #to_gtin_8 ⇒ Object
- #valid? ⇒ Boolean
- #valid_length ⇒ Object
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
#input ⇒ Object (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_class ⇒ Object
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.
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_digit ⇒ Object
96 97 98 |
# File 'lib/barcodevalidation/gtin/base.rb', line 96 def check_digit CheckDigit.new(last, expected: expected_check_digit) end |
#to_all_valid ⇒ Object
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_12 ⇒ Object
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_13 ⇒ Object
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_14 ⇒ Object
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_8 ⇒ Object
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
54 55 56 |
# File 'lib/barcodevalidation/gtin/base.rb', line 54 def valid? valid_length == length && !all_zeros? && check_digit.valid? end |
#valid_length ⇒ Object
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 |