Class: ZAIDNumber
- Inherits:
-
Object
- Object
- ZAIDNumber
- 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..1)
- ZA_CITIZEN =
0- PERMANENT_RESIDENT =
1
Instance Attribute Summary collapse
-
#id_number ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute id_number.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #citizenship ⇒ Object
- #date_of_birth ⇒ Object
- #female? ⇒ Boolean
- #gender ⇒ Object
- #hash ⇒ Object
-
#initialize(id_num) ⇒ ZAIDNumber
constructor
A new instance of ZAIDNumber.
- #male? ⇒ Boolean
- #only_digits? ⇒ Boolean
- #permanent_resident? ⇒ Boolean
- #valid? ⇒ Boolean
- #valid_checksum? ⇒ Boolean
- #valid_citizenship? ⇒ Boolean
- #valid_date? ⇒ Boolean
- #valid_length? ⇒ Boolean
- #za_citizen? ⇒ Boolean
Constructor Details
#initialize(id_num) ⇒ ZAIDNumber
Returns a new instance of ZAIDNumber.
18 19 20 |
# File 'lib/za_id_number.rb', line 18 def initialize(id_num) @id_number = id_num.to_s.freeze end |
Instance Attribute Details
#id_number ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute id_number.
15 16 17 |
# File 'lib/za_id_number.rb', line 15 def id_number @id_number end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
80 81 82 83 84 |
# File 'lib/za_id_number.rb', line 80 def ==(other) return false unless other.is_a?(ZAIDNumber) return id_number == other.id_number end |
#citizenship ⇒ Object
68 69 70 |
# File 'lib/za_id_number.rb', line 68 def citizenship za_citizen? ? :za_citizen : :permanent_resident end |
#date_of_birth ⇒ Object
50 51 52 53 54 |
# File 'lib/za_id_number.rb', line 50 def date_of_birth Date.parse("#{@id_number[0..1]}-#{@id_number[2..3]}-#{@id_number[4..5]}") rescue ArgumentError nil end |
#female? ⇒ Boolean
60 61 62 |
# File 'lib/za_id_number.rb', line 60 def female? FEMALE_RANGE.include? @id_number[6..9].to_i end |
#gender ⇒ Object
56 57 58 |
# File 'lib/za_id_number.rb', line 56 def gender female? ? :f : :m end |
#hash ⇒ Object
87 88 89 |
# File 'lib/za_id_number.rb', line 87 def hash id_number.hash end |
#male? ⇒ Boolean
64 65 66 |
# File 'lib/za_id_number.rb', line 64 def male? !female? end |
#only_digits? ⇒ Boolean
38 39 40 |
# File 'lib/za_id_number.rb', line 38 def only_digits? @id_number.to_s.gsub(/\D*/, '') == @id_number.to_s end |
#permanent_resident? ⇒ Boolean
76 77 78 |
# File 'lib/za_id_number.rb', line 76 def permanent_resident? !za_citizen? end |
#valid? ⇒ Boolean
22 23 24 25 26 27 28 |
# File 'lib/za_id_number.rb', line 22 def valid? valid_length? && only_digits? && valid_date? && valid_citizenship? && valid_checksum? end |
#valid_checksum? ⇒ Boolean
30 31 32 |
# File 'lib/za_id_number.rb', line 30 def valid_checksum? Luhn.valid? @id_number end |
#valid_citizenship? ⇒ Boolean
46 47 48 |
# File 'lib/za_id_number.rb', line 46 def valid_citizenship? CITIZENSHIP_RANGE.include? @id_number[10].to_i end |
#valid_date? ⇒ Boolean
42 43 44 |
# File 'lib/za_id_number.rb', line 42 def valid_date? date_of_birth ? true : false end |
#valid_length? ⇒ Boolean
34 35 36 |
# File 'lib/za_id_number.rb', line 34 def valid_length? @id_number.length == REQUIRED_ID_LENGTH end |
#za_citizen? ⇒ Boolean
72 73 74 |
# File 'lib/za_id_number.rb', line 72 def za_citizen? @id_number[10].to_i == ZA_CITIZEN end |