Class: Barby::Codabar

Inherits:
Barcode1D show all
Defined in:
lib/barby/barcode/codabar.rb

Overview

Constant Summary collapse

BLACK_NARROW_WIDTH =
1
WHITE_NARROW_WIDTH =
2
WIDE_WIDTH_RATE =
3
SPACING =

must be equal to or wider than white narrow width

5
CHARACTERS =
"0123456789-$:/.+ABCD".freeze
BINARY_EXPRESSION =

even: black, odd: white 0: narrow, 1: wide

[
  "0000011", # 0
  "0000110", # 1
  "0001001", # 2
  "1100000", # 3
  "0010010", # 4
  "1000010", # 5
  "0100001", # 6
  "0100100", # 7
  "0110000", # 8
  "1001000", # 9
  "0001100", # -
  "0011000", # $
  "1000101", # :
  "1010001", # /
  "1010100", # .
  "0010101", # +
  "0011010", # A
  "0101001", # B
  "0001011", # C
  "0001110", # D
].each(&:freeze)
CHARACTER_TO_BINARY =
Hash[CHARACTERS.chars.zip(BINARY_EXPRESSION)].freeze
FORMAT =
/\A[ABCD][0123456789\-\$:\/\.\+]+[ABCD]\z/.freeze
ONE =
"1".freeze
ZERO =
"0".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Barcode

#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #to_s, #two_dimensional?

Constructor Details

#initialize(data) ⇒ Codabar

Returns a new instance of Codabar.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
# File 'lib/barby/barcode/codabar.rb', line 42

def initialize(data)
  self.data = data
  raise ArgumentError, 'data not valid' unless valid?

  self.black_narrow_width = BLACK_NARROW_WIDTH
  self.white_narrow_width = WHITE_NARROW_WIDTH
  self.wide_width_rate = WIDE_WIDTH_RATE
  self.spacing = SPACING
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Barby::Barcode

Instance Attribute Details

#black_narrow_widthObject

Returns the value of attribute black_narrow_width.



40
41
42
# File 'lib/barby/barcode/codabar.rb', line 40

def black_narrow_width
  @black_narrow_width
end

#dataObject

Returns the value of attribute data.



40
41
42
# File 'lib/barby/barcode/codabar.rb', line 40

def data
  @data
end

#spacingObject

Returns the value of attribute spacing.



40
41
42
# File 'lib/barby/barcode/codabar.rb', line 40

def spacing
  @spacing
end

#white_narrow_widthObject

Returns the value of attribute white_narrow_width.



40
41
42
# File 'lib/barby/barcode/codabar.rb', line 40

def white_narrow_width
  @white_narrow_width
end

#wide_width_rateObject

Returns the value of attribute wide_width_rate.



40
41
42
# File 'lib/barby/barcode/codabar.rb', line 40

def wide_width_rate
  @wide_width_rate
end

Instance Method Details

#encodingObject



52
53
54
# File 'lib/barby/barcode/codabar.rb', line 52

def encoding
  data.chars.map{|c| binary_to_bars(CHARACTER_TO_BINARY[c]) }.join(ZERO * spacing)
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/barby/barcode/codabar.rb', line 56

def valid?
  data =~ FORMAT
end