abn 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG +18 -0
  2. data/README.rdoc +24 -1
  3. data/lib/abn.rb +2 -40
  4. data/spec/abn_spec.rb +9 -50
  5. metadata +83 -10
@@ -0,0 +1,18 @@
1
+ v2.0.0
2
+ - remove ActiveRecord validation macro, it gets messy to maintain
3
+ support for AR 2.x and 3.x so I opted to keep things simple and
4
+ remove it
5
+ - modernise package - clean out the Rakefile, use bundler for load
6
+ path management, etc.
7
+
8
+ v1.3
9
+ - [Dan Cheail] Added an ActiveRecord validation macro, validates_abn_correctness_of
10
+
11
+ v1.2
12
+ - Merged some cleanup and safety patches by Bodaniel Jeanes
13
+
14
+ v1.1
15
+ - Small tweak to add compatability with MRI < 1.8.7
16
+
17
+ v1.0
18
+ - Initial Release
@@ -1,4 +1,4 @@
1
- A small class for validating Australian Business Numbers (ABN)
1
+ A small library for validating Australian Business Numbers (ABN)
2
2
 
3
3
  = Installation
4
4
 
@@ -17,6 +17,29 @@ A small class for validating Australian Business Numbers (ABN)
17
17
  ABN.valid?("12042168744")
18
18
  => false
19
19
 
20
+ == With ActiveRecord
21
+
22
+ Version 1.3 of this gem had an active record mixin for validating ABN fields in
23
+ models. I removed it in version 2.0 to avoid complications of supporting active
24
+ record 2.x and 3.x.
25
+
26
+ To validate an ABN field in your model add the ABN gem to your Gemfile and
27
+ use the following pattern:
28
+
29
+ class Business < ActiveRecord::Base
30
+
31
+ validate :validate_abn
32
+
33
+ private
34
+
35
+ def validate_abn
36
+ if self.abn.present && !ABN.valid?(self.abn)
37
+ self.errors.add(:abn, "is not a valid ABN")
38
+ false
39
+ end
40
+ end
41
+ end
42
+
20
43
  = Further Reading
21
44
 
22
45
  - http://www.clearwater.com.au/?action=code
data/lib/abn.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  class ABN
2
2
 
3
3
  module Version #:nodoc:
4
- Major = 1
5
- Minor = 3
4
+ Major = 2
5
+ Minor = 0
6
6
  Tiny = 0
7
7
 
8
8
  String = [Major, Minor, Tiny].join('.')
@@ -43,41 +43,3 @@ 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,55 +1,10 @@
1
- $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup
2
4
 
3
- require 'active_support'
4
- require 'active_record'
5
- require 'spec'
6
- require 'abn'
5
+ require "abn"
7
6
 
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
-
50
- def bad_parameter; (1..11).to_a; end
51
-
52
- describe "The ABN class" do
7
+ describe ABN, "valid? class method" do
53
8
  it "should identify a valid ABN" do
54
9
  ABN.valid?("12042168743").should be_true
55
10
  ABN.valid?(12042168743).should be_true
@@ -75,10 +30,13 @@ describe "The ABN class" do
75
30
  end
76
31
 
77
32
  it "should have a problem with invalid parameter type that has a #length of 11" do
33
+ bad_parameter = (1..11).to_a
78
34
  bad_parameter.length.should eql(11)
79
35
  ABN.valid?(bad_parameter).should be_false
80
36
  end
37
+ end
81
38
 
39
+ describe ABN, "to_s instance method" do
82
40
  it "should be able to format a valid ABN" do
83
41
  ABN.new("12042168743").to_s.should eql("12 042 168 743")
84
42
  end
@@ -94,6 +52,7 @@ describe "The ABN class" do
94
52
  end
95
53
 
96
54
  it "should not format an invalid parameter type that has a #length of 11" do
55
+ bad_parameter = (1..11).to_a
97
56
  bad_parameter.length.should eql(11)
98
57
  ABN.new(bad_parameter).to_s.should eql("")
99
58
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - James Healy
@@ -9,12 +15,69 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-04-16 00:00:00 +10:00
18
+ date: 2010-12-04 00:00:00 +11:00
13
19
  default_executable:
14
- dependencies: []
15
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rcov
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: roodi
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 2
74
+ - 0
75
+ version: "2.0"
76
+ type: :development
77
+ version_requirements: *id004
16
78
  description: a (very) small library for working with Australian Business Numbers.
17
- email: jimmy@deefa.com
79
+ email:
80
+ - jimmy@deefa.com
18
81
  executables: []
19
82
 
20
83
  extensions: []
@@ -23,10 +86,14 @@ extra_rdoc_files: []
23
86
 
24
87
  files:
25
88
  - lib/abn.rb
89
+ - CHANGELOG
26
90
  - MIT-LICENSE
27
91
  - README.rdoc
92
+ - spec/abn_spec.rb
28
93
  has_rdoc: true
29
- homepage: http://github.com/yob/abn/tree/master
94
+ homepage: http://github.com/yob/abn
95
+ licenses: []
96
+
30
97
  post_install_message:
31
98
  rdoc_options:
32
99
  - --title
@@ -35,23 +102,29 @@ rdoc_options:
35
102
  require_paths:
36
103
  - lib
37
104
  required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
38
106
  requirements:
39
107
  - - ">="
40
108
  - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
41
112
  version: "0"
42
- version:
43
113
  required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
44
115
  requirements:
45
116
  - - ">="
46
117
  - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
47
121
  version: "0"
48
- version:
49
122
  requirements: []
50
123
 
51
124
  rubyforge_project: yob-projects
52
- rubygems_version: 1.3.1
125
+ rubygems_version: 1.3.7
53
126
  signing_key:
54
- specification_version: 2
127
+ specification_version: 3
55
128
  summary: a (very) small library for working with Australian Business Numbers.
56
129
  test_files:
57
130
  - spec/abn_spec.rb