Class: Barby::EAN13
- Defined in:
- lib/barby/barcode/ean_13.rb
Overview
EAN-13, aka UPC-A, barcodes are the ones you can see at your local supermarket, in your house and, well, everywhere..
To use this for a UPC barcode, just add a 0 to the front
Constant Summary collapse
- LEFT_ENCODINGS_ODD =
{ 0 => '0001101', 1 => '0011001', 2 => '0010011', 3 => '0111101', 4 => '0100011', 5 => '0110001', 6 => '0101111', 7 => '0111011', 8 => '0110111', 9 => '0001011' }
- LEFT_ENCODINGS_EVEN =
{ 0 => '0100111', 1 => '0110011', 2 => '0011011', 3 => '0100001', 4 => '0011101', 5 => '0111001', 6 => '0000101', 7 => '0010001', 8 => '0001001', 9 => '0010111' }
- RIGHT_ENCODINGS =
{ 0 => '1110010', 1 => '1100110', 2 => '1101100', 3 => '1000010', 4 => '1011100', 5 => '1001110', 6 => '1010000', 7 => '1000100', 8 => '1001000', 9 => '1110100' }
- LEFT_PARITY_MAPS =
Describes whether the left-hand encoding should use LEFT_ENCODINGS_ODD or LEFT_ENCODINGS_EVEN, based on the first digit in the number system (and the barcode as a whole)
{ 0 => [:odd, :odd, :odd, :odd, :odd, :odd], #UPC-A 1 => [:odd, :odd, :even, :odd, :even, :even], 2 => [:odd, :odd, :even, :even, :odd, :even], 3 => [:odd, :odd, :even, :even, :even, :odd], 4 => [:odd, :even, :odd, :odd, :even, :even], 5 => [:odd, :even, :even, :odd, :odd, :even], 6 => [:odd, :even, :even, :even, :odd, :odd], 7 => [:odd, :even, :odd, :even, :odd, :even], 8 => [:odd, :even, :odd, :even, :even, :odd], 9 => [:odd, :even, :even, :odd, :even, :odd] }
- START =
These are the lines that “stick down” in the graphical representation
'101'
- CENTER =
'01010'
- STOP =
'101'
- FORMAT =
EAN-13 barcodes have 12 digits + check digit
/^\d{12}$/
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #center_encoding ⇒ Object
- #characters ⇒ Object
-
#checksum ⇒ Object
Mod10.
- #checksum_encoding ⇒ Object
- #data_with_checksum ⇒ Object
- #encoding ⇒ Object
-
#initialize(data) ⇒ EAN13
constructor
A new instance of EAN13.
- #left_encoding ⇒ Object
- #left_encodings ⇒ Object
-
#left_numbers ⇒ Object
Numbers that are encoded to the left of the center The first digit is not included.
-
#left_parity_map ⇒ Object
The parities to use for encoding left-hand numbers.
- #numbers ⇒ Object
- #numbers_with_checksum ⇒ Object
- #odd_and_even_numbers ⇒ Object
- #right_encoding ⇒ Object
- #right_encodings ⇒ Object
-
#right_numbers ⇒ Object
Numbers that are encoded to the right of the center The checksum is included here.
- #start_encoding ⇒ Object
- #stop_encoding ⇒ Object
- #to_s ⇒ Object
-
#upc? ⇒ Boolean
Is this a UPC-A barcode? UPC barcodes are EAN codes that start with 0.
- #valid? ⇒ Boolean
- #weighted_sum ⇒ Object
Methods inherited from Barcode
#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #two_dimensional?
Constructor Details
#initialize(data) ⇒ EAN13
Returns a new instance of EAN13.
59 60 61 62 |
# File 'lib/barby/barcode/ean_13.rb', line 59 def initialize(data) self.data = data raise ArgumentError, 'data not valid' unless valid? 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.
56 57 58 |
# File 'lib/barby/barcode/ean_13.rb', line 56 def data @data end |
Instance Method Details
#center_encoding ⇒ Object
167 168 169 |
# File 'lib/barby/barcode/ean_13.rb', line 167 def center_encoding CENTER end |
#characters ⇒ Object
65 66 67 |
# File 'lib/barby/barcode/ean_13.rb', line 65 def characters data.split(//) end |
#checksum ⇒ Object
Mod10
136 137 138 139 |
# File 'lib/barby/barcode/ean_13.rb', line 136 def checksum mod = weighted_sum % 10 mod.zero? ? 0 : 10-mod end |
#checksum_encoding ⇒ Object
141 142 143 |
# File 'lib/barby/barcode/ean_13.rb', line 141 def checksum_encoding RIGHT_ENCODINGS[checksum] end |
#data_with_checksum ⇒ Object
95 96 97 |
# File 'lib/barby/barcode/ean_13.rb', line 95 def data_with_checksum data + checksum.to_s end |
#encoding ⇒ Object
118 119 120 |
# File 'lib/barby/barcode/ean_13.rb', line 118 def encoding start_encoding+left_encoding+center_encoding+right_encoding+stop_encoding end |
#left_encoding ⇒ Object
110 111 112 |
# File 'lib/barby/barcode/ean_13.rb', line 110 def left_encoding left_encodings.join end |
#left_encodings ⇒ Object
100 101 102 103 104 |
# File 'lib/barby/barcode/ean_13.rb', line 100 def left_encodings left_parity_map.zip(left_numbers).map do |parity,number| parity == :odd ? LEFT_ENCODINGS_ODD[number] : LEFT_ENCODINGS_EVEN[number] end end |
#left_numbers ⇒ Object
Numbers that are encoded to the left of the center The first digit is not included
80 81 82 |
# File 'lib/barby/barcode/ean_13.rb', line 80 def left_numbers numbers[1,6] end |
#left_parity_map ⇒ Object
The parities to use for encoding left-hand numbers
124 125 126 |
# File 'lib/barby/barcode/ean_13.rb', line 124 def left_parity_map LEFT_PARITY_MAPS[numbers.first] end |
#numbers ⇒ Object
69 70 71 |
# File 'lib/barby/barcode/ean_13.rb', line 69 def numbers characters.map{|s| s.to_i } end |
#numbers_with_checksum ⇒ Object
90 91 92 |
# File 'lib/barby/barcode/ean_13.rb', line 90 def numbers_with_checksum numbers + [checksum] end |
#odd_and_even_numbers ⇒ Object
73 74 75 76 |
# File 'lib/barby/barcode/ean_13.rb', line 73 def odd_and_even_numbers alternater = false numbers.reverse.partition{ alternater = !alternater } end |
#right_encoding ⇒ Object
114 115 116 |
# File 'lib/barby/barcode/ean_13.rb', line 114 def right_encoding right_encodings.join end |
#right_encodings ⇒ Object
106 107 108 |
# File 'lib/barby/barcode/ean_13.rb', line 106 def right_encodings right_numbers.map{|n| RIGHT_ENCODINGS[n] } end |
#right_numbers ⇒ Object
Numbers that are encoded to the right of the center The checksum is included here
86 87 88 |
# File 'lib/barby/barcode/ean_13.rb', line 86 def right_numbers numbers_with_checksum[7,6] end |
#start_encoding ⇒ Object
163 164 165 |
# File 'lib/barby/barcode/ean_13.rb', line 163 def start_encoding START end |
#stop_encoding ⇒ Object
171 172 173 |
# File 'lib/barby/barcode/ean_13.rb', line 171 def stop_encoding STOP end |
#to_s ⇒ Object
151 152 153 |
# File 'lib/barby/barcode/ean_13.rb', line 151 def to_s data_with_checksum end |
#upc? ⇒ Boolean
Is this a UPC-A barcode? UPC barcodes are EAN codes that start with 0
158 159 160 |
# File 'lib/barby/barcode/ean_13.rb', line 158 def upc? numbers.first.zero? end |
#valid? ⇒ Boolean
146 147 148 |
# File 'lib/barby/barcode/ean_13.rb', line 146 def valid? data =~ FORMAT end |
#weighted_sum ⇒ Object
129 130 131 132 133 |
# File 'lib/barby/barcode/ean_13.rb', line 129 def weighted_sum odds, evens = odd_and_even_numbers odds.map!{|n| n * 3 } (odds+evens).inject(0){|s,n| s+n } end |