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.



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

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.



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

def human_barcode
  @human_barcode
end

Instance Method Details

#=~(other) ⇒ Object



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

def =~(other)
  human_barcode == other
end

#barcode_prefixObject



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

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

#childObject



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

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

#code128_barcodeObject



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

def code128_barcode
  human_barcode if code128_barcode?
end

#code128_barcode?Boolean

Returns:

  • (Boolean)


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

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

#code39_barcodeObject



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

def code39_barcode
  human_barcode if code39_barcode?
end

#code39_barcode?Boolean

Returns:

  • (Boolean)


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

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

#numberObject



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

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

#suffixObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  format.match? @human_barcode
end