Class: Barcode1DTools::Barcode1D

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#check_digitObject (readonly)

Returns the value of attribute check_digit.



73
74
75
# File 'lib/barcode1dtools.rb', line 73

def check_digit
  @check_digit
end

#encoded_stringObject (readonly)

Returns the value of attribute encoded_string.



75
76
77
# File 'lib/barcode1dtools.rb', line 75

def encoded_string
  @encoded_string
end

#optionsObject (readonly)

Returns the value of attribute options.



76
77
78
# File 'lib/barcode1dtools.rb', line 76

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



74
75
76
# File 'lib/barcode1dtools.rb', line 74

def value
  @value
end

Class Method Details

.bar_pair(options = {}) ⇒ Object

Get a bar pair from the options



107
108
109
# File 'lib/barcode1dtools.rb', line 107

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



87
88
89
# File 'lib/barcode1dtools.rb', line 87

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



81
82
83
84
# File 'lib/barcode1dtools.rb', line 81

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



97
98
99
# File 'lib/barcode1dtools.rb', line 97

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



102
103
104
# File 'lib/barcode1dtools.rb', line 102

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



92
93
94
# File 'lib/barcode1dtools.rb', line 92

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