reference_number 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ ### ReferenceNumber 0.0.1 (2012-09-28)
2
+
3
+ * First version created.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Antti Akonniemi and contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # ReferenceNumber
2
+
3
+ Original idea and code was gotten from Christian:
4
+ http://snippets.aktagon.com/snippets/51-How-to-Calculate-a-reference-number-with-Ruby
5
+
6
+ ## Usage
7
+
8
+ Add this to your gem file:
9
+
10
+ gem "reference_number"
11
+
12
+ Example usage:
13
+
14
+ ReferenceNumber.new("1111").to_s
15
+ ReferenceNumber.new(1111).to_s
16
+
17
+ The above example prints out “11112”.
18
+
19
+ ReferenceNumber.new(1111, :zero_padding => true).to_s
20
+
21
+ The above example prints out zero padded reference number "00000000000000011112".
22
+
23
+ This example shows how to generate a series of reference number:
24
+
25
+ (1000..1023).to_a.each do |number|
26
+ puts ReferenceNumber.new(number).to_s
27
+ end
28
+
29
+ ## License
30
+
31
+ ReferenceNumber is licensed under the MIT License, for more details see the LICENSE file.
@@ -0,0 +1,48 @@
1
+ class ReferenceNumber
2
+ class InvalidReferenceNumber < ArgumentError
3
+ end
4
+
5
+ attr_accessor :input
6
+
7
+ def initialize(input, opts = { :zero_padding => false })
8
+ @zero_padding = opts[:zero_padding]
9
+
10
+ @input = input
11
+ if(@input.is_a? Fixnum)
12
+ @input = @input.to_s
13
+ end
14
+
15
+ raise InvalidReferenceNumber, "Input should be between 3 to 19 characters long" if (@input.length < 3 || @input.length > 19)
16
+ raise InvalidReferenceNumber, "Only spaces and numbers are allowed" if (@input =~ /[^ \d]/) != nil
17
+
18
+ @input.gsub!(/ /, "")
19
+ @input.sub!(/0*/, "") # remove leading zeros
20
+
21
+ raise InvalidReferenceNumber, "Input can't be just spaces and zeroes." if(@input.length == 0)
22
+ end
23
+
24
+ def to_s(options = { :zero_padding => false })
25
+ @input.reverse!
26
+ base = "731" * 50
27
+
28
+ index = 0
29
+ sum = 0
30
+
31
+ @input.each_byte do |b|
32
+ result = b.chr.to_i * base[index % 3].chr.to_i
33
+ sum = sum + result
34
+ index = index + 1
35
+ end
36
+
37
+ # Calculate the last number. If sum = 94 then number is 6.
38
+ difference = (10 - (sum % 10)) % 10
39
+ difference_and_input = "#{difference}#{@input}".reverse
40
+
41
+ if @zero_padding
42
+ "%020d" % difference_and_input if @zero_padding
43
+ else
44
+ difference_and_input
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ class ReferenceNumber
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reference_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Antti Akonniemi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.11.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.11.0
62
+ description: No need to copy paste your old reference number codes anymore, just use
63
+ this gem
64
+ email:
65
+ - antti@kiskolabs.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/reference_number/version.rb
71
+ - lib/reference_number.rb
72
+ - LICENSE
73
+ - CHANGELOG.md
74
+ - README.md
75
+ homepage: http://github.com/akonan/reference_number
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A gem for calculating reference number (viitenumere, referensnummer)
99
+ test_files: []