barcode1dtools 1.0.1.0 → 1.0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,11 +19,12 @@ module Barcode1DTools
19
19
  # Matrix 2 of 5 is low-density and limited. It should not be
20
20
  # used in any new applications.
21
21
  #
22
- # val = "3423"
23
- # bc = Barcode1DTools::Matrix2of5.new(val)
24
- # pattern = bc.bars
25
- # rle_pattern = bc.rle
26
- # width = bc.width
22
+ # == Example
23
+ # val = "3423"
24
+ # bc = Barcode1DTools::Matrix2of5.new(val)
25
+ # pattern = bc.bars
26
+ # rle_pattern = bc.rle
27
+ # width = bc.width
27
28
  #
28
29
  # The object created is immutable.
29
30
  #
@@ -34,27 +35,28 @@ module Barcode1DTools
34
35
  # Matrix2of5 characters consist of 3 bars and 2 spaces, with a narrow
35
36
  # space between them. 2 of the bars/spaces in each symbol are wide.
36
37
  #
38
+ # == Formats
37
39
  # There are three formats for the returned pattern:
38
40
  #
39
- # bars - 1s and 0s specifying black lines and white spaces. Actual
40
- # characters can be changed from "1" and 0" with options
41
- # :line_character and :space_character.
41
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
42
+ # characters can be changed from "1" and 0" with options
43
+ # :line_character and :space_character.
42
44
  #
43
- # rle - Run-length-encoded version of the pattern. The first
44
- # number is always a black line, with subsequent digits
45
- # alternating between spaces and lines. The digits specify
46
- # the width of each line or space.
45
+ # *rle* - Run-length-encoded version of the pattern. The first
46
+ # number is always a black line, with subsequent digits
47
+ # alternating between spaces and lines. The digits specify
48
+ # the width of each line or space.
47
49
  #
48
- # wn - The native format for this barcode type. The string
49
- # consists of a series of "w" and "n" characters. The first
50
- # item is always a black line, with subsequent characters
51
- # alternating between spaces and lines. A "wide" item
52
- # is twice the width of a "narrow" item.
50
+ # *wn* - The native format for this barcode type. The string
51
+ # consists of a series of "w" and "n" characters. The first
52
+ # item is always a black line, with subsequent characters
53
+ # alternating between spaces and lines. A "wide" item
54
+ # is twice the width of a "narrow" item.
53
55
  #
54
56
  # The "width" method will tell you the total end-to-end width, in
55
57
  # units, of the entire barcode.
56
58
  #
57
- #== Rendering
59
+ # == Rendering
58
60
  #
59
61
  # The standard w/n ratio seems to be 2:1. There seem to be no real
60
62
  # standards for display.
@@ -82,7 +84,9 @@ module Barcode1DTools
82
84
  '9'=> {'val'=>9 ,'wn'=>'nwnwn'}
83
85
  }
84
86
 
87
+ # Left guard pattern
85
88
  GUARD_PATTERN_LEFT_WN = 'wnnnn'
89
+ # Right guard pattern
86
90
  GUARD_PATTERN_RIGHT_WN = 'wnnnn'
87
91
 
88
92
  DEFAULT_OPTIONS = {
@@ -94,11 +98,13 @@ module Barcode1DTools
94
98
  }
95
99
 
96
100
  class << self
97
- # Matrix2of5 can encode digits
101
+ # Returns true if the value is encodable in this symbology.
102
+ # Matrix2of5 can encode digits.
98
103
  def can_encode?(value)
99
104
  value.to_s =~ /\A[0-9]+\z/
100
105
  end
101
106
 
107
+ # Generates a check digit for a given value using the Luhn algorithm.
102
108
  def generate_check_digit_for(value)
103
109
  raise UnencodableCharactersError unless self.can_encode?(value)
104
110
  mult = 3
@@ -106,13 +112,15 @@ module Barcode1DTools
106
112
  (10 - (value % 10)) % 10
107
113
  end
108
114
 
115
+ # Returns true if the final digit if the given string is a valid
116
+ # check digit for the rest of the string.
109
117
  def validate_check_digit_for(value)
110
118
  raise UnencodableCharactersError unless self.can_encode?(value)
111
119
  md = value.match(/^(\d+?)(\d)$/)
112
120
  self.generate_check_digit_for(md[1]) == md[2].to_i
113
121
  end
114
122
 
115
- # Decode a string in rle format. This will return a Matrix2of5
123
+ # Decode a string in rle or w/n format. This will return a Matrix2of5
116
124
  # object.
117
125
  def decode(str, options = {})
118
126
  if str =~ /[^1-3]/ && str =~ /[^wn]/
@@ -163,6 +171,7 @@ module Barcode1DTools
163
171
 
164
172
  end
165
173
 
174
+ # Create a new Matrix2of5 object with a given value.
166
175
  # Options are :line_character, :space_character, :w_character,
167
176
  # :n_character, :checksum_included, and :skip_checksum.
168
177
  def initialize(value, options = {})
@@ -190,22 +199,22 @@ module Barcode1DTools
190
199
  end
191
200
  end
192
201
 
193
- # Returns a string of "w" or "n" ("wide" and "narrow")
202
+ # Returns a string of "w" or "n" ("wide" and "narrow").
194
203
  def wn
195
204
  @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
196
205
  end
197
206
 
198
- # returns a run-length-encoded string representation
207
+ # Returns a run-length-encoded string representation.
199
208
  def rle
200
209
  @rle ||= self.class.wn_to_rle(self.wn, @options)
201
210
  end
202
211
 
203
- # returns 1s and 0s (for "black" and "white")
212
+ # Returns 1s and 0s (for "black" and "white").
204
213
  def bars
205
214
  @bars ||= self.class.rle_to_bars(self.rle, @options)
206
215
  end
207
216
 
208
- # returns the total unit width of the bar code
217
+ # Returns the total unit width of the bar code.
209
218
  def width
210
219
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
211
220
  end
@@ -21,11 +21,12 @@ module Barcode1DTools
21
21
  # MSI is a terrible symbology in modern terms and should
22
22
  # not be used in any new applications.
23
23
  #
24
- # val = "2898289238"
25
- # bc = Barcode1DTools::MSI.new(val)
26
- # pattern = bc.bars
27
- # rle_pattern = bc.rle
28
- # width = bc.width
24
+ # == Example
25
+ # val = "2898289238"
26
+ # bc = Barcode1DTools::MSI.new(val)
27
+ # pattern = bc.bars
28
+ # rle_pattern = bc.rle
29
+ # width = bc.width
29
30
  #
30
31
  # The object created is immutable.
31
32
  #
@@ -40,27 +41,28 @@ module Barcode1DTools
40
41
  # The bits are ordered descending, so 9 is 1001 binary,
41
42
  # "wn nw nw wn" in w/n format.
42
43
  #
44
+ # == Formats
43
45
  # There are three formats for the returned pattern:
44
46
  #
45
- # bars - 1s and 0s specifying black lines and white spaces. Actual
46
- # characters can be changed from "1" and 0" with options
47
- # :line_character and :space_character.
47
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
48
+ # characters can be changed from "1" and 0" with options
49
+ # :line_character and :space_character.
48
50
  #
49
- # rle - Run-length-encoded version of the pattern. The first
50
- # number is always a black line, with subsequent digits
51
- # alternating between spaces and lines. The digits specify
52
- # the width of each line or space.
51
+ # *rle* - Run-length-encoded version of the pattern. The first
52
+ # number is always a black line, with subsequent digits
53
+ # alternating between spaces and lines. The digits specify
54
+ # the width of each line or space.
53
55
  #
54
- # wn - The native format for this barcode type. The string
55
- # consists of a series of "w" and "n" characters. The first
56
- # item is always a black line, with subsequent characters
57
- # alternating between spaces and lines. A "wide" item
58
- # is twice the width of a "narrow" item.
56
+ # *wn* - The native format for this barcode type. The string
57
+ # consists of a series of "w" and "n" characters. The first
58
+ # item is always a black line, with subsequent characters
59
+ # alternating between spaces and lines. A "wide" item
60
+ # is twice the width of a "narrow" item.
59
61
  #
60
62
  # The "width" method will tell you the total end-to-end width, in
61
63
  # units, of the entire barcode.
62
64
  #
63
- #== Rendering
65
+ # == Rendering
64
66
  #
65
67
  # The author is aware of no standards for display.
66
68
 
@@ -84,7 +86,9 @@ module Barcode1DTools
84
86
  '9'=> {'val'=>9 ,'wn'=>'wnnwnwwn'}
85
87
  }
86
88
 
89
+ # Left guard pattern
87
90
  GUARD_PATTERN_LEFT_WN = 'wn'
91
+ # Right guard pattern
88
92
  GUARD_PATTERN_RIGHT_WN = 'nwn'
89
93
 
90
94
  DEFAULT_OPTIONS = {
@@ -98,11 +102,13 @@ module Barcode1DTools
98
102
  }
99
103
 
