Class: Barcode::FormatHandlers::BaseRegExBarcode

Inherits:
Object
  • Object
show all
Includes:
Ean13Incompatible
Defined in:
app/models/barcode/format_handlers.rb

Overview

A basic class for barodes that can be validated and decomposed by simple regular expressions Classes that inherit from this should define a regular expression with optional names matchers for prefix, number and suffix. This regex should be assigned to self.format

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ean13Incompatible

#ean13_barcode, #ean13_barcode?

Constructor Details

#initialize(barcode) ⇒ BaseRegExBarcode

Returns a new instance of BaseRegExBarcode.



109
110
111
112
# File 'app/models/barcode/format_handlers.rb', line 109

def initialize(barcode)
  @human_barcode = barcode
  @matches = format.match(@human_barcode)
end

Instance Attribute Details

#human_barcodeObject (readonly) Also known as: machine_barcode, serialize_barcode

Returns the value of attribute human_barcode.



105
106
107
# File 'app/models/barcode/format_handlers.rb', line 105

def human_barcode
  @human_barcode
end

Instance Method Details

#=~(other) ⇒ Object



150
151
152
# File 'app/models/barcode/format_handlers.rb', line 150

def =~(other)
  human_barcode == other
end

#barcode_prefixObject



114
115
116
# File 'app/models/barcode/format_handlers.rb', line 114

def barcode_prefix
  @matches[:prefix] if @matches&.names&.include?('prefix')
end

#childObject



126
127
128
# File 'app/models/barcode/format_handlers.rb', line 126

def child
  @matches[:child].to_i if @matches&.names&.include?('child') && !@matches[:child].nil?
end

#code128_barcodeObject



142
143
144
# File 'app/models/barcode/format_handlers.rb', line 142

def code128_barcode
  human_barcode if code128_barcode?
end

#code128_barcode?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/barcode/format_handlers.rb', line 130

def code128_barcode?
  /\A[[:ascii:]]+\z/.match?(@human_barcode)
end

#code39_barcodeObject



146
147
148
# File 'app/models/barcode/format_handlers.rb', line 146

def code39_barcode
  human_barcode if code39_barcode?
end

#code39_barcode?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'app/models/barcode/format_handlers.rb', line 134

def code39_barcode?
  %r{\A[A-Z0-9 \-.$/+%]+\z}.match?(@human_barcode)
end

#numberObject



118
119
120
# File 'app/models/barcode/format_handlers.rb', line 118

def number
  @matches[:number].to_i if @matches&.names&.include?('number')
end

#suffixObject



122
123
124
# File 'app/models/barcode/format_handlers.rb', line 122

def suffix
  @matches[:suffix] if @matches&.names&.include?('suffix')
end

#valid?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'app/models/barcode/format_handlers.rb', line 138

def valid?
  format.match? @human_barcode
end