abn_validator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e4fc93db90a95fde368d005a98e1bf5b4603dcc
4
+ data.tar.gz: 7ab0a73690b83bae2788fe67024060c1ed5ab336
5
+ SHA512:
6
+ metadata.gz: 37871307c5109b26b1d535ae2f56ac4984328ecf606ba1b3ea7846bc26de248290d7deb1030650910c9893ace42a13b15e047108d81cd7a8111028e7e32d8c17
7
+ data.tar.gz: 145a0a33a037795e505b03fedec38927c1b7ef16a3a489c1e6b9c997a646bb3bbdfc0f14a2e7006749fb60f19d652d463143f563692e2a10a3fd8669f9eed955
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rob McFadzean
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ AbnValidator
2
+ ============
3
+
4
+ Super simple Rails validator for checking the validity of ABNs
5
+
6
+ [Format of the ABN | Australian Taxation Office](https://www.ato.gov.au/Business/Australian-business-number/In-detail/Introduction/Format-of-the-ABN/)
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```
14
+ gem 'abn_validator'
15
+ ```
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Usage
22
+ -----
23
+
24
+ ### With ActiveRecord
25
+
26
+ ```
27
+ class Business < ActiveRecord::Base
28
+ validates :abn, abn: true
29
+ end
30
+ ```
31
+
32
+ ### i18n
33
+
34
+ ```
35
+ en:
36
+ errors:
37
+ messages:
38
+ invalid_abn: 'Invalid ABN'
39
+ invalid_abn_length: 'Invalid ABN Length'
40
+ ```
41
+
42
+ Contributing
43
+ ------------
44
+
45
+ 1. Fork it ( https://github.com/wonderbread/abn_validator/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:test)
5
+
6
+ task default: :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'abn_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'abn_validator'
8
+ spec.version = AbnValidator::VERSION
9
+ spec.authors = ['Rob McFadzean']
10
+ spec.email = ['root@sphericalcube.net']
11
+ spec.summary = %q{Rails validator for ABNs}
12
+ spec.description = %q{Rails validator for Australian Business Numbers}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+
25
+ spec.add_runtime_dependency 'activemodel'
26
+ end
@@ -0,0 +1,5 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_abn: 'Invalid ABN'
5
+ invalid_abn_length: 'Invalid ABN Length'
@@ -0,0 +1,7 @@
1
+ require 'active_model/validations/abn_validator'
2
+ require 'active_support/i18n'
3
+ I18n.load_path += Dir.glob(File.expand_path('../../config/locales/**/*',__FILE__))
4
+
5
+
6
+ module AbnValidator
7
+ end
@@ -0,0 +1,3 @@
1
+ module AbnValidator
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_model'
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ class AbnValidator < ActiveModel::EachValidator
6
+
7
+ ABN_WEIGHTS = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
8
+
9
+ def validate_each(record, attribute, value)
10
+
11
+ return if value.nil? || value == ''
12
+
13
+ abn = value.delete(' ').split('').map(&:to_i)
14
+
15
+ if abn.length != 11
16
+ record.errors.add attribute, :invalid_abn_length, value: value
17
+ return
18
+ end
19
+
20
+ abn[0] = abn[0] - 1
21
+ record.errors.add attribute, :invalid_abn, value: value unless abn.zip(ABN_WEIGHTS).map{|a,w|a*w}.inject(:+)/89.00 % 1 == 0
22
+
23
+ end
24
+ end
25
+
26
+
27
+ module HelperMethods
28
+
29
+ def validates_abn(*attr_names)
30
+ validates_with AbnValidator, _merge_attributes(attr_names)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ describe AbnValidator do
4
+
5
+ subject { TestBusiness.new }
6
+
7
+ it 'should be ok with nil ABNs' do
8
+ expect(subject.valid?).to be(true)
9
+ end
10
+
11
+ it 'should invalidate invalid ABNs' do
12
+ INVALID_ABNS.each do |abn|
13
+ subject.abn = abn
14
+ expect(subject.valid?).to be(false)
15
+ end
16
+ end
17
+
18
+ it 'should be ok with valid ABNs' do
19
+ VALID_ABNS.each do |abn|
20
+ subject.abn = abn
21
+ expect(subject.valid?).to be(true)
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,20 @@
1
+ require 'abn_validator'
2
+
3
+ INVALID_ABNS = [
4
+ 'AB CDE FGH JKI',
5
+ '11 111 111 111',
6
+ '53 004 085 615'
7
+ ]
8
+
9
+ VALID_ABNS = [
10
+ '53 004 085 616',
11
+ '34000908716'
12
+ ]
13
+
14
+ class TestBusiness
15
+
16
+ include ActiveModel::Validations
17
+ attr_accessor :abn
18
+ validates :abn, abn: true
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abn_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob McFadzean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails validator for Australian Business Numbers
70
+ email:
71
+ - root@sphericalcube.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - abn_validator.gemspec
82
+ - config/locales/en.yml
83
+ - lib/abn_validator.rb
84
+ - lib/abn_validator/version.rb
85
+ - lib/active_model/validations/abn_validator.rb
86
+ - spec/abn_validator_spec.rb
87
+ - spec/helper.rb
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Rails validator for ABNs
112
+ test_files:
113
+ - spec/abn_validator_spec.rb
114
+ - spec/helper.rb