Class: ValidatesIdentity::ArDni::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_identity/ar_dni/validator.rb

Constant Summary collapse

VALIDATION_REGULAR_EXPRESSION =
/^(\d{2}\.?\d{3}\.?\d{3})$/.freeze
FORMAT_REGULAR_EXPRESSION =
/(\d{2})(\d{3})(\d{3})/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Validator

Returns a new instance of Validator.



11
12
13
# File 'lib/validates_identity/ar_dni/validator.rb', line 11

def initialize(value)
  @value = value.to_s
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/validates_identity/ar_dni/validator.rb', line 9

def value
  @value
end

Instance Method Details

#formattedObject



21
22
23
24
25
26
# File 'lib/validates_identity/ar_dni/validator.rb', line 21

def formatted
  return if result.nil?

  result = FORMAT_REGULAR_EXPRESSION.match(striped_value)
  "#{result[1]}.#{result[2]}.#{result[3]}"
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/validates_identity/ar_dni/validator.rb', line 15

def valid?
  return true if value.blank?

  result.present?
end