Class: ZAIDNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/za_id_number.rb,
lib/za_id_number/version.rb

Defined Under Namespace

Classes: Version

Constant Summary collapse

REQUIRED_ID_LENGTH =
13
FEMALE_RANGE =
(0..4999)
MALE_RANGE =
(5000..9999)
CITIZENSHIP_RANGE =
(0..2)
ZA_CITIZEN =
0
PERMANENT_RESIDENT =
1
REFUGEE =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_num) ⇒ ZAIDNumber



19
20
21
# File 'lib/za_id_number.rb', line 19

def initialize(id_num)
  @id_number = id_num.to_s.freeze
end

Instance Attribute Details

#id_numberObject (readonly) Also known as: to_s

Returns the value of attribute id_number.



16
17
18
# File 'lib/za_id_number.rb', line 16

def id_number
  @id_number
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



89
90
91
92
93
# File 'lib/za_id_number.rb', line 89

def ==(other)
  return false unless other.is_a?(ZAIDNumber)

  return id_number == other.id_number
end

#citizenshipObject



69
70
71
72
73
74
75
# File 'lib/za_id_number.rb', line 69

def citizenship
  case citizenship_status
  when ZA_CITIZEN         then :za_citizen
  when PERMANENT_RESIDENT then :permanent_resident
  when REFUGEE            then :refugee
  end
end

#date_of_birthObject



51
52
53
54
55
# File 'lib/za_id_number.rb', line 51

def date_of_birth
  Date.parse("#{@id_number[0..1]}-#{@id_number[2..3]}-#{@id_number[4..5]}")
rescue ArgumentError
  nil
end

#female?Boolean



61
62
63
# File 'lib/za_id_number.rb', line 61

def female?
  FEMALE_RANGE.include? @id_number[6..9].to_i
end

#genderObject



57
58
59
# File 'lib/za_id_number.rb', line 57

def gender
  female? ? :f : :m
end

#hashObject



96
97
98
# File 'lib/za_id_number.rb', line 96

def hash
  id_number.hash
end

#male?Boolean



65
66
67
# File 'lib/za_id_number.rb', line 65

def male?
  !female?
end

#only_digits?Boolean



39
40
41
# File 'lib/za_id_number.rb', line 39

def only_digits?
  @id_number.to_s.gsub(/\D*/, '') == @id_number.to_s
end

#permanent_resident?Boolean



81
82
83
# File 'lib/za_id_number.rb', line 81

def permanent_resident?
  !za_citizen?
end

#refugee?Boolean



85
86
87
# File 'lib/za_id_number.rb', line 85

def refugee?
  citizenship_status == REFUGEE
end

#valid?Boolean



23
24
25
26
27
28
29
# File 'lib/za_id_number.rb', line 23

def valid?
  valid_length?      &&
  only_digits?       &&
  valid_date?        &&
  valid_citizenship? &&
  valid_checksum?
end

#valid_checksum?Boolean



31
32
33
# File 'lib/za_id_number.rb', line 31

def valid_checksum?
  Luhn.valid? @id_number
end

#valid_citizenship?Boolean



47
48
49
# File 'lib/za_id_number.rb', line 47

def valid_citizenship?
  CITIZENSHIP_RANGE.include? citizenship_status
end

#valid_date?Boolean



43
44
45
# File 'lib/za_id_number.rb', line 43

def valid_date?
  date_of_birth ? true : false
end

#valid_length?Boolean



35
36
37
# File 'lib/za_id_number.rb', line 35

def valid_length?
  @id_number.length == REQUIRED_ID_LENGTH
end

#za_citizen?Boolean



77
78
79
# File 'lib/za_id_number.rb', line 77

def za_citizen?
  citizenship_status == ZA_CITIZEN
end