100
104
  class << self
101
- # MSI can encode digits
105
+ # MSI can encode digits - returns true if given a string of digits.
102
106
  def can_encode?(value)
103
107
  value.to_s =~ /\A\d+\z/
104
108
  end
105
109
 
110
+ # Generates a check digit using one of four algorithms. The algorithm
111
+ # must be specified in the second parameter (the options hash).
106
112
  def generate_check_digit_for(value, options = {})
107
113
  if options[:check_digit] == 'mod 10'
108
114
  generate_mod10_check_digit_for(value).to_s
@@ -119,11 +125,13 @@ module Barcode1DTools
119
125
  end
120
126
  end
121
127
 
128
+ # Validates the check digit(s) for a given string.
122
129
  def validate_check_digit_for(value, options = {})
123
130
  payload, check_digits = split_payload_and_check_digits(value, options)
124
131
  self.generate_check_digit_for(payload, options) == check_digits
125
132
  end
126
133
 
134
+ # Splits payload and check digit(s) given a check_digit option.
127
135
  def split_payload_and_check_digits(value, options = {})
128
136
  if options[:check_digit] == 'mod 1010' || options[:check_digit] == 'mod 1110'
129
137
  md = value.to_s.match(/\A(.*?)(..)\z/)
@@ -133,6 +141,7 @@ module Barcode1DTools
133
141
  [md[1], md[2]]
134
142
  end
135
143
 
144
+ # Generates a mod 10 check digit.
136
145
  def generate_mod10_check_digit_for(value)
137
146
  value = value.to_s
138
147
  valarr = value.scan(/\d\d?/)
@@ -148,6 +157,7 @@ module Barcode1DTools
148
157
  (10 - ((odd + even) % 10)) % 10
149
158
  end
150
159
 
160
+ # Generates a mod 11 check digit.
151
161
  def generate_mod11_check_digit_for(value, style)
152
162
  max = (style == 'ncr' ? 9 : 7)
153
163
  value = value.to_s
@@ -206,8 +216,10 @@ module Barcode1DTools
206
216
 
207
217
  end
208
218
 
219
+ # Create a new MSI object with a given value.
209
220
  # Options are :line_character, :space_character, :w_character,
210
- # :n_character, :checksum_included, and :skip_checksum.
221
+ # :n_character, :check_digit, :checksum_included, and
222
+ # :skip_checksum.
211
223
  def initialize(value, options = {})
212
224
 
213
225
  @options = DEFAULT_OPTIONS.merge(options)
@@ -235,17 +247,17 @@ module Barcode1DTools
235
247
  @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
236
248
  end
237
249
 
238
- # returns a run-length-encoded string representation
250
+ # Returns a run-length-encoded string representation
239
251
  def rle
240
252
  @rle ||= self.class.wn_to_rle(self.wn, @options)
241
253
  end
242
254
 
243
- # returns 1s and 0s (for "black" and "white")
255
+ # Returns 1s and 0s (for "black" and "white")
244
256
  def bars
245
257
  @bars ||= self.class.rle_to_bars(self.rle, @options)
246
258
  end
247
259
 
248
- # returns the total unit width of the bar code
260
+ # Returns the total unit width of the bar code
249
261
  def width
250
262
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
251
263
  end
@@ -18,11 +18,12 @@ module Barcode1DTools
18
18
  # Plessey is a terrible symbology in modern terms and should
19
19
  # not be used in any new applications.
20
20
  #
21
- # val = "2898289238AF"
22
- # bc = Barcode1DTools::Plessey.new(val)
23
- # pattern = bc.bars
24
- # rle_pattern = bc.rle
25
- # width = bc.width
21
+ # == Example
22
+ # val = "2898289238AF"
23
+ # bc = Barcode1DTools::Plessey.new(val)
24
+ # pattern = bc.bars
25
+ # rle_pattern = bc.rle
26
+ # width = bc.width
26
27
  #
27
28
  # The object created is immutable.
28
29
  #
@@ -37,27 +38,28 @@ module Barcode1DTools
37
38
  # The bits are ordered ascending, so 9 is 1001 binary,
38
39
  # "wn nw nw wn" in w/n format.
39
40
  #
41
+ # == Formats
40
42
  # There are three formats for the returned pattern:
41
43
  #
42
- # bars - 1s and 0s specifying black lines and white spaces. Actual
43
- # characters can be changed from "1" and 0" with options
44
- # :line_character and :space_character.
44
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
45
+ # characters can be changed from "1" and 0" with options
46
+ # :line_character and :space_character.
45
47
  #
