verhoeff 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/verhoeff.rb CHANGED
@@ -1,42 +1,43 @@
1
- require 'enumerator'
2
1
  module Verhoeff
3
- VERSION = '1.0.0'
4
-
5
- # Verhoeff Multiplication Table
6
- M = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
7
- [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
8
- [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
9
- [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
10
- [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
11
- [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
12
- [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
13
- [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
14
- [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
15
- [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] ]
16
-
17
- P = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
18
- [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
19
- [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
20
- [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
21
- [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
22
- [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
23
- [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
24
- [7, 0, 4, 6, 9, 1, 3, 2, 5, 8] ]
25
-
2
+ D = [
3
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
4
+ [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
5
+ [2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
6
+ [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
7
+ [4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
8
+ [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
9
+ [6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
10
+ [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
11
+ [8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
12
+ [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
13
+ ]
14
+
15
+ P = [
16
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
17
+ [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
18
+ [5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
19
+ [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
20
+ [9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
21
+ [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
22
+ [2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
23
+ [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
24
+ ]
25
+
26
26
  INV = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]
27
-
27
+
28
+ ZERO_ORDINAL = 48 # '0'.each_byte.first on 1.8 or '0'.ord on 1.9
29
+
28
30
  def self.checksum_digit_of(arg)
29
- INV[arg.to_s.split("").reverse.enum_for(:each_with_index).inject(0) { |check,(x,i)|
30
- M[check][P[i.next % 8][x[0] - ?0]]
31
+ INV[arg.to_s.each_byte.reverse_each.with_index.inject(0) { |check,(x,i)|
32
+ D[check][P[i.next % 8][x - ZERO_ORDINAL]]
31
33
  }]
32
34
  end
33
-
35
+
34
36
  def self.checksum_of(arg)
35
37
  arg.to_i * 10 + checksum_digit_of(arg)
36
38
  end
37
-
38
- def self.checks_out?(num)
39
- checksum_digit_of(num.to_s[0..-2]) == num.to_s[-1] - ?0
39
+
40
+ def self.valid?(num)
41
+ checksum_digit_of(num.to_s[0..-2]) == num % 10
40
42
  end
41
-
42
43
  end
@@ -0,0 +1,3 @@
1
+ module Verhoeff
2
+ VERSION = "2.0.0"
3
+ end
metadata CHANGED
@@ -1,70 +1,71 @@
1
- --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
1
+ --- !ruby/object:Gem::Specification
4
2
  name: verhoeff
5
- version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2007-07-15 00:00:00 -07:00
8
- summary: A pure-Ruby implementation of the Verhoeff checksum algorithm
9
- require_paths:
10
- - lib
11
- email: jay -at- codemecca.com
12
- homepage: " by Jay Phillips (your name)"
13
- rubyforge_project: verhoeff
14
- description: "== FEATURES/PROBLEMS: A library that returns == SYNOPSIS: Verhoeff.checkum_of 12345 # => \"123451\" Verhoeff.checkum_of 54321 # => \"543217\" Verhoeff.checks_out? 543217 # => true Verhoeff.checks_out? 543211 # => false Or, if you've required verhoeff/monkey_patches, you can do this: 656.checksum # => 6569 \"657\".checksum # => 6576 6567.checks_out? # => true \"6560\".checks_out? # => false"
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jicksta
9
+ - beawesomeinstead
10
+ - oklasoft
11
+ - hgavin
15
12
  autorequire:
16
- default_executable:
17
13
  bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
25
- platform: ruby
26
- signing_key:
27
- cert_chain:
14
+ cert_chain: []
15
+ date: 2011-12-15 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rake
19
+ requirement: &70155577391560 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: *70155577391560
28
+ - !ruby/object:Gem::Dependency
29
+ name: shoulda
30
+ requirement: &70155577390400 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: *70155577390400
39
+ description: Pure-Ruby implementation of the Verhoeff checksum algorithm
40
+ email: graf.otodrakula@gmail.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - lib/verhoeff.rb
46
+ - lib/verhoeff/version.rb
47
+ homepage: http://github.com/bai/verhoeff
48
+ licenses: []
28
49
  post_install_message:
29
- authors:
30
- - Jay Phillips
31
- files:
32
- - bin
33
- - bin/verhoeff
34
- - History.txt
50
+ rdoc_options: []
51
+ require_paths:
35
52
  - lib
36
- - lib/monkey_patches.rb
37
- - lib/verhoeff.rb
38
- - Manifest.txt
39
- - pkg
40
- - Rakefile
41
- - README.txt
42
- - spec
43
- - spec/checksum_spec.rb
44
- - spec/monkey_patches_spec.rb
45
- - test
46
- test_files: []
47
-
48
- rdoc_options:
49
- - --main
50
- - README.txt
51
- extra_rdoc_files:
52
- - History.txt
53
- - Manifest.txt
54
- - README.txt
55
- executables:
56
- - verhoeff
57
- extensions: []
58
-
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
59
65
  requirements: []
60
-
61
- dependencies:
62
- - !ruby/object:Gem::Dependency
63
- name: hoe
64
- version_requirement:
65
- version_requirements: !ruby/object:Gem::Version::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: 1.2.1
70
- version:
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.10
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Pure-Ruby implementation of the Verhoeff checksum algorithm
71
+ test_files: []
data/History.txt DELETED
@@ -1,3 +0,0 @@
1
- == 0.9.9 / 2007-07-10
2
-
3
- * Initial implementation. Has decent spec coverage but it could be better.
data/Manifest.txt DELETED
@@ -1,7 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- bin/verhoeff
6
- lib/verhoeff.rb
7
- test/test_verhoeff.rb
data/README.txt DELETED
@@ -1,55 +0,0 @@
1
- verhoeff
2
- by Jay Phillips (your name)
3
- Codemecca LLC (http://codemecca.com)
4
-
5
- == DESCRIPTION:
6
-
7
- Pure-Ruby implementation of the Verhoeff checksum algorithm. Useful for attaching to numbers which will later be entered by an error-prone human. Typos in the number can be found quickly with the checksum before any further work is performed.
8
-
9
- == FEATURES/PROBLEMS:
10
-
11
- A library that returns
12
-
13
- == SYNOPSIS:
14
-
15
- Verhoeff.checkum_of 12345 # => "123451"
16
- Verhoeff.checkum_of 54321 # => "543217"
17
- Verhoeff.checks_out? 543217 # => true
18
- Verhoeff.checks_out? 543211 # => false
19
-
20
- Or, if you've required verhoeff/monkey_patches, you can do this:
21
-
22
- 656.checksum # => 6569
23
- "657".checksum # => 6576
24
- 6567.checks_out? # => true
25
- "6560".checks_out? # => false
26
-
27
-
28
- == INSTALL:
29
-
30
- * sudo gem install verhoeff
31
-
32
- == LICENSE:
33
-
34
- (The MIT License)
35
-
36
- Copyright (c) 2007 Jay Phillips
37
-
38
- Permission is hereby granted, free of charge, to any person obtaining
39
- a copy of this software and associated documentation files (the
40
- 'Software'), to deal in the Software without restriction, including
41
- without limitation the rights to use, copy, modify, merge, publish,
42
- distribute, sublicense, and/or sell copies of the Software, and to
43
- permit persons to whom the Software is furnished to do so, subject to
44
- the following conditions:
45
-
46
- The above copyright notice and this permission notice shall be
47
- included in all copies or substantial portions of the Software.
48
-
49
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
50
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile DELETED
@@ -1,26 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/verhoeff.rb'
6
- require 'spec/rake/spectask'
7
- require 'rake/gempackagetask'
8
-
9
- Hoe.new('verhoeff', Verhoeff::VERSION) do |p|
10
- p.rubyforge_name = 'verhoeff'
11
- p.author = 'Jay Phillips'
12
- p.email = 'jay -at- codemecca.com'
13
- p.summary = 'A pure-Ruby implementation of the Verhoeff checksum algorithm'
14
- p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
15
- p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
16
- p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
17
- p.spec_extras = {
18
- "files" => Dir['**/*']
19
- }
20
- end
21
-
22
- Spec::Rake::SpecTask.new "spec" do |t|
23
- t.spec_opts = ["-c"]
24
- t.spec_files = Dir['spec/*_spec.rb']
25
- end
26
-
data/bin/verhoeff DELETED
File without changes
@@ -1,21 +0,0 @@
1
- require File.dirname(__FILE__) + '/verhoeff.rb'
2
-
3
- module Verhoeff
4
- module ChecksumMethods
5
- def checksum
6
- Verhoeff.checksum_of self
7
- end
8
- def checksum_digit
9
- Verhoeff.checksum_digit_of self
10
- end
11
- def checks_out?
12
- Verhoeff.checks_out? self
13
- end
14
- end
15
- end
16
-
17
- [Numeric,String].each do |klass|
18
- klass.class_eval do
19
- include Verhoeff::ChecksumMethods
20
- end
21
- end
@@ -1,18 +0,0 @@
1
- require File.dirname(__FILE__) + '/../lib/verhoeff.rb'
2
-
3
- describe "The Verhoeff checksum algorithm implementation" do
4
- it "should work with correct, previously calculated checksums" do
5
- (1..23).inject(1) do |checksum,i|
6
- Verhoeff.checksum_of checksum
7
- end.should eql(150493068613366131371194)
8
- end
9
-
10
- it "should work when given a String argument and return a Fixnum" do
11
- Verhoeff.checksum_of("999").should equal(9998)
12
- end
13
-
14
- it "should validate a properly generated checksum" do
15
- Verhoeff.checks_out?(13375).should be_true
16
- Verhoeff.checks_out?(13735).should be_false
17
- end
18
- end
@@ -1,24 +0,0 @@
1
- require File.dirname(__FILE__) + "/../lib/monkey_patches.rb"
2
-
3
- KNOWN_CASES = {
4
- 123 => 3,
5
- 765 => 7,
6
- 43167312675318282 => 3,
7
- 01 => 5
8
- }
9
-
10
- describe "The Verhoeff checksum monkey patches" do
11
-
12
- it "should monkey patch Numeric and String properly" do
13
- check = lambda do |k,v|
14
- k.checksum_digit.should eql(v)
15
- k.checksum.should eql(k.to_i*10+v)
16
- (k.to_i*10+v).should be_checks_out
17
- end
18
- KNOWN_CASES.each_pair &check
19
- KNOWN_CASES.each_pair do |k,v|
20
- check.call(k.to_s,v)
21
- end
22
- end
23
-
24
- end