class GS1::Barcode::Segment
A segment of a barcode. Retrives a single segment and keeps the rest.
Attributes
data[R]
separator[R]
Public Class Methods
new(data, separator: GS1.configuration.barcode_separator)
click to toggle source
# File lib/gs1/barcode/segment.rb, line 8 def initialize(data, separator: GS1.configuration.barcode_separator) @data = data.to_s.chars @separator = separator end
Public Instance Methods
errors()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 63 def errors @errors ||= [] end
record()
click to toggle source
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.
# File lib/gs1/barcode/segment.rb, line 23 def record @record ||= process_ai_variants(2) || process_ai_variants(1) || process_ai_variants(1) end
record_data()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 29 def record_data return unless record @record_data ||= _record_data end
rest()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 35 def rest record_data # Trigger segment retrieval @data.join end
to_params()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 13 def to_params return [] unless record [record.underscore_name, record_data] end
valid?()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 41 def valid? errors.clear validate errors.empty? end
validate()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 49 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!()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 57 def validate! return true if valid? raise InvalidTokenError, errors.join(', ') end
Private Instance Methods
_record_data()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 85 def _record_data shift_barcode_length || shift_separator_length || shift_max_barcode_length end
ai_variants()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 69 def ai_variants @ai_variants ||= [] end
can_shift_ai_data?(shifts)
click to toggle source
# File lib/gs1/barcode/segment.rb, line 81 def can_shift_ai_data?(shifts) data.size >= shifts end
can_shift_record_data?(shifts)
click to toggle source
# File lib/gs1/barcode/segment.rb, line 121 def can_shift_record_data?(shifts) data.size >= shifts && record.allowed_lengths.include?(shifts) end
process_ai_variants(shifts)
click to toggle source
# File lib/gs1/barcode/segment.rb, line 73 def process_ai_variants(shifts) return unless can_shift_ai_data?(shifts) ai_variants << ai_variants.last.to_s + data.shift(shifts).join AI_CLASSES[ai_variants.last] end
shift_barcode_length()
click to toggle source
Record
has a fixed barcode length
# File lib/gs1/barcode/segment.rb, line 92 def shift_barcode_length return unless record.barcode_length shift_fixed_length(record.barcode_length) end
shift_fixed_length(shifts)
click to toggle source
# File lib/gs1/barcode/segment.rb, line 113 def shift_fixed_length(shifts) normalized_shifts = [data.size, shifts].min return unless can_shift_record_data?(normalized_shifts) data.shift(normalized_shifts).join end
shift_max_barcode_length()
click to toggle source
# File lib/gs1/barcode/segment.rb, line 109 def shift_max_barcode_length shift_fixed_length(record.barcode_max_length) end
shift_separator_length()
click to toggle source
Record
has a variable barcode length
# File lib/gs1/barcode/segment.rb, line 99 def shift_separator_length separator_index = data.find_index(separator) return unless separator_index && separator_index <= record.barcode_max_length shift_fixed_length(separator_index).tap do data.shift # Shift separator character end end