Class: IdNumberLatam::EcDni
Instance Attribute Summary collapse
-
#digit ⇒ Object
readonly
Returns the value of attribute digit.
-
#third_digit ⇒ Object
readonly
Returns the value of attribute third_digit.
Attributes inherited from Base
#country, #id_number, #unformatted_id_number
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(id_number, opts = {}) ⇒ EcDni
constructor
A new instance of EcDni.
- #unformat ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from Base
Constructor Details
#initialize(id_number, opts = {}) ⇒ EcDni
Returns a new instance of EcDni.
7 8 9 10 11 12 |
# File 'lib/id_number_latam/ec_dni.rb', line 7 def initialize(id_number, opts = {}) super @country = :ec @third_digit = unformat[2] @digit = @third_digit == "6" ? unformat[8] : unformat[9] end |
Instance Attribute Details
#digit ⇒ Object (readonly)
Returns the value of attribute digit.
5 6 7 |
# File 'lib/id_number_latam/ec_dni.rb', line 5 def digit @digit end |
#third_digit ⇒ Object (readonly)
Returns the value of attribute third_digit.
5 6 7 |
# File 'lib/id_number_latam/ec_dni.rb', line 5 def third_digit @third_digit end |
Instance Method Details
#format ⇒ Object
14 15 16 17 18 |
# File 'lib/id_number_latam/ec_dni.rb', line 14 def format dni = unformat digit = dni.slice!(-1) [dni, digit].join("-") end |
#unformat ⇒ Object
20 21 22 |
# File 'lib/id_number_latam/ec_dni.rb', line 20 def unformat id_number.gsub(/\D/, "") end |
#valid? ⇒ Boolean
24 25 26 27 28 29 30 31 |
# File 'lib/id_number_latam/ec_dni.rb', line 24 def valid? return false unless valid_length return false unless unformat[0..1].to_i <= 24 return false unless [0, 1, 2, 3, 4, 5, 6, 9].include?(unformat[2].to_i) dni = unformat[0..8] (modulo - (dni.chars.zip(coefficient.chars).map { |p| p.map(&:to_i).inject(&:*) }.map { |r| operation_block(r) }.sum % modulo)).to_s == @digit end |