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

Direct Known Subclasses

AkerBarcode, Cgap, External, Fluidigm, FluidxBarcode, Infinium

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


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

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


102
103
104
# File 'app/models/barcode/format_handlers.rb', line 102

def human_barcode
  @human_barcode
end

Instance Method Details

#=~(other) ⇒ Object


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

def =~(other)
  human_barcode == other
end

#barcode_prefixObject


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

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

#code128_barcodeObject


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

def code128_barcode
  human_barcode if code128_barcode?
end

#code128_barcode?Boolean

Returns:

  • (Boolean)

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

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

#code39_barcodeObject


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

def code39_barcode
  human_barcode if code39_barcode?
end

#code39_barcode?Boolean

Returns:

  • (Boolean)

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

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

#numberObject


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

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

#suffixObject


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

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

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  format.match? @human_barcode
end