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
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.
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.
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.
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_digit ⇒ Object
87 88 89 |
# File 'lib/barcodevalidation/gtin/base.rb', line 87 def check_digit CheckDigit.new(last, expected: expected_check_digit) end |
#to_all_valid ⇒ Object
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_12 ⇒ Object
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_13 ⇒ Object
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_14 ⇒ Object
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_8 ⇒ Object
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
50 51 52 |
# File 'lib/barcodevalidation/gtin/base.rb', line 50 def valid? valid_length == length && check_digit.valid? end |
#valid_length ⇒ Object
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 |