Class: Barby::Code25

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

Overview

Standard/Industrial 2 of 5, non-interleaved

Checksum not included by default, to include it, set include_checksum = true

Direct Known Subclasses

Code25IATA, Code25Interleaved

Constant Summary collapse

WIDE =
W = true
NARROW =
N = false
START_ENCODING =
[W,W,N]
STOP_ENCODING =
[W,N,W]
ENCODINGS =
{
  0 => [N,N,W,W,N],
  1 => [W,N,N,N,W],
  2 => [N,W,N,N,W],
  3 => [W,W,N,N,N],
  4 => [N,N,W,N,W],
  5 => [W,N,W,N,N],
  6 => [N,W,W,N,N],
  7 => [N,N,N,W,W],
  8 => [W,N,N,W,N],
  9 => [N,W,N,W,N]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Barcode

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

Constructor Details

#initialize(data) ⇒ Code25

Returns a new instance of Code25.



36
37
38
39
# File 'lib/barby/barcode/code_25.rb', line 36

def initialize(data)
  self.data = data
  @narrow_width, @wide_width, @space_width = nil
end

Dynamic Method Handling

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



33
34
35
# File 'lib/barby/barcode/code_25.rb', line 33

def data
  @data
end

#include_checksumObject

Returns the value of attribute include_checksum.



31
32
33
# File 'lib/barby/barcode/code_25.rb', line 31

def include_checksum
  @include_checksum
end

#narrow_widthObject

The width of a narrow bar in xdims



122
123
124
# File 'lib/barby/barcode/code_25.rb', line 122

def narrow_width
  @narrow_width || 1
end

#space_widthObject

The width of the space between the bars in xdims By default the same width as a narrow bar

A space serves only as a separator for the bars, there is no encoded meaning in them



137
138
139
# File 'lib/barby/barcode/code_25.rb', line 137

def space_width
  @space_width || narrow_width
end

#wide_widthObject

The width of a wide bar in xdims By default three times as wide as a narrow bar



128
129
130
# File 'lib/barby/barcode/code_25.rb', line 128

def wide_width
  @wide_width || narrow_width*3
end

Instance Method Details

#charactersObject



55
56
57
# File 'lib/barby/barcode/code_25.rb', line 55

def characters
  data.split(//)
end

#characters_with_checksumObject



59
60
61
# File 'lib/barby/barcode/code_25.rb', line 59

def characters_with_checksum
  characters.push(checksum.to_s)
end

#checksumObject

Mod10



109
110
111
112
113
114
# File 'lib/barby/barcode/code_25.rb', line 109

def checksum
  evens, odds = even_and_odd_digits
  sum = odds.inject(0){|s,d| s + d } + evens.inject(0){|s,d| s + (d*3) }
  sum %= 10
  sum.zero? ? 0 : 10-sum
end

#checksum_encodingObject



116
117
118
# File 'lib/barby/barcode/code_25.rb', line 116

def checksum_encoding
  encoding_for(checksum)
end

#data_encodingObject



42
43
44
# File 'lib/barby/barcode/code_25.rb', line 42

def data_encoding
  digit_encodings.join
end

#data_encoding_with_checksumObject



46
47
48
# File 'lib/barby/barcode/code_25.rb', line 46

def data_encoding_with_checksum
  digit_encodings_with_checksum.join
end

#digit_encodingsObject Also known as: character_encodings



77
78
79
80
# File 'lib/barby/barcode/code_25.rb', line 77

def digit_encodings
  raise_invalid unless valid?
  digits.map{|d| encoding_for(d) }
end

#digit_encodings_with_checksumObject Also known as: character_encodings_with_checksum



83
84
85
86
# File 'lib/barby/barcode/code_25.rb', line 83

def digit_encodings_with_checksum
  raise_invalid unless valid?
  digits_with_checksum.map{|d| encoding_for(d) }
end

#digitsObject



63
64
65
# File 'lib/barby/barcode/code_25.rb', line 63

def digits
  characters.map{|c| c.to_i }
end

#digits_with_checksumObject



67
68
69
# File 'lib/barby/barcode/code_25.rb', line 67

def digits_with_checksum
  digits.push(checksum)
end

#encodingObject



50
51
52
# File 'lib/barby/barcode/code_25.rb', line 50

def encoding
  start_encoding+(include_checksum? ? data_encoding_with_checksum : data_encoding)+stop_encoding
end

#encoding_for(digit) ⇒ Object

Returns the encoding for a single digit



91
92
93
# File 'lib/barby/barcode/code_25.rb', line 91

def encoding_for(digit)
  encoding_for_bars(ENCODINGS[digit])
end

#encoding_for_bars(*bars) ⇒ Object

Generate encoding for an array of W,N



96
97
98
99
100
101
# File 'lib/barby/barcode/code_25.rb', line 96

def encoding_for_bars(*bars)
  wide, narrow, space = wide_encoding, narrow_encoding, space_encoding
  bars.flatten.inject '' do |enc,bar|
    enc + (bar == WIDE ? wide : narrow) + space
  end
end

#encoding_for_bars_without_end_space(*a) ⇒ Object



103
104
105
# File 'lib/barby/barcode/code_25.rb', line 103

def encoding_for_bars_without_end_space(*a)
  encoding_for_bars(*a).gsub(/0+$/, '')
end

#even_and_odd_digitsObject



71
72
73
74
# File 'lib/barby/barcode/code_25.rb', line 71

def even_and_odd_digits
  alternater = false
  digits.reverse.partition{ alternater = !alternater }
end

#include_checksum?Boolean

2 of 5 doesn’t require a checksum, but you can include a Mod10 checksum by setting include_checksum to true

Returns:

  • (Boolean)


144
145
146
# File 'lib/barby/barcode/code_25.rb', line 144

def include_checksum?
  include_checksum
end

#narrow_encodingObject



163
164
165
# File 'lib/barby/barcode/code_25.rb', line 163

def narrow_encoding
  '1' * narrow_width
end

#space_encodingObject



171
172
173
# File 'lib/barby/barcode/code_25.rb', line 171

def space_encoding
  '0' * space_width
end

#start_encodingObject



154
155
156
# File 'lib/barby/barcode/code_25.rb', line 154

def start_encoding
  encoding_for_bars(START_ENCODING)
end

#stop_encodingObject



158
159
160
# File 'lib/barby/barcode/code_25.rb', line 158

def stop_encoding
  encoding_for_bars_without_end_space(STOP_ENCODING)
end

#to_sObject



181
182
183
# File 'lib/barby/barcode/code_25.rb', line 181

def to_s
  (include_checksum? ? characters_with_checksum : characters).join
end

#valid?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/barby/barcode/code_25.rb', line 176

def valid?
  data =~ /^[0-9]*$/
end

#wide_encodingObject



167
168
169
# File 'lib/barby/barcode/code_25.rb', line 167

def wide_encoding
  '1' * wide_width
end