abn 1.2.0 → 1.3.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 +3 -3
  2. data/lib/abn.rb +41 -3
  3. data/spec/abn_spec.rb +54 -10
  4. metadata +4 -4
@@ -2,7 +2,7 @@ A small class for validating Australian Business Numbers (ABN)
2
2
 
3
3
  = Installation
4
4
 
5
- gem install yob-abn --source http://gems.github.com/
5
+ gem install abn
6
6
 
7
7
  = Usage
8
8
 
@@ -11,10 +11,10 @@ A small class for validating Australian Business Numbers (ABN)
11
11
  ABN.new("12042168743").valid?
12
12
  => true
13
13
 
14
- ABN.valid_abn?("12042168743")
14
+ ABN.valid?("12042168743")
15
15
  => true
16
16
 
17
- ABN.valid_abn?("12042168744")
17
+ ABN.valid?("12042168744")
18
18
  => false
19
19
 
20
20
  = Further Reading
data/lib/abn.rb CHANGED
@@ -2,9 +2,9 @@ class ABN
2
2
 
3
3
  module Version #:nodoc:
4
4
  Major = 1
5
- Minor = 2
5
+ Minor = 3
6
6
  Tiny = 0
7
-
7
+
8
8
  String = [Major, Minor, Tiny].join('.')
9
9
  end
10
10
 
@@ -27,7 +27,7 @@ class ABN
27
27
  digit = c.to_i - (i.zero? ? 1 : 0)
28
28
  sum += weights[i] * digit
29
29
  end
30
-
30
+
31
31
  sum % 89 == 0 ? true : false
32
32
  end
33
33
 
@@ -43,3 +43,41 @@ class ABN
43
43
  new(abn).valid?
44
44
  end
45
45
  end
46
+
47
+ module ABNValidations
48
+ class << self
49
+ def enable
50
+ ActiveRecord::Base.extend ABNValidations
51
+ end
52
+ end
53
+
54
+
55
+ # Validates whether the value of the specified attribute conforms to the Australian Business Number (ABN) format.
56
+ #
57
+ # +validates_abn_correctness_of+ will automatically non-destructively strip any formatting and whitespace before
58
+ # validation
59
+ def validates_abn_correctness_of(*args)
60
+
61
+ # Set up our configuration for this validation
62
+ configuration = { :on => :save, :allow_nil => false, :message => "is not a valid ABN" }
63
+ configuration.update(args.extract_options!)
64
+
65
+ # iterate through each field we've set up to validate the local numericality of
66
+ validates_each(args, configuration) do |instance, attr_name, value|
67
+
68
+ # get our raw value (ie, what was last sent to the instance...)
69
+ raw_value = instance.send("#{attr_name}_before_type_cast") || value
70
+
71
+ # skip our processing if we've got a legal nil...
72
+ next if (configuration[:allow_nil] and raw_value.nil?)
73
+
74
+ unless ABN.valid?(raw_value)
75
+ instance.errors.add(attr_name, configuration[:message])
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ if defined?(ActiveRecord)
82
+ ABNValidations.enable
83
+ end
@@ -1,8 +1,52 @@
1
1
  $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
2
 
3
+ require 'active_support'
4
+ require 'active_record'
3
5
  require 'spec'
4
6
  require 'abn'
5
7
 
8
+ # set up some AR test frameworks
9
+ # We gotta shove this somewhere...
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
11
+
12
+ # And finally...
13
+ ActiveRecord::Schema.define(:version => 1) do
14
+ create_table :businesses do |t|
15
+ t.string :abn
16
+ end
17
+ end
18
+
19
+ class Business < ActiveRecord::Base
20
+ validates_abn_correctness_of :abn
21
+ end
22
+
23
+ describe "The ABN validation plugin" do
24
+ it "should just plain work." do
25
+ b = Business.new
26
+
27
+ b.abn = "12 042 168 743"
28
+ b.should be_valid
29
+
30
+ b.abn = "12042168743"
31
+ b.should be_valid
32
+
33
+ b.abn = 12042168743
34
+ b.should be_valid
35
+
36
+
37
+
38
+ b.abn = "12 042 168 744"
39
+ b.should_not be_valid
40
+
41
+ b.abn = "12042168744"
42
+ b.should_not be_valid
43
+
44
+ b.abn = 12042168744
45
+ b.should_not be_valid
46
+ end
47
+ end
48
+
49
+
6
50
  def bad_parameter; (1..11).to_a; end
7
51
 
8
52
  describe "The ABN class" do
@@ -10,26 +54,26 @@ describe "The ABN class" do
10
54
  ABN.valid?("12042168743").should be_true
11
55
  ABN.valid?(12042168743).should be_true
12
56
  end
13
-
14
- it "should identify a preformatted valid ABN" do
57
+
58
+ it "should identify a preformatted valid ABN" do
15
59
  ABN.valid?("12 042 168 743").should be_true
16
60
  end
17
-
61
+
18
62
  it "should have a problem with a pre-formatted invalid ABN" do
19
63
  ABN.valid?("12 042 168 744").should be_false
20
64
  end
21
-
65
+
22
66
  it "should have a problem with an invalid ABN" do
23
67
  ABN.valid?("12042168744").should be_false
24
68
  ABN.valid?("902865").should be_false
25
69
  end
26
-
70
+
27
71
  it "should have a problem with invalid parameters" do
28
72
  ABN.valid?(nil).should be_false
29
73
  ABN.valid?(Array).should be_false
30
74
  ABN.valid?(Array.new).should be_false
31
75
  end
32
-
76
+
33
77
  it "should have a problem with invalid parameter type that has a #length of 11" do
34
78
  bad_parameter.length.should eql(11)
35
79
  ABN.valid?(bad_parameter).should be_false
@@ -38,22 +82,22 @@ describe "The ABN class" do
38
82
  it "should be able to format a valid ABN" do
39
83
  ABN.new("12042168743").to_s.should eql("12 042 168 743")
40
84
  end
41
-
85
+
42
86
  it "should be able to return a pre-formatted ABN" do
43
87
  ABN.new("12 042 168 743").to_s.should eql("12 042 168 743")
44
88
  end
45
-
89
+
46
90
  it "should not format invalid parameter" do
47
91
  ABN.new(nil).to_s.should eql("")
48
92
  ABN.new(Array).to_s.should eql("")
49
93
  ABN.new(Array.new).to_s.should eql("")
50
94
  end
51
-
95
+
52
96
  it "should not format an invalid parameter type that has a #length of 11" do
53
97
  bad_parameter.length.should eql(11)
54
98
  ABN.new(bad_parameter).to_s.should eql("")
55
99
  end
56
-
100
+
57
101
  it "should not format an invalid ABN" do
58
102
  ABN.new("12042168744").to_s.should eql("")
59
103
  ABN.new("902865").to_s.should eql("")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-12 00:00:00 +10:00
12
+ date: 2009-04-16 00:00:00 +10:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -48,8 +48,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  version:
49
49
  requirements: []
50
50
 
51
- rubyforge_project: abn
52
- rubygems_version: 1.2.0
51
+ rubyforge_project: yob-projects
52
+ rubygems_version: 1.3.1
53
53
  signing_key:
54
54
  specification_version: 2
55
55
  summary: a (very) small library for working with Australian Business Numbers.