module Validator::Nric
Constants
- F_TABLE
- G_TABLE
- 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
- T_TABLE
- VERSION
- WEIGHT
The multiplier for each individual digit
Public Instance Methods
check(nric)
click to toggle source
# File lib/validator/nric.rb, line 40 def check(nric) return false if nric.nil? || nric.size != 9 nric = nric.upcase return false unless %w(S T F G).include?(nric[0]) 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)
click to toggle source
# File lib/validator/nric.rb, line 36 def lookup(century_prefix, position) module_eval("#{century_prefix}_TABLE")[position] end
mod(value)
click to toggle source
# 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