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.



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

def initialize(data)
  self.data = data
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.



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

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



119
120
121
# File 'lib/barby/barcode/code_25.rb', line 119

def narrow_width
  @narrow_width
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



134
135
136
# File 'lib/barby/barcode/code_25.rb', line 134

def space_width
  @space_width
end

#wide_widthObject

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



125
126
127
# File 'lib/barby/barcode/code_25.rb', line 125

def wide_width
  @wide_width
end

Instance Method Details

#charactersObject



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

def characters
  data.split(//)
end

#characters_with_checksumObject



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

def characters_with_checksum
  characters.push(checksum.to_s)
end

#checksumObject

Mod10



106
107
108
109
110
111
# File 'lib/barby/barcode/code_25.rb', line 106

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

#checksum_encodingObject



113
114
115
# File 'lib/barby/barcode/code_25.rb', line 113

def checksum_encoding
  encoding_for(checksum)
end

#data_encodingObject



39
40
41
# File 'lib/barby/barcode/code_25.rb', line 39

def data_encoding
  digit_encodings.join
end

#data_encoding_with_checksumObject



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

def data_encoding_with_checksum
  digit_encodings_with_checksum.join
end

#digit_encodingsObject Also known as: character_encodings



74
75
76
77
# File 'lib/barby/barcode/code_25.rb', line 74

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



80
81
82
83
# File 'lib/barby/barcode/code_25.rb', line 80

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

#digitsObject



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

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

#digits_with_checksumObject



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

def digits_with_checksum
  digits.push(checksum)
end

#encodingObject



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

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



88
89
90
# File 'lib/barby/barcode/code_25.rb', line 88

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

#encoding_for_bars(*bars) ⇒ Object

Generate encoding for an array of W,N



93
94
95
96
97
98
# File 'lib/barby/barcode/code_25.rb', line 93

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



100
101
102
# File 'lib/barby/barcode/code_25.rb', line 100

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

#even_and_odd_digitsObject



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

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)


141
142
143
# File 'lib/barby/barcode/code_25.rb', line 141

def include_checksum?
  include_checksum
end

#narrow_encodingObject



160
161
162
# File 'lib/barby/barcode/code_25.rb', line 160

def narrow_encoding
  '1' * narrow_width
end

#space_encodingObject



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

def space_encoding
  '0' * space_width
end

#start_encodingObject



151
152
153
# File 'lib/barby/barcode/code_25.rb', line 151

def start_encoding
  encoding_for_bars(START_ENCODING)
end

#stop_encodingObject



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

def stop_encoding
  encoding_for_bars_without_end_space(STOP_ENCODING)
end

#to_sObject



178
179
180
# File 'lib/barby/barcode/code_25.rb', line 178

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

#valid?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/barby/barcode/code_25.rb', line 173

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

#wide_encodingObject



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

def wide_encoding
  '1' * wide_width
end