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.
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
#data ⇒ Object
Returns the value of attribute data.
31 32 33 |
# File 'lib/barby/barcode/code_25.rb', line 31 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
119 120 121 |
# File 'lib/barby/barcode/code_25.rb', line 119 def narrow_width @narrow_width 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
134 135 136 |
# File 'lib/barby/barcode/code_25.rb', line 134 def space_width @space_width end |
#wide_width ⇒ Object
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
#characters ⇒ Object
52 53 54 |
# File 'lib/barby/barcode/code_25.rb', line 52 def characters data.split(//) end |
#characters_with_checksum ⇒ Object
56 57 58 |
# File 'lib/barby/barcode/code_25.rb', line 56 def characters_with_checksum characters.push(checksum.to_s) end |
#checksum ⇒ Object
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_encoding ⇒ Object
113 114 115 |
# File 'lib/barby/barcode/code_25.rb', line 113 def checksum_encoding encoding_for(checksum) end |
#data_encoding ⇒ Object
39 40 41 |
# File 'lib/barby/barcode/code_25.rb', line 39 def data_encoding digit_encodings.join end |
#data_encoding_with_checksum ⇒ Object
43 44 45 |
# File 'lib/barby/barcode/code_25.rb', line 43 def data_encoding_with_checksum digit_encodings_with_checksum.join end |
#digit_encodings ⇒ Object 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_checksum ⇒ Object 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 |
#digits ⇒ Object
60 61 62 |
# File 'lib/barby/barcode/code_25.rb', line 60 def digits characters.map{|c| c.to_i } end |
#digits_with_checksum ⇒ Object
64 65 66 |
# File 'lib/barby/barcode/code_25.rb', line 64 def digits_with_checksum digits.push(checksum) end |
#encoding ⇒ Object
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) (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 (*) 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
100 101 102 |
# File 'lib/barby/barcode/code_25.rb', line 100 def (*a) (*a).gsub(/0+$/, '') end |
#even_and_odd_digits ⇒ Object
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
141 142 143 |
# File 'lib/barby/barcode/code_25.rb', line 141 def include_checksum? include_checksum end |
#narrow_encoding ⇒ Object
160 161 162 |
# File 'lib/barby/barcode/code_25.rb', line 160 def narrow_encoding '1' * narrow_width end |
#space_encoding ⇒ Object
168 169 170 |
# File 'lib/barby/barcode/code_25.rb', line 168 def space_encoding '0' * space_width end |
#start_encoding ⇒ Object
151 152 153 |
# File 'lib/barby/barcode/code_25.rb', line 151 def start_encoding (START_ENCODING) end |
#stop_encoding ⇒ Object
155 156 157 |
# File 'lib/barby/barcode/code_25.rb', line 155 def stop_encoding (STOP_ENCODING) end |
#to_s ⇒ Object
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
173 174 175 |
# File 'lib/barby/barcode/code_25.rb', line 173 def valid? data =~ /^[0-9]*$/ end |
#wide_encoding ⇒ Object
164 165 166 |
# File 'lib/barby/barcode/code_25.rb', line 164 def wide_encoding '1' * wide_width end |