46
- # rle - Run-length-encoded version of the pattern. The first
47
- # number is always a black line, with subsequent digits
48
- # alternating between spaces and lines. The digits specify
49
- # the width of each line or space.
48
+ # *rle* - Run-length-encoded version of the pattern. The first
49
+ # number is always a black line, with subsequent digits
50
+ # alternating between spaces and lines. The digits specify
51
+ # the width of each line or space.
50
52
  #
51
- # wn - The native format for this barcode type. The string
52
- # consists of a series of "w" and "n" characters. The first
53
- # item is always a black line, with subsequent characters
54
- # alternating between spaces and lines. A "wide" item
55
- # is twice the width of a "narrow" item.
53
+ # *wn* - The native format for this barcode type. The string
54
+ # consists of a series of "w" and "n" characters. The first
55
+ # item is always a black line, with subsequent characters
56
+ # alternating between spaces and lines. A "wide" item
57
+ # is twice the width of a "narrow" item.
56
58
  #
57
59
  # The "width" method will tell you the total end-to-end width, in
58
60
  # units, of the entire barcode.
59
61
  #
60
- #== Rendering
62
+ # == Rendering
61
63
  #
62
64
  # The author is aware of no standards for display.
63
65
 
@@ -87,7 +89,9 @@ module Barcode1DTools
87
89
  'F'=> {'val'=>15 ,'wn'=>'wnwnwnwn'}
88
90
  }
89
91
 
92
+ # Left guard pattern
90
93
  GUARD_PATTERN_LEFT_WN = 'wnwnnwwn'
94
+ # Right guard pattern
91
95
  GUARD_PATTERN_RIGHT_WN = 'wnwnnwnw'
92
96
 
93
97
  DEFAULT_OPTIONS = {
@@ -99,15 +103,20 @@ module Barcode1DTools
99
103
  }
100
104
 
101
105
  class << self
102
- # Plessey can encode digits and A-F.
106
+ # Plessey can encode digits and A-F. Returns "true" if the
107
+ # given value is encodable.
103
108
  def can_encode?(value)
104
109
  value.to_s =~ /\A[0-9A-F]+\z/
105
110
  end
106
111
 
112
+ # We don't generate check digits for Plessey, so this will raise
113
+ # an error.
107
114
  def generate_check_digit_for(value)
108
115
  raise NotImplementedError
109
116
  end
110
117
 
118
+ # We don't generate check digits for Plessey, so this will raise
119
+ # an error.
111
120
  def validate_check_digit_for(value)
112
121
  raise NotImplementedError
113
122
  end
@@ -162,8 +171,9 @@ module Barcode1DTools
162
171
 
163
172
  end
164
173
 
174
+ # Create a new Plessey barcode object with the given value.
165
175
  # Options are :line_character, :space_character, :w_character,
166
- # :n_character, :checksum_included, and :skip_checksum.
176
+ # and :n_character.
167
177
  def initialize(value, options = {})
168
178
 
169
179
  @options = DEFAULT_OPTIONS.merge(options)
@@ -181,17 +191,17 @@ module Barcode1DTools
181
191
  @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
182
192
  end
183
193
 
184
- # returns a run-length-encoded string representation
194
+ # Returns a run-length-encoded string representation
185
195
  def rle
186
196
  @rle ||= self.class.wn_to_rle(self.wn, @options)
187
197
  end
188
198
 
189
- # returns 1s and 0s (for "black" and "white")
199
+ # Returns 1s and 0s (for "black" and "white")
190
200
  def bars
191
201
  @bars ||= self.class.rle_to_bars(self.rle, @options)
192
202
  end
193
203
 
194
- # returns the total unit width of the bar code
204
+ # Returns the total unit width of the bar code
195
205
  def width
196
206
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
197
207
  end
@@ -18,11 +18,12 @@ module Barcode1DTools
18
18
  # PostNet is used by the USPS for mail sorting, although it
19
19
  # is technically deprecated.
20
20
  #
21
- # val = "37211"
22
- # bc = Barcode1DTools::PostNet.new(val)
23
- # pattern = bc.bars
24
- # rle_pattern = bc.rle
25
- # width = bc.width
21
+ # == Example
22
+ # val = "37211"
23
+ # bc = Barcode1DTools::PostNet.new(val)
24
+ # pattern = bc.bars
25
+ # rle_pattern = bc.rle
26
+ # width = bc.width
26
27
  #
27
28
  # The object created is immutable.
28
29
  #
@@ -31,27 +32,28 @@ module Barcode1DTools
31
32
  # string. In this symbology, "wide" means "tall" and "narrow"
32
33
  # means "short".
33
34
  #
35
+ # == Formats
34
36
  # There are three formats for the returned pattern:
35
37
  #
36
- # bars - 1s and 0s specifying black lines and white spaces. Actual
37
- # characters can be changed from "1" and 0" with options
38
- # :line_character and :space_character.
38
+ # *bars* - 1s and 0s specifying black lines and white spaces. Actual
39
+ # characters can be changed from "1" and 0" with options
40
+ # :line_character and :space_character.
39
41
  #
40
- # rle - Run-length-encoded version of the pattern. The first
41
- # number is always a black line, with subsequent digits
42
- # alternating between spaces and lines. The digits specify
43
- # the width of each line or space.
42
+ # *rle* - Run-length-encoded version of the pattern. The first
43
+ # number is always a black line, with subsequent digits
44
+ # alternating between spaces and lines. The digits specify
45
+ # the width of each line or space.
44
46
  #
45
- # wn - The native format for this barcode type. The string
46
- # consists of a series of "w" and "n" characters. The first
47
- # item is always a black line, with subsequent characters
48
- # alternating between spaces and lines. A "wide" item
49
- # is twice the width of a "narrow" item.
47
+ # *wn* - The native format for this barcode type. The string
48
+ # consists of a series of "w" and "n" characters. The first
49
+ # item is always a black line, with subsequent characters
50
+ # alternating between spaces and lines. A "wide" item
51
+ # is twice the width of a "narrow" item.
50
52
  #
51
53
  # The "width" method will tell you the total end-to-end width, in
52
54
  # units, of the entire barcode.
53
55
  #
54
- #== Rendering
56
+ # == Rendering
55
57
  #
56
58
  # See USPS documentation for exact specifications for display.
57
59
 
@@ -78,7 +80,9 @@ module Barcode1DTools
78
80
  '9'=> {'val'=>9 ,'wn'=>'wnwnn'}
79
81
  }
80
82
 
83
+ # Left guard pattern
81
84
  GUARD_PATTERN_LEFT_WN = 'w'
85
+ # Right guard pattern
82
86
  GUARD_PATTERN_RIGHT_WN = 'w'
83
87
 
84
88
  DEFAULT_OPTIONS = {
@@ -90,16 +94,19 @@ module Barcode1DTools
90
94
 
91
95
  class << self
92
96
  # PostNet can encode 5, 9, or 11 digits, plus a check digit.
97
+ # This returns true if the value is encodable.
93
98
  def can_encode?(value)
94
99
  value.to_s =~ /\A\d{5}(\d{4})?(\d{2})?\d?\z/
95
100
  end
96
101
 
102
+ # Generate the check digit for a given value.
97
103
  def generate_check_digit_for(value)
98
104
  raise UnencodableCharactersError unless self.can_encode?(value)
99
105
  value = value.split('').collect { |c| c.to_i }.inject(0) { |a,c| a + c }
100
106
  (10 - (value % 10)) % 10
101
107
  end
102
108
 
109
+ # Validate the check digit for a given value.
103
110
  def validate_check_digit_for(value)
104
111
  raise UnencodableCharactersError unless self.can_encode?(value)
105
112
  md = value.match(/^(\d+?)(\d)$/)
@@ -152,6 +159,7 @@ module Barcode1DTools
152
159
 
153
160
  end
154
161
 
162
+ # Create a new PostNet object with the given value.
155
163
  # Options are :line_character, :space_character, :w_character,
156
164
  # :n_character, :checksum_included, and :skip_checksum.
157
165
  def initialize(value, options = {})
@@ -196,17 +204,17 @@ module Barcode1DTools
196
204
  @wn ||= wn_str.tr('wn', @options[:w_character].to_s + @options[:n_character].to_s)
197
205
  end
198
206
 
199
- # returns a run-length-encoded string representation
207
+ # Returns a run-length-encoded string representation
200
208
  def rle
201
209
  @rle ||= self.class.wn_to_rle(self.wn, @options)
202
210
  end
203
211
 
204
- # returns 1s and 0s (for "black" and "white")
212
+ # Returns 1s and 0s (for "black" and "white")
205
213
  def bars
206
214
  @bars ||= self.class.rle_to_bars(self.rle, @options)
207
215
  end
208
216
 
209
- # returns the total unit width of the bar code
217
+ # Returns the total unit width of the bar code
210
218
  def width
211
219
  @width ||= rle.split('').inject(0) { |a,c| a + c.to_i }
212
220
  end