Class: Barby::UPCSupplemental
Constant Summary
collapse
- FORMAT =
/^\d\d\d\d\d$|^\d\d$/
- START =
'1011'
- SEPARATOR =
'01'
- ODD =
:odd
- EVEN =
:even
- PARITY_MAPS =
{
2 => {
0 => [ODD, ODD],
1 => [ODD, EVEN],
2 => [EVEN, ODD],
3 => [EVEN, EVEN]
},
5 => {
0 => [EVEN, EVEN, ODD, ODD, ODD],
1 => [EVEN, ODD, EVEN, ODD, ODD],
2 => [EVEN, ODD, ODD, EVEN, ODD],
3 => [EVEN, ODD, ODD, ODD, EVEN],
4 => [ODD, EVEN, EVEN, ODD, ODD],
5 => [ODD, ODD, EVEN, EVEN, ODD],
6 => [ODD, ODD, ODD, EVEN, EVEN],
7 => [ODD, EVEN, ODD, EVEN, ODD],
8 => [ODD, EVEN, ODD, ODD, EVEN],
9 => [ODD, ODD, EVEN, ODD, EVEN]
}
}
- ENCODINGS =
{
ODD => EAN13::LEFT_ENCODINGS_ODD,
EVEN => EAN13::LEFT_ENCODINGS_EVEN
}
- NO_PRICE =
The book doesn’t have a suggested retail price
new('90000')
- COMPLIMENTARY =
The book is complimentary (~free)
new('99991')
- USED =
The book is marked as used
new('99990')
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
Returns a new instance of UPCSupplemental.
46
47
48
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 46
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.
9
10
11
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 9
def data
@data
end
|
Instance Method Details
#characters ⇒ Object
64
65
66
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 64
def characters
data.split(//)
end
|
#checksum ⇒ Object
Checksum is different for 2 and 5 digits 2-digits don’t really have a checksum, just a remainder to look up the parity
95
96
97
98
99
100
101
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 95
def checksum
if two_digit?
data.to_i % 4
elsif five_digit?
(odd_sum + even_sum) % 10
end
end
|
#digits ⇒ Object
68
69
70
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 68
def digits
characters.map{|c| c.to_i }
end
|
#encoded_characters ⇒ Object
110
111
112
113
114
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 110
def encoded_characters
parity_map.zip(digits).map do |parity, digit|
ENCODINGS[parity][digit]
end
end
|
#encoding ⇒ Object
117
118
119
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 117
def encoding
START + encoded_characters.join(SEPARATOR)
end
|
#even_digits ⇒ Object
79
80
81
82
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 79
def even_digits
alternater = true
digits.reverse.select{ alternater = !alternater }
end
|
#even_sum ⇒ Object
88
89
90
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 88
def even_sum
even_digits.inject(0){|s,d| s + d * 9 }
end
|
#five_digit? ⇒ Boolean
59
60
61
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 59
def five_digit?
size == 5
end
|
#odd_digits ⇒ Object
Odd and even methods are only useful for 5 digits
74
75
76
77
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 74
def odd_digits
alternater = false
digits.reverse.select{ alternater = !alternater }
end
|
#odd_sum ⇒ Object
84
85
86
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 84
def odd_sum
odd_digits.inject(0){|s,d| s + d * 3 }
end
|
#parity_map ⇒ Object
Parity maps are different for 2 and 5 digits
105
106
107
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 105
def parity_map
PARITY_MAPS[size][checksum]
end
|
#size ⇒ Object
51
52
53
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 51
def size
data.size
end
|
#to_s ⇒ Object
127
128
129
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 127
def to_s
data
end
|
#two_digit? ⇒ Boolean
55
56
57
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 55
def two_digit?
size == 2
end
|
#valid? ⇒ Boolean
122
123
124
|
# File 'lib/barby/barcode/upc_supplemental.rb', line 122
def valid?
data =~ FORMAT
end
|