syc-barcode 0.0.2 → 0.0.3

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.
Files changed (3) hide show
  1. data/README.rdoc +4 -0
  2. data/lib/interleave2of5.rb +33 -7
  3. metadata +6 -5
@@ -22,5 +22,9 @@ The barcode can be used to create a graphical representation of the barcode.
22
22
  * Version 0.0.2
23
23
  Fix check digit calculation
24
24
 
25
+ * Version 0.0.3
26
+ Add valid? to check whether a decoded value (e.g. by a barcode scanner) is
27
+ valid
28
+
25
29
  == Licencse
26
30
  Barcode is published under the MIT license
@@ -1,11 +1,11 @@
1
1
  require_relative 'checked_attributes'
2
2
  require_relative 'barcode'
3
3
 
4
- # Creates a Interleave 2 of 5 barcode
4
+ # Creates a Interleaved 2 of 5 barcode
5
5
  class Interleave2of5 < Barcode
6
6
  include CheckedAttributes
7
7
 
8
- # Holds the options for an Interleave 2 of 5 barcode
8
+ # Holds the options for an Interleaved 2 of 5 barcode
9
9
  OPTIONS = {
10
10
  x: 0,
11
11
  y: 0,
@@ -104,18 +104,44 @@ class Interleave2of5 < Barcode
104
104
  @graph
105
105
  end
106
106
 
107
+ # Returns true if the decoded value (e.g. returned by a barcode scanner) is
108
+ # a valid barcode. Validity is identified by the check digit. If no check
109
+ # digit is given valid? will return true if the decoded value's size is even
110
+ #
111
+ # Example
112
+ # Interleave2of5.valid? "002127", true -> true
113
+ # Interleave2of5.valid? "002128", true -> false
114
+ # Interleave2of5.valid? "00212", false -> false
115
+ def self.valid?(decoded, check_digit = true)
116
+ return false if decoded.size.odd?
117
+ if check_digit
118
+ value = decoded[0...decoded.size-1]
119
+ check = decoded[decoded.size-1]
120
+ return check_digit_for(value) == check
121
+ else
122
+ true
123
+ end
124
+ end
125
+
107
126
  private
108
127
 
109
128
  # Adds a check digit if check is true and ensures even digit size
110
129
  def add_check_digit_and_ensure_even_digit_size
111
- reverse_value = @value.split('').reverse
112
- @check_digit = 10 - reverse_value.each_with_index.collect do |v, i|
113
- i.even? ? v.to_i * 3 : v.to_i
114
- end.inject(:+) % 10 if @check
115
- @check_digit = 0 if @check_digit == 10
130
+ @check_digit = self.class.check_digit_for @value
116
131
  @encodable = @value + (@check ? @check_digit.to_s : "")
117
132
  @encodable = "0" + @encodable if @encodable.size.odd?
118
133
  @encodable
119
134
  end
120
135
 
136
+ # Calculate the check digit for the provided value
137
+ def self.check_digit_for(value)
138
+ reverse_value = value.split('').reverse
139
+ check = 10 - reverse_value.each_with_index.collect do |v, i|
140
+ i.even? ? v.to_i * 3 : v.to_i
141
+ end.inject(:+) % 10
142
+ check = 0 if check == 10
143
+
144
+ check.to_s
145
+ end
146
+
121
147
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syc-barcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-16 00:00:00.000000000 Z
12
+ date: 2014-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdoc
@@ -66,8 +66,9 @@ description: ! "= Barcode\nCreating barcodes (at the moment only Interleaved2of5
66
66
  \ require 'interleave2of5'\nto config/application.rb\n\nThe barcode can be used
67
67
  to create a graphical representation of the barcode.\n\n== Release Notes\n* Version
68
68
  0.0.1\n Create barcode Interleaved 2 of 5 that can be added to a pdf file\n\n*
69
- Version 0.0.2\n Fix check digit calculation\n\n== Licencse\nBarcode is published
70
- under the MIT license\n"
69
+ Version 0.0.2\n Fix check digit calculation\n\n* Version 0.0.3\n Add valid? to
70
+ check whether a decoded value (e.g. by a barcode scanner) is\n valid\n\n== Licencse\nBarcode
71
+ is published under the MIT license\n"
71
72
  email: pierre@sugaryourcoffee.de
72
73
  executables: []
73
74
  extensions: []
@@ -105,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
106
  version: '0'
106
107
  requirements: []
107
108
  rubyforge_project:
108
- rubygems_version: 1.8.25
109
+ rubygems_version: 1.8.23
109
110
  signing_key:
110
111
  specification_version: 3
111
112
  summary: Simple barcode creator