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

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

Instance Method Summary collapse

Constructor Details

#initialize(str_value, options) ⇒ SinValidatorCanada

Returns a new instance of SinValidatorCanada.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 31

def initialize(str_value, options)
  @sin = str_value.gsub(/\D/, '') # keep only integers
  @allow_permanent_residents = false

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

  if allow_permanent_residents == true
    @allow_permanent_residents = true
  end
end

Instance Method Details

#allow_permanent_residents?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 51

def allow_permanent_residents?
  permanent_residents_indentifier = "9"

  if @allow_permanent_residents == false && @sin.start_with?(permanent_residents_indentifier)
    false
  else
    true
  end
end

#size_is?(count) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 47

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

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/active_validators/active_model/validations/sin_validator.rb', line 43

def valid?
  size_is?(9) && allow_permanent_residents? && LuhnChecker.valid?(@sin)
end