gtin 0.0.1 → 0.1.1

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.
@@ -2,3 +2,8 @@
2
2
 
3
3
  * 1 major enhancement:
4
4
  * Initial release
5
+
6
+ === 0.1.0 2010-04-26
7
+
8
+ * 1 major enhancement:
9
+ * Complete rework of core GTIN functionality to have a much cleaner checksum functionality
@@ -1,6 +1,6 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
- module Gtin
5
- VERSION = '0.0.1'
4
+ module GTIN
5
+ VERSION = '0.1.1'
6
6
  end
@@ -1,84 +1,71 @@
1
+ # This will ultimately become a gem in its own right
1
2
  # GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
2
3
  # GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
3
4
  # GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
4
5
  # GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels
5
6
 
6
- module GTIN
7
- def generate_check_digit
8
- numbers = self.to_s.gsub(/[\D]+/, "").split(//)
9
-
10
- checksum = 0
11
- case numbers.length
12
- when 7
13
- 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
14
- when 11
15
- 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
16
- when 12
17
- 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
18
- when 13
19
- 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
20
- else
21
- 0
22
- end
23
-
24
- return ((10 - checksum % 10)%10).to_s
7
+ class Integer
8
+ def odd?
9
+ self & 1 != 0
25
10
  end
11
+
12
+ def even?
13
+ self & 1 == 0
14
+ end
15
+ end
26
16
 
17
+ module GTIN
27
18
  def ean?
28
- numbers = self.to_s.gsub(/[\D]+/, "").split(//)
29
-
30
- checksum = 0
31
- case numbers.length
32
- when 8
33
- 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
34
- when 13
35
- 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
36
- when 14
37
- 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
38
- else
39
- return false
40
- end
41
-
42
- return numbers[-1].to_i == (10 - checksum % 10)%10
19
+ # self = self.to_s.gsub(/[\D]+/, "").split(//)
20
+ return false if self.length != 13
21
+ valid_checksum?
43
22
  end
44
23
 
45
24
  def upc?
46
- value = self.to_s.gsub(/[\D]+/, "").split(//)
47
- return false if value.length != 12
48
- valid_checksum? value
25
+ # self = self.to_s.gsub(/[\D]+/, "").split(//)
26
+ return false if self.length != 12
27
+ valid_checksum?
49
28
  end
50
29
 
30
+ # FOR A UPC:
31
+ # From the right to left, start with odd position, assign the odd/even position to each digit.
32
+ # Sum all digits in odd position and multiply the result by 3.
33
+ # Sum all digits in even position.
34
+ # Sum the results of step 3 and step 4.
35
+ # divide the result of step 4 by 10. The check digit is the number which adds the remainder to 10.
36
+
51
37
  # Determine if a gtin value has a valid checksum
52
- def valid_checksum?(value)
53
- checksum = 0
54
- 0.upto(value.length-2) do |i|
55
- value%2 == 0 ?
56
- (checksum += value[i].to_i * ((i-1)%2*3 +i%2)) :
57
- (checksum += numbers[i].to_i * (i%2*3 +(i-1)%2))
38
+ def valid_checksum?
39
+ number = self.reverse
40
+ odd = even = 0
41
+
42
+ (1..number.length-1).each do |i|
43
+ i.even? ? (even += number[i].chr.to_i) : (odd += number[i].chr.to_i)
58
44
  end
59
- value[-1].to_i == (10 - checksum % 10)%10
45
+
46
+ number[0].chr.to_i == (10 - ((odd * 3) + even) % 10)
60
47
  end
61
48
 
62
49
 
63
50
  # GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
64
- def to_gtin_12(number)
65
- to_gtin(number, 12)
51
+ def to_gtin_12
52
+ to_gtin(12)
66
53
  end
67
54
  alias :to_upc :to_gtin_12
68
55
  alias :to_upc_a :to_gtin_12
69
56
 
70
57
 
71
58
  # GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
72
- def to_gtin_8(number)
73
- to_gtin(number, 8)
59
+ def to_gtin_8
60
+ to_gtin(8)
74
61
  end
75
62
  alias :to_ean_8 :to_gtin_8
76
63
  alias :to_ucc_8 :to_gtin_8
77
64
 
78
65
 
79
66
  # GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
80
- def to_gtin_13(number)
81
- to_gtin(number, 13)
67
+ def to_gtin_13
68
+ to_gtin(13)
82
69
  end
83
70
  alias :to_ean_13 :to_gtin_13
84
71
  alias :to_ucc_13 :to_gtin_13
@@ -86,13 +73,13 @@ module GTIN
86
73
 
87
74
 
88
75
  # GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels
89
- def to_gtin_14(number)
90
- to_gtin(number, 14)
76
+ def to_gtin_14
77
+ to_gtin(14)
91
78
  end
92
79
  alias :to_gtin :to_gtin_14
93
80
 
94
- def to_gtin(number, size)
95
- "%0#{size}d" % number.to_s.gsub(/[\D]+/, "")
81
+ def to_gtin(size=14)
82
+ "%0#{size.to_i}d" % self.to_s.gsub(/[\D]+/, "").to_i
96
83
  end
97
84
  end
98
85
 
@@ -100,8 +87,3 @@ end
100
87
  class String
101
88
  include GTIN
102
89
  end
103
-
104
- # Extend Numberic to include these methods
105
- class Numeric
106
- include GTIN
107
- end
@@ -1,7 +1,7 @@
1
1
  module GTIN #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 0
4
+ MINOR = 1
5
5
  TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 0
8
7
  - 1
9
- version: 0.0.1
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hans Masing
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2009-04-25 00:00:00 -04:00
17
+ date: 2009-04-26 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -40,7 +40,7 @@ files:
40
40
  - test/test_gtin.rb
41
41
  - test/test_helper.rb
42
42
  has_rdoc: true
43
- homepage: http://www.dragonflydepot.org/
43
+ homepage: http://github.com/hmasing/gtin
44
44
  licenses: []
45
45
 
46
46
  post_install_message: