Module: Validator::Nric

Defined in:
lib/validator/nric.rb,
lib/validator/nric/version.rb,
lib/validator/nric/nric_validator.rb

Defined Under Namespace

Classes: NricValidator

Constant Summary collapse

WEIGHT =

The multiplier for each individual digit

[2, 7, 6, 5, 4, 3, 2]
S_TABLE =

Century prefix lookup tables

S - Born before 2000. S is the 19th alphabet denoting (1900 - 1999) T - Born in 2000 and beyong. (2000 - 2099??) F - Foreigners with pass issued before 2000 G - Foreigners with pass issued after 2000

Notice the it is backward of ABCDEFGHIZJ For T and G, there is a shift of 4 places

%w(J Z I H G F E D C B A)
T_TABLE =
%w(G F E D C B A J Z I H)
F_TABLE =
%w(X W U T R Q P N M L K)
G_TABLE =
%w(R Q P N M L K X W U T)
VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.check(nric) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/validator/nric.rb', line 40

def check(nric)
  return false if nric.nil? || nric.size != 9
  nric.upcase!

  century_prefix = nric[0, 1]
  ic_number = nric[1, 7]
  check_character = nric[8, 1]

  lookup(century_prefix, mod(ic_number)) == check_character
end

.lookup(century_prefix, position) ⇒ Object



36
37
38
# File 'lib/validator/nric.rb', line 36

def lookup(century_prefix, position)
  module_eval("#{century_prefix}_TABLE")[position]
end

.mod(value) ⇒ Object



31
32
33
34
# File 'lib/validator/nric.rb', line 31

def mod(value)
  ic_array = value.each_char.map(&:to_i)
  ic_array.zip(Validator::Nric::WEIGHT).map { |a, b| a * b }.inject(:+) % 11
end