Class: Barby::Code25
- 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
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
-
#data ⇒ Object
Returns the value of attribute data.
-
#include_checksum ⇒ Object
Returns the value of attribute include_checksum.
-
#narrow_width ⇒ Object
The width of a narrow bar in xdims.
-
#space_width ⇒ Object
The width of the space between the bars in xdims By default the same width as a narrow bar.
-
#wide_width ⇒ Object
The width of a wide bar in xdims By default three times as wide as a narrow bar.
Instance Method Summary collapse
- #characters ⇒ Object
- #characters_with_checksum ⇒ Object
-
#checksum ⇒ Object
Mod10.
- #checksum_encoding ⇒ Object
- #data_encoding ⇒ Object
- #data_encoding_with_checksum ⇒ Object
- #digit_encodings ⇒ Object (also: #character_encodings)
- #digit_encodings_with_checksum ⇒ Object (also: #character_encodings_with_checksum)
- #digits ⇒ Object
- #digits_with_checksum ⇒ Object
- #encoding ⇒ Object
-
#encoding_for(digit) ⇒ Object
Returns the encoding for a single digit.
-
#encoding_for_bars(*bars) ⇒ Object
Generate encoding for an array of W,N.
- #encoding_for_bars_without_end_space(*a) ⇒ Object
- #even_and_odd_digits ⇒ Object
-
#include_checksum? ⇒ Boolean
2 of 5 doesn’t require a checksum, but you can include a Mod10 checksum by setting
include_checksum
to true. -
#initialize(data) ⇒ Code25
constructor
A new instance of Code25.
- #narrow_encoding ⇒ Object
- #space_encoding ⇒ Object
- #start_encoding ⇒ Object
- #stop_encoding ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #wide_encoding ⇒ Object
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
#data ⇒ Object
Returns the value of attribute data.
33 34 35 |
# File 'lib/barby/barcode/code_25.rb', line 33 def data @data end |
#include_checksum ⇒ Object
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_width ⇒ Object
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_width ⇒ Object
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_width ⇒ Object
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
#characters ⇒ Object
55 56 57 |
# File 'lib/barby/barcode/code_25.rb', line 55 def characters data.split(//) end |
#characters_with_checksum ⇒ Object
59 60 61 |
# File 'lib/barby/barcode/code_25.rb', line 59 def characters_with_checksum characters.push(checksum.to_s) end |
#checksum ⇒ Object
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_encoding ⇒ Object
116 117 118 |
# File 'lib/barby/barcode/code_25.rb', line 116 def checksum_encoding encoding_for(checksum) end |
#data_encoding ⇒ Object
42 43 44 |
# File 'lib/barby/barcode/code_25.rb', line 42 def data_encoding digit_encodings.join end |
#data_encoding_with_checksum ⇒ Object
46 47 48 |
# File 'lib/barby/barcode/code_25.rb', line 46 def data_encoding_with_checksum digit_encodings_with_checksum.join end |
#digit_encodings ⇒ Object 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_checksum ⇒ Object 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 |
#digits ⇒ Object
63 64 65 |
# File 'lib/barby/barcode/code_25.rb', line 63 def digits characters.map{|c| c.to_i } end |
#digits_with_checksum ⇒ Object
67 68 69 |
# File 'lib/barby/barcode/code_25.rb', line 67 def digits_with_checksum digits.push(checksum) end |
#encoding ⇒ Object
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) (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 (*) wide, narrow, space = wide_encoding, narrow_encoding, space_encoding .flatten.inject '' do |enc,| enc + ( == 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 (*a) (*a).gsub(/0+$/, '') end |
#even_and_odd_digits ⇒ Object
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
144 145 146 |
# File 'lib/barby/barcode/code_25.rb', line 144 def include_checksum? include_checksum end |
#narrow_encoding ⇒ Object
163 164 165 |
# File 'lib/barby/barcode/code_25.rb', line 163 def narrow_encoding '1' * narrow_width end |
#space_encoding ⇒ Object
171 172 173 |
# File 'lib/barby/barcode/code_25.rb', line 171 def space_encoding '0' * space_width end |
#start_encoding ⇒ Object
154 155 156 |
# File 'lib/barby/barcode/code_25.rb', line 154 def start_encoding (START_ENCODING) end |
#stop_encoding ⇒ Object
158 159 160 |
# File 'lib/barby/barcode/code_25.rb', line 158 def stop_encoding (STOP_ENCODING) end |
#to_s ⇒ Object
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
176 177 178 |
# File 'lib/barby/barcode/code_25.rb', line 176 def valid? data =~ /^[0-9]*$/ end |
#wide_encoding ⇒ Object
167 168 169 |
# File 'lib/barby/barcode/code_25.rb', line 167 def wide_encoding '1' * wide_width end |