yob-abn 1.1.0 → 1.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.
Files changed (4) hide show
  1. data/README.rdoc +7 -1
  2. data/lib/abn.rb +25 -19
  3. data/spec/abn_spec.rb +45 -3
  4. metadata +2 -2
data/README.rdoc CHANGED
@@ -1,7 +1,13 @@
1
1
  A small class for validating Australian Business Numbers (ABN)
2
2
 
3
+ = Installation
4
+
5
+ gem install abn
6
+
3
7
  = Usage
4
8
 
9
+ require 'abn'
10
+
5
11
  ABN.new("12042168743").valid?
6
12
  => true
7
13
 
@@ -11,6 +17,6 @@ A small class for validating Australian Business Numbers (ABN)
11
17
  ABN.valid_abn?("12042168744")
12
18
  => false
13
19
 
14
- = Further Reader
20
+ = Further Reading
15
21
 
16
22
  - http://www.clearwater.com.au/?action=code
data/lib/abn.rb CHANGED
@@ -1,39 +1,45 @@
1
1
  class ABN
2
2
 
3
- class Version #:nodoc:
3
+ module Version #:nodoc:
4
4
  Major = 1
5
- Minor = 1
5
+ Minor = 2
6
6
  Tiny = 0
7
7
 
8
8
  String = [Major, Minor, Tiny].join('.')
9
9
  end
10
10
 
11
+ # Creates an ABN object representing the ABN number passed
12
+ # as the only parameter.
11
13
  def initialize(num)
12
- @number = num.to_s
14
+ @number = num.to_s.tr ' ',''
13
15
  end
14
16
 
17
+ # Returns whether the current ABN class represents a
18
+ # valid ABN number according to a weighting
19
+ # algorithm (not checked against a datbase)
15
20
  def valid?
16
- ABN.valid? @number
17
- end
18
-
19
- def to_s
20
- "#{@number[0,2]} #{@number[2,3]} #{@number[5,3]} #{@number[8,3]}"
21
- end
22
-
23
- def self.valid?(abn)
24
- abn = abn.to_s
25
- return false unless abn.length == 11
21
+ return false unless @number.length == 11
26
22
 
27
23
  weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
28
24
  sum = 0
29
- (0..10).each do |idx|
30
- c = abn[idx,1]
31
- idx == 0 ? sub = 1 : sub = 0
32
- digit = c.to_i - sub
33
- sum += weights[idx] * digit
25
+ (0..10).each do |i|
26
+ c = @number[i,1]
27
+ digit = c.to_i - (i.zero? ? 1 : 0)
28
+ sum += weights[i] * digit
34
29
  end
35
30
 
36
31
  sum % 89 == 0 ? true : false
37
32
  end
38
-
33
+
34
+ # Correctly formats the represented ABN if valid, else returns
35
+ # an empty string
36
+ def to_s
37
+ valid? ? "%s%s %s%s%s %s%s%s %s%s%s" % @number.split('') : ""
38
+ end
39
+
40
+ # Accepts an ABN number as a String or Bignum and returns
41
+ # whether or not it is valid (not checked against a database)
42
+ def self.valid?(abn)
43
+ new(abn).valid?
44
+ end
39
45
  end
data/spec/abn_spec.rb CHANGED
@@ -3,21 +3,63 @@ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
3
3
  require 'spec'
4
4
  require 'abn'
5
5
 
6
+ def bad_parameter; (1..11).to_a; end
7
+
6
8
  describe "The ABN class" do
7
9
  it "should identify a valid ABN" do
8
10
  ABN.valid?("12042168743").should be_true
9
11
  ABN.valid?(12042168743).should be_true
10
12
  end
11
13
 
12
- it "should identify an invalid ABN" do
14
+ it "should identify a preformatted valid ABN" do
15
+ ABN.valid?("12 042 168 743").should be_true
16
+ end
17
+
18
+ it "should have a problem with a pre-formatted invalid ABN" do
19
+ ABN.valid?("12 042 168 744").should be_false
20
+ end
21
+
22
+ it "should have a problem with an invalid ABN" do
23
+ ABN.valid?("12042168744").should be_false
24
+ ABN.valid?("902865").should be_false
25
+ end
26
+
27
+ it "should have a problem with invalid parameters" do
13
28
  ABN.valid?(nil).should be_false
14
- ABN.valid?("902865").should be_false
15
29
  ABN.valid?(Array).should be_false
16
30
  ABN.valid?(Array.new).should be_false
17
31
  end
32
+
33
+ it "should have a problem with invalid parameter type that has a #length of 11" do
34
+ bad_parameter.length.should eql(11)
35
+ ABN.valid?(bad_parameter).should be_false
36
+ end
18
37
 
19
- it "should be able to pretty print a valid ABN" do
38
+ it "should be able to format a valid ABN" do
20
39
  ABN.new("12042168743").to_s.should eql("12 042 168 743")
21
40
  end
41
+
42
+ it "should be able to return a pre-formatted ABN" do
43
+ ABN.new("12 042 168 743").to_s.should eql("12 042 168 743")
44
+ end
45
+
46
+ it "should not format invalid parameter" do
47
+ ABN.new(nil).to_s.should eql("")
48
+ ABN.new(Array).to_s.should eql("")
49
+ ABN.new(Array.new).to_s.should eql("")
50
+ end
51
+
52
+ it "should not format an invalid parameter type that has a #length of 11" do
53
+ bad_parameter.length.should eql(11)
54
+ ABN.new(bad_parameter).to_s.should eql("")
55
+ end
56
+
57
+ it "should not format an invalid ABN" do
58
+ ABN.new("12042168744").to_s.should eql("")
59
+ ABN.new("902865").to_s.should eql("")
60
+ end
22
61
 
62
+ it "should not format an pre-formatted invalid ABN" do
63
+ ABN.new("12 042 168 744").to_s.should eql("")
64
+ end
23
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yob-abn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version:
49
49
  requirements: []
50
50
 
51
- rubyforge_project:
51
+ rubyforge_project: yob-projects
52
52
  rubygems_version: 1.2.0
53
53
  signing_key:
54
54
  specification_version: 2