Class: Barcode1DTools::Barcode1D

Inherits:
Object
  • Object
show all
Defined in:
lib/barcode1dtools.rb

Overview

Base class for all barcode classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#check_digitObject (readonly)

The check digit(s), if any - may be nil



185
186
187
# File 'lib/barcode1dtools.rb', line 185

def check_digit
  @check_digit
end

#encoded_stringObject (readonly)

The actual string that is encoded, including check digits



187
188
189
# File 'lib/barcode1dtools.rb', line 187

def encoded_string
  @encoded_string
end

#optionsObject (readonly)

Options, including defaults and those explicitly set



189
190
191
# File 'lib/barcode1dtools.rb', line 189

def options
  @options
end

#valueObject (readonly)

The value that is encoded



183
184
185
# File 'lib/barcode1dtools.rb', line 183

def value
  @value
end

Class Method Details

.bar_pair(options = {}) ⇒ Object

Get a bar pair from the options



220
221
222
# File 'lib/barcode1dtools.rb', line 220

def bar_pair(options = {})
  (options[:space_character] || '0').to_s + (options[:line_character] || '1').to_s
end

.bars_to_rle(bar_str, options = {}) ⇒ Object

Generate rle pattern from bar string



200
201
202
# File 'lib/barcode1dtools.rb', line 200

def bars_to_rle(bar_str, options = {})
  bar_str.scan(/(.)(\1*)/).collect { |char,rest| 1+rest.length }.join
end

.rle_to_bars(rle_str, options = {}) ⇒ Object

Generate bar pattern string from rle string



194
195
196
197
# File 'lib/barcode1dtools.rb', line 194

def rle_to_bars(rle_str, options = {})
  str=0
  rle_str.split('').inject('') { |a,c| str = 1 - str; a + (str.to_s * c.to_i) }.tr('01', bar_pair(options))
end

.rle_to_wn(rle_str, options = {}) ⇒ Object

Generate wn pattern from rle string



210
211
212
# File 'lib/barcode1dtools.rb', line 210

def rle_to_wn(rle_str, options = {})
  rle_str.tr('123456789', 'nwwwwwwww').tr('wn', wn_pair(options))
end

.wn_pair(options = {}) ⇒ Object

Get an “wn” pair from the options



215
216
217
# File 'lib/barcode1dtools.rb', line 215

def wn_pair(options = {})
  (options[:w_character] || 'w') + (options[:n_character] || 'n')
end

.wn_to_rle(wn_str, options = {}) ⇒ Object

Generate rle pattern from wn string



205
206
207
# File 'lib/barcode1dtools.rb', line 205

def wn_to_rle(wn_str, options = {})
  wn_str.tr(wn_pair(options), (options[:wn_ratio] || 2).to_s + '1')
end