class NationalIdentificationNumber::Base

Public Class Methods

new(number) click to toggle source
# File lib/national_identification_number/base.rb, line 6
def initialize(number)
  @number = number
  repair
  validate
end

Public Instance Methods

==(other) click to toggle source
# File lib/national_identification_number/base.rb, line 28
def ==(other)
  other.class == self.class && other&.to_s == to_s
end
age() click to toggle source
# File lib/national_identification_number/base.rb, line 24
def age
  age_for_dob date
end
sanitize() click to toggle source
# File lib/national_identification_number/base.rb, line 20
def sanitize
  to_s if valid?
end
to_s() click to toggle source
# File lib/national_identification_number/base.rb, line 16
def to_s
  @number
end
valid?() click to toggle source
# File lib/national_identification_number/base.rb, line 12
def valid?
  !!@valid
end

Protected Instance Methods

age_for_dob(dob) click to toggle source

stackoverflow.com/questions/819263

# File lib/national_identification_number/base.rb, line 35
def age_for_dob(dob)
  today = Time.now.utc.to_date
  return nil unless dob && today >= dob
  today.year - dob.year - ((today.month > dob.month || (today.month == dob.month && today.day >= dob.day)) ? 0 : 1)
end
repair() click to toggle source
# File lib/national_identification_number/base.rb, line 41
def repair
  @number = @number.to_s.upcase.gsub(/\s/, '')
end
validate() click to toggle source
# File lib/national_identification_number/base.rb, line 45
def validate
  raise 'You need to implement at least this method in subclasses.'
end