Class: GS1::Barcode::Segment
- Inherits:
-
Object
- Object
- GS1::Barcode::Segment
- Defined in:
- lib/gs1/barcode/segment.rb
Overview
A segment of a barcode. Retrives a single segment and keeps the rest.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(data, separator: GS1.configuration.barcode_separator, ai_classes: GS1.ai_classes) ⇒ Segment
constructor
A new instance of Segment.
-
#record ⇒ Object
Fetch the two first characters (interpreted as AI) from the remaining data and try to find record class.
- #record_data ⇒ Object
- #rest ⇒ Object
- #to_params ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(data, separator: GS1.configuration.barcode_separator, ai_classes: GS1.ai_classes) ⇒ Segment
Returns a new instance of Segment.
8 9 10 11 12 |
# File 'lib/gs1/barcode/segment.rb', line 8 def initialize(data, separator: GS1.configuration., ai_classes: GS1.ai_classes) @data = data.to_s.chars @separator = separator @ai_classes = ai_classes end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/gs1/barcode/segment.rb', line 6 def data @data end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
6 7 8 |
# File 'lib/gs1/barcode/segment.rb', line 6 def separator @separator end |
Instance Method Details
#errors ⇒ Object
64 65 66 |
# File 'lib/gs1/barcode/segment.rb', line 64 def errors @errors ||= [] end |
#record ⇒ Object
Fetch the two first characters (interpreted as AI) from the remaining data and try to find record class. If no record class was found, fetch a third character and try again, and then finally a forth, as no AI currently have more then 4 characters.
24 25 26 27 28 |
# File 'lib/gs1/barcode/segment.rb', line 24 def record @record ||= process_ai_variants(2) || process_ai_variants(1) || process_ai_variants(1) end |
#record_data ⇒ Object
30 31 32 33 34 |
# File 'lib/gs1/barcode/segment.rb', line 30 def record_data return unless record @record_data ||= _record_data end |
#rest ⇒ Object
36 37 38 39 40 |
# File 'lib/gs1/barcode/segment.rb', line 36 def rest record_data # Trigger segment retrieval @data.join end |
#to_params ⇒ Object
14 15 16 17 18 |
# File 'lib/gs1/barcode/segment.rb', line 14 def to_params return [] unless record [record.underscore_name, record_data] end |
#valid? ⇒ Boolean
42 43 44 45 46 47 48 |
# File 'lib/gs1/barcode/segment.rb', line 42 def valid? errors.clear validate errors.empty? end |
#validate ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/gs1/barcode/segment.rb', line 50 def validate if record errors << "Unable to retrieve data to #{record}" unless record_data else errors << "Unable to retrieve record from application identifier(s) #{ai_variants.join(', ')}" end end |
#validate! ⇒ Object
58 59 60 61 62 |
# File 'lib/gs1/barcode/segment.rb', line 58 def validate! return true if valid? raise InvalidTokenError, errors.join(', ') end |