Class: ActiveModel::Validations::SinValidatorCanada

Inherits:
Object
  • Object
show all
Defined in:
lib/active_validators/active_model/validations/sin_validator.rb

Overview

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}}

Instance Method Summary collapse

Constructor Details

#initialize(str_value, options) ⇒ SinValidatorCanada



33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Method Details

#allow_business_numbers?Boolean



68
69
70
71
72
# 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?Boolean



58
59
60
61
62
63
64
65
66
# 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_zeroesObject



54
55
56
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 54

def is_not_full_of_zeroes
  @sin != '000000000'
end

#size_is?(count) ⇒ Boolean



50
51
52
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 50

def size_is?(count)
  @sin.size == count
end

#valid?Boolean



46
47
48
# 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