class ActiveModel::Validations::NinoValidatorUK

The National Insurance number is a number used in the United Kingdom. The format of the number is two prefix letters, six digits, and one suffix letter. The example used is typically AB123456C. Where AB - prefix, 123456 - number, C - suffix. More details: en.wikipedia.org/wiki/National_Insurance_number

Public Class Methods

new(value) click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 14
def initialize(value)
  @nino        = value.gsub(/\s/, '').upcase                                                  # Remove spaces (they may be) and make text to be same.
  @first_char  = @nino[0]
  @second_char = @nino[1]
  @prefix      = @nino[0..1]
  @number      = @nino[2..7]
  @suffix      = @nino[-1]
end

Public Instance Methods

valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 23
def valid?
  size_is?(9) && first_char_valid? && second_char_valid? && prefix_valid? &&
  prefix_not_allocated? && prefix_not_administrative_number? && number_valid? && suffix_valid?
end

Private Instance Methods

first_char_valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 34
def first_char_valid?
  forbidden_chars = %w[D F I Q U V]
  !forbidden_chars.include?(@first_char)
end
number_valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 49
def number_valid?
  number_rule = /^[0-9]{6}$/                                                                  # Exactly 6 digits.
  !!(number_rule =~ @number)
end
prefix_not_administrative_number?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 64
def prefix_not_administrative_number?
  administrative_prefixes = %w[OO CR FY MW NC PP PY PZ]
  !administrative_prefixes.include?(@prefix)
end
prefix_not_allocated?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 59
def prefix_not_allocated?
  forbidden_prefixes = %w[BG GB NK KN TN NT ZZ]
  !forbidden_prefixes.include?(@prefix)
end
prefix_valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 44
def prefix_valid?
  prefix_rule = /[a-zA-Z]{2}/                                                                 # Exactly 2 alphabet chars.
  !!(prefix_rule =~ @prefix)
end
second_char_valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 39
def second_char_valid?
  forbidden_chars = %w[D F I Q U V O]
  !forbidden_chars.include?(@second_char)
end
size_is?(count) click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 30
def size_is?(count)
  @nino.size == count
end
suffix_valid?() click to toggle source
# File lib/active_validators/active_model/validations/nino_validator.rb, line 54
def suffix_valid?
  allowed_chars = %w[A B C D]
  allowed_chars.include?(@suffix)
end