Class: SpainDni::Dni
- Inherits:
-
Object
- Object
- SpainDni::Dni
- Defined in:
- lib/spain_dni.rb
Instance Attribute Summary collapse
-
#id_number ⇒ Object
Returns the value of attribute id_number.
Instance Method Summary collapse
- #calculate_check ⇒ Object
- #check_letter ⇒ Object
- #dni? ⇒ Boolean
- #id_type ⇒ Object
-
#initialize(id_number) ⇒ Dni
constructor
A new instance of Dni.
- #nie? ⇒ Boolean
- #valid? ⇒ Boolean
- #valid_letter ⇒ Object
Constructor Details
#initialize(id_number) ⇒ Dni
Returns a new instance of Dni.
8 9 10 |
# File 'lib/spain_dni.rb', line 8 def initialize(id_number) @id_number = id_number.to_s end |
Instance Attribute Details
#id_number ⇒ Object
Returns the value of attribute id_number.
6 7 8 |
# File 'lib/spain_dni.rb', line 6 def id_number @id_number end |
Instance Method Details
#calculate_check ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/spain_dni.rb', line 24 def calculate_check nie_letters = { X: 0, Y: 1, Z: 2 } number = if nie? [nie_letters[@id_number[0].to_sym], @id_number[1..-2]].join else @id_number.chop end number.to_i % 23 end |
#check_letter ⇒ Object
20 21 22 |
# File 'lib/spain_dni.rb', line 20 def check_letter @id_number[-1] end |
#dni? ⇒ Boolean
12 13 14 |
# File 'lib/spain_dni.rb', line 12 def dni? !@id_number[0].match(/\d/).nil? end |
#id_type ⇒ Object
50 51 52 53 |
# File 'lib/spain_dni.rb', line 50 def id_type return nil unless valid? nie? ? 'NIE' : 'DNI' end |
#nie? ⇒ Boolean
16 17 18 |
# File 'lib/spain_dni.rb', line 16 def nie? @id_number[0].match(/\d/).nil? end |
#valid? ⇒ Boolean
46 47 48 |
# File 'lib/spain_dni.rb', line 46 def valid? check_letter == valid_letter end |
#valid_letter ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/spain_dni.rb', line 34 def valid_letter verifiers = { 0 => 'T', 1 => 'R', 2 => 'W', 3 => 'A', 4 => 'G', 5 => 'M', 6 => 'Y', 7 => 'F', 8 => 'P', 9 => 'D', 10 => 'X', 11 => 'B', 12 => 'N', 13 => 'J', 14 => 'Z' ,15 => 'S', 16 => 'Q', 17 => 'V', 18 => 'H', 19 => 'L', 20 => 'C', 21 => 'K', 22 => 'E' } verifiers.each do |key, value| return value if calculate_check.to_i == key end end |