uk_validators 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uk_validators.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tony Headford
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.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # UK Validators
2
+
3
+ This library contains validators for the following UK-centric data:
4
+
5
+ * National Insurance Number
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'uk_validators'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ ### National Insurance Number (Nino)
21
+
22
+ Simply add the following to your model
23
+
24
+ validates :my_nino_attribute, nino: true
25
+
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ["lib"]
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
@@ -0,0 +1,19 @@
1
+ class NinoValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ record.errors.add(attribute, invalid_message(record, attribute)) unless valid_nino?(value)
5
+ end
6
+
7
+ private
8
+
9
+ def valid_nino?(value)
10
+ value =~ /^[A-CEGHJ-PRSTW-Z][A-CEGHJ-NPRSTW-Z][0-9]{6}[A-D\s]$/i && value !~ /^(GB|BG|NK|KN|TN|NT|ZZ)/i
11
+ end
12
+
13
+ def invalid_message(record, attribute)
14
+ I18n.t :nino,
15
+ scope: "#{record.class.i18n_scope}.errors.models.#{record.class.model_name.i18n_key}.attributes.#{attribute}",
16
+ default: "is not a valid National Insurance No."
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module UkValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "uk_validators/version"
2
+ require "uk_validators/nino_validator"
3
+
4
+ module UkValidators
5
+ end
data/test/nino_test.rb ADDED
@@ -0,0 +1,93 @@
1
+ require_relative 'test_helper'
2
+
3
+ class TestNinoValidator < MiniTest::Unit::TestCase
4
+ class User < TestModel
5
+ validates :ni_number, nino: true
6
+ end
7
+
8
+ def test_valid_ninos
9
+ valid_ninos.each { |nino| assert User.new(ni_number: nino).valid?, "Bad nino #{nino}" }
10
+ end
11
+
12
+ def test_invalid_ninos
13
+ invalid_ninos.each { |nino| assert User.new(ni_number: nino).invalid? }
14
+ end
15
+
16
+ private
17
+
18
+ def valid_ninos
19
+ [
20
+ 'aa123456a',
21
+ 'bb940321b',
22
+ 'cc987654c',
23
+ 'ee903221d',
24
+ 'gg849302 ', # apparently space is valid for the final character
25
+ 'hh123849a',
26
+ 'jj123849b',
27
+ 'kk123849c',
28
+ 'll123849d',
29
+ 'mm123849 ',
30
+ 'nn123849a',
31
+ 'on123849b',
32
+ 'pp123849c',
33
+ 'rr123849d',
34
+ 'ss123849 ',
35
+ 'tt123849a',
36
+ 'ww123849b',
37
+ 'xx123849c',
38
+ 'yy123849d',
39
+ 'za123849 '
40
+ ]
41
+ end
42
+
43
+ def invalid_ninos
44
+ [
45
+ 'aa123456e',
46
+ 'bb940321f',
47
+ 'cc987654g',
48
+ 'ee903221h',
49
+ 'gg849302i',
50
+ 'hh123849j',
51
+ 'jj123849k',
52
+ 'kk123849l',
53
+ 'll123849m',
54
+ 'mm123849n',
55
+ 'nn123849o',
56
+ 'on123849p',
57
+ 'pp123849q',
58
+ 'rr123849r',
59
+ 'ss123849s',
60
+ 'tt123849t',
61
+ 'ww123849u',
62
+ 'xx123849v',
63
+ 'yy123849w',
64
+ 'zz123849x',
65
+ 'aa123456y',
66
+ 'bb940321z',
67
+ 'cc9854c',
68
+ 'e',
69
+ 'ga',
70
+ '1w',
71
+ 'a ',
72
+ 'a1',
73
+ ' ',
74
+ '2s123456a',
75
+ 'a4123849c',
76
+ 'll 23849d',
77
+ 'mm1e3849 ',
78
+ 'nn12f849a',
79
+ 'on123b49b',
80
+ 'pp1238v9c',
81
+ 'rr12389md',
82
+ 's1238492a',
83
+ 'gb123849a',
84
+ 'bg123849b',
85
+ 'nk123849c',
86
+ 'kn123849d',
87
+ 'tn123849a',
88
+ 'nt123849b',
89
+ 'zz123849c'
90
+ ]
91
+ end
92
+ end
93
+
@@ -0,0 +1,15 @@
1
+ require 'active_model'
2
+ require 'minitest/autorun'
3
+ require 'uk_validators'
4
+
5
+ class TestModel
6
+ include ActiveModel::Validations
7
+
8
+ def initialize(attributes = {})
9
+ @attributes = attributes
10
+ end
11
+
12
+ def read_attribute_for_validation(key)
13
+ @attributes[key]
14
+ end
15
+ end
@@ -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 'uk_validators/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "uk_validators"
8
+ spec.version = UkValidators::VERSION
9
+ spec.authors = ["Tony Headford"]
10
+ spec.email = ["tony@binarycircus.com"]
11
+ spec.description = %q{Rails Validations for typically UK specific data}
12
+ spec.summary = %q{Rails validations for UK data }
13
+ spec.homepage = "https://github.com/tonyheadford/uk_validators"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency "activemodel", ">= 3.2"
26
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uk_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tony Headford
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-04 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: '1.3'
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: '1.3'
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: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activemodel
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '3.2'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '3.2'
78
+ description: Rails Validations for typically UK specific data
79
+ email:
80
+ - tony@binarycircus.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/uk_validators.rb
91
+ - lib/uk_validators/nino_validator.rb
92
+ - lib/uk_validators/version.rb
93
+ - test/nino_test.rb
94
+ - test/test_helper.rb
95
+ - uk_validators.gemspec
96
+ homepage: https://github.com/tonyheadford/uk_validators
97
+ licenses:
98
+ - MIT
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 1782607928652499814
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: 1782607928652499814
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 1.8.25
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Rails validations for UK data
127
+ test_files:
128
+ - test/nino_test.rb
129
+ - test/test_helper.rb