validator-nric 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.rubocop.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/lib/validator/nric/nric_validator.rb +10 -0
- data/lib/validator/nric/version.rb +5 -0
- data/lib/validator/nric.rb +51 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/validator/nric_spec.rb +39 -0
- data/validator-nric.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a138d8840913e8bc431d4ca286c3fdf5fe35c265
|
4
|
+
data.tar.gz: 915f6c91440240d59a2dc1cfef77571ac6d377eb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b33edd940a4c26cd6949410e57710a6bdab1d232721c0a4d4ebc42665f7479a28d1274a0862a33623bff05a6dce8bd96cabd3c7af865d71f13d1f4b31a427771
|
7
|
+
data.tar.gz: f038c423f9d61015a064159dc72f91a3f560f842d351957686ba48c31d30fe6964360c55f327f4aebb5e71cc346e8e1875d6118d99702d57b5f30adc1a2344ed
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 mech
|
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,41 @@
|
|
1
|
+
# Validator for Singapore NRIC
|
2
|
+
|
3
|
+
A standalone NRIC validator as well as ActiveModel validator for use in your Rails models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'validator-nric'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
As a standalone validator:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Validator::Nric.check('SXXXXXXXC') # => true or false
|
19
|
+
```
|
20
|
+
|
21
|
+
Use with ActiveModel model:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class User < ActiveRecord::Base
|
25
|
+
validates :identity_card, nric: true, presence: true, uniqueness: true
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
## Credits
|
30
|
+
|
31
|
+
* [Wiki on NRIC](http://en.wikipedia.org/wiki/National_Registration_Identity_Card)
|
32
|
+
* [Fun with numbers](http://www.ngiam.net/NRIC/)
|
33
|
+
* [All about NRIC number in Singapore](http://www.arjun.com.np/blog/all-about-nric-number-in-singapore/)
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( https://github.com/mech/validator-nric/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
module Validator
|
2
|
+
module Nric
|
3
|
+
class NricValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
record.errors[attribute] << (options[:message] || 'is not an NRIC') \
|
6
|
+
unless Validator::Nric.check(value)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'validator/nric/version'
|
2
|
+
|
3
|
+
if defined?(ActiveModel)
|
4
|
+
require 'validator/nric/nric_validator'
|
5
|
+
|
6
|
+
# Play nice with ActiveModel custom validator expectation
|
7
|
+
NricValidator = Validator::Nric::NricValidator
|
8
|
+
end
|
9
|
+
|
10
|
+
module Validator
|
11
|
+
module Nric
|
12
|
+
module_function
|
13
|
+
|
14
|
+
# The multiplier for each individual digit
|
15
|
+
WEIGHT = [2, 7, 6, 5, 4, 3, 2]
|
16
|
+
|
17
|
+
# Century prefix lookup tables
|
18
|
+
#
|
19
|
+
# S - Born before 2000. S is the 19th alphabet denoting (1900 - 1999)
|
20
|
+
# T - Born in 2000 and beyong. (2000 - 2099??)
|
21
|
+
# F - Foreigners with pass issued before 2000
|
22
|
+
# G - Foreigners with pass issued after 2000
|
23
|
+
#
|
24
|
+
# Notice the it is backward of ABCDEFGHIZJ
|
25
|
+
# For T and G, there is a shift of 4 places
|
26
|
+
S_TABLE = %w(J Z I H G F E D C B A)
|
27
|
+
T_TABLE = %w(G F E D C B A J Z I H)
|
28
|
+
F_TABLE = %w(X W U T R Q P N M L K)
|
29
|
+
G_TABLE = %w(R Q P N M L K X W U T)
|
30
|
+
|
31
|
+
def mod(value)
|
32
|
+
ic_array = value.each_char.map(&:to_i)
|
33
|
+
ic_array.zip(Validator::Nric::WEIGHT).map { |a, b| a * b }.inject(:+) % 11
|
34
|
+
end
|
35
|
+
|
36
|
+
def lookup(century_prefix, position)
|
37
|
+
module_eval("#{century_prefix}_TABLE")[position]
|
38
|
+
end
|
39
|
+
|
40
|
+
def check(nric)
|
41
|
+
return false if nric.nil? || nric.size != 9
|
42
|
+
nric.upcase!
|
43
|
+
|
44
|
+
century_prefix = nric[0, 1]
|
45
|
+
ic_number = nric[1, 7]
|
46
|
+
check_character = nric[8, 1]
|
47
|
+
|
48
|
+
lookup(century_prefix, mod(ic_number)) == check_character
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'validator/nric'
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
6
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
7
|
+
# files.
|
8
|
+
#
|
9
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
10
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
11
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
12
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
13
|
+
# a separate helper file that requires the additional dependencies and performs
|
14
|
+
# the additional setup, and require it from the spec files that actually need
|
15
|
+
# it.
|
16
|
+
#
|
17
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
18
|
+
# users commonly want.
|
19
|
+
#
|
20
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# rspec-expectations config goes here. You can use an alternate
|
23
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
24
|
+
# assertions if you prefer.
|
25
|
+
config.expect_with :rspec do |expectations|
|
26
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
27
|
+
# and `failure_message` of custom matchers include text for helper methods
|
28
|
+
# defined using `chain`, e.g.:
|
29
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
30
|
+
# # => "be bigger than 2 and smaller than 4"
|
31
|
+
# ...rather than:
|
32
|
+
# # => "be bigger than 2"
|
33
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
34
|
+
end
|
35
|
+
|
36
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
37
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
38
|
+
config.mock_with :rspec do |mocks|
|
39
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
40
|
+
# a real object. This is generally recommended, and will default to
|
41
|
+
# `true` in RSpec 4.
|
42
|
+
mocks.verify_partial_doubles = true
|
43
|
+
end
|
44
|
+
|
45
|
+
# These two settings work together to allow you to limit a spec run
|
46
|
+
# to individual examples or groups you care about by tagging them with
|
47
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
48
|
+
# get run.
|
49
|
+
config.filter_run :focus
|
50
|
+
config.run_all_when_everything_filtered = true
|
51
|
+
|
52
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
53
|
+
# recommended.
|
54
|
+
# For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
RSpec.describe Validator::Nric do
|
2
|
+
|
3
|
+
describe 'check' do
|
4
|
+
it 'returns false if nric is nil' do
|
5
|
+
expect(subject.check(nil)).to be false
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'returns false if nric has less than 9 characters' do
|
9
|
+
expect(subject.check('ABC')).to be false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'mod' do
|
14
|
+
it 'sums the product of IC number with weighted array and % 11' do
|
15
|
+
expect(subject.mod('1111111')).to eq 7
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sums the product of IC number with weighted array and % 11' do
|
19
|
+
expect(subject.mod('1234567')).to eq 7
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'lookup' do
|
24
|
+
it 'retrieves the compared character from lookup table' do
|
25
|
+
expect(subject.lookup('S', 0)).to eq 'J'
|
26
|
+
expect(subject.lookup('S', 1)).to eq 'Z'
|
27
|
+
expect(subject.lookup('S', 2)).to eq 'I'
|
28
|
+
expect(subject.lookup('S', 3)).to eq 'H'
|
29
|
+
expect(subject.lookup('S', 4)).to eq 'G'
|
30
|
+
expect(subject.lookup('S', 5)).to eq 'F'
|
31
|
+
expect(subject.lookup('S', 6)).to eq 'E'
|
32
|
+
expect(subject.lookup('S', 7)).to eq 'D'
|
33
|
+
expect(subject.lookup('S', 8)).to eq 'C'
|
34
|
+
expect(subject.lookup('S', 9)).to eq 'B'
|
35
|
+
expect(subject.lookup('S', 10)).to eq 'A'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validator/nric/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'validator-nric'
|
8
|
+
spec.version = Validator::Nric::VERSION
|
9
|
+
spec.authors = ['mech']
|
10
|
+
spec.email = ['mech@me.com']
|
11
|
+
spec.summary = 'Validates Singapore NRIC'
|
12
|
+
spec.description = 'Global validator as well as ActiveModel validator'
|
13
|
+
spec.homepage = 'https://github.com/mech/validator-nric'
|
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.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.1'
|
24
|
+
spec.add_development_dependency 'rubocop'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validator-nric
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mech
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Global validator as well as ActiveModel validator
|
70
|
+
email:
|
71
|
+
- mech@me.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/validator/nric.rb
|
84
|
+
- lib/validator/nric/nric_validator.rb
|
85
|
+
- lib/validator/nric/version.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/validator/nric_spec.rb
|
88
|
+
- validator-nric.gemspec
|
89
|
+
homepage: https://github.com/mech/validator-nric
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Validates Singapore NRIC
|
113
|
+
test_files:
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/validator/nric_spec.rb
|