Class: Nifval::NifValidator::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/nifval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nif) ⇒ Document

Returns a new instance of Document.



17
18
19
20
21
# File 'lib/nifval.rb', line 17

def initialize(nif)
  @nif = nif

  setup
end

Instance Attribute Details

#nifObject

Returns the value of attribute nif.



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

def nif
  @nif
end

Instance Method Details

#cif?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/nifval.rb', line 55

def cif?
  nif.match(/^[ABCDEFGHJNPQRSUVW]{1}/)
end

#invalid_format?Boolean

Returns:

  • (Boolean)


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

def invalid_format?
  !nif.match(/^[A-Z]{1}\d{7}[A-Z0-9]{1}$/) && !nif.match(/^[0-9]{8}[A-Z]{1}$/)
end

#nie?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/nifval.rb', line 59

def nie?
  nif.match(/^[XYZ]{1}/)
end

#special?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/nifval.rb', line 51

def special?
  nif.match(/^[KLM]{1}/)
end

#standard?Boolean

Returns:

  • (Boolean)


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

def standard?
  nif.match(/^[0-9]{8}[A-Z]{1}$/)
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
# File 'lib/nifval.rb', line 23

def valid?
  case
  when invalid_format? then false
  when standard? then valid_standard?
  when special? then valid_special?
  when cif? then valid_cif?
  when nie? then valid_nie?
  else false
  end
end

#valid_cif?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/nifval.rb', line 63

def valid_cif?
  nstr = cif_algorithm_value.to_s
  (ival(nif[8]) == (64+cif_algorithm_value).chr) || (nif[8] == nstr[nstr.length-1])
end

#valid_nie?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/nifval.rb', line 68

def valid_nie?
  niff = nif.gsub("X","0").gsub("Y","1").gsub("Z","2")
  nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[niff[0..7].to_i % 23]
end

#valid_special?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/nifval.rb', line 73

def valid_special?
  nif[8] == (64+cif_algorithm_value).chr
end

#valid_standard?Boolean

Returns:

  • (Boolean)


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

def valid_standard?
  nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[nif[0..7].to_i % 23]
end