class ActiveModel::Validations::SinValidatorCanada

The Social Insurance Number is a nine-digit number in the format “AAA BBB CCC”. The number is divided into three parts. AAA - is the first, BBB - is the second and the CCC is the third. Numbers that begin with the number “9” are issued to temporary residents who are not Canadian citizens. Numbers that begin with the number “8” are issued to businesses as “Business Numbers”. More details: en.wikipedia.org/wiki/Social_Insurance_Number

Possible flags:

allow_permanent_residents - citizens with cars, which begins with "9" are valid. By default it is false.
allow_business_numbers - business numbers, which begins with "8" are valid. By default it is false.

validates :sin, :sin => {:country => :canada, :country_options => {:allow_permanent_residents => true, :allow_business_numbers => true}}

Public Class Methods

new(str_value, options) click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 33
def initialize(str_value, options)
  @sin = str_value.gsub(/\D/, '') # keep only integers
  @allow_permanent_residents = false
  @allow_business_numbers = false

  country_options = options.fetch(:country_options, {})
  allow_permanent_residents = country_options.fetch(:allow_permanent_residents, :nil)
  allow_business_numbers = country_options.fetch(:allow_business_numbers, :nil)

  @allow_permanent_residents = true if allow_permanent_residents == true
  @allow_business_numbers = true if allow_business_numbers == true
end

Public Instance Methods

allow_business_numbers?() click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 68
def allow_business_numbers?
  business_indentifier = '8'

  !(@allow_business_numbers == false && @sin.start_with?(business_indentifier))
end
allow_permanent_residents?() click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 58
def allow_permanent_residents?
  permanent_residents_indentifier = "9"

  if @allow_permanent_residents == false && @sin.start_with?(permanent_residents_indentifier)
    false
  else
    true
  end
end
is_not_full_of_zeroes() click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 54
def is_not_full_of_zeroes
  @sin != '000000000'
end
size_is?(count) click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 50
def size_is?(count)
  @sin.size == count
end
valid?() click to toggle source
# File lib/active_validators/active_model/validations/sin_validator.rb, line 46
def valid?
  size_is?(9) && is_not_full_of_zeroes && allow_permanent_residents? && allow_business_numbers? && LuhnChecker.valid?(@sin)
end