Class: Cfita::CodiceFiscale

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

Overview

Controllo codice fiscale italiano

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fiscal_code, birth_place: nil, birth_date: nil, name: nil, surname: nil, sex: nil) ⇒ CodiceFiscale

Returns a new instance of CodiceFiscale.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cfita/codice_fiscale.rb', line 19

def initialize(
  fiscal_code,
  birth_place: nil,
  birth_date: nil,
  name: nil,
  surname: nil,
  sex: nil
)
  @fiscal_code = fiscal_code.to_s.upcase.strip
  @birth_place = birth_place&.upcase
  @birth_date = birth_date
  @birth_date = Date.parse(birth_date) if birth_date.is_a?(String)
  @name = name&.parameterize&.upcase
  @surname = surname&.parameterize&.upcase
  @sex = sex&.upcase
  @errors = []
  parse
end

Instance Attribute Details

#birth_dateObject (readonly)

Returns the value of attribute birth_date.



9
10
11
# File 'lib/cfita/codice_fiscale.rb', line 9

def birth_date
  @birth_date
end

#birth_placeObject (readonly)

Returns the value of attribute birth_place.



9
10
11
# File 'lib/cfita/codice_fiscale.rb', line 9

def birth_place
  @birth_place
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/cfita/codice_fiscale.rb', line 9

def errors
  @errors
end

#fiscal_codeObject (readonly)

Returns the value of attribute fiscal_code.



9
10
11
# File 'lib/cfita/codice_fiscale.rb', line 9

def fiscal_code
  @fiscal_code
end

#sexObject (readonly)

Returns the value of attribute sex.



9
10
11
# File 'lib/cfita/codice_fiscale.rb', line 9

def sex
  @sex
end

Class Method Details

.ccatObject



15
16
17
# File 'lib/cfita/codice_fiscale.rb', line 15

def self.ccat
  @ccat ||= JSON.parse(open('ccat.json'))
end

Instance Method Details

#cf_birth_dateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cfita/codice_fiscale.rb', line 66

def cf_birth_date
  return @cf_birth_date if @cf_birth_date

  yy = cifre(6..7)
  day = cifre(9..10)
  return unless yy && day
  @errors << 'Cifra decina giorno di nascita errata' if day && day > 71

  month = MESI.index(@fiscal_code[8])
  @errors << 'Mese errato' unless month
  return unless yy && month && day

  @cf_birth_date = Date.new(yy2yyyy(yy), month + 1, day % 40) rescue nil
end

#cf_birth_placesObject



60
61
62
63
64
# File 'lib/cfita/codice_fiscale.rb', line 60

def cf_birth_places
  return @cf_birth_places if @cf_birth_places

  @cf_birth_places = CODICI_CATASTALI[codice_catastale]
end

#cf_sexObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cfita/codice_fiscale.rb', line 46

def cf_sex
  return @cf_sex if @cf_sex
  case @fiscal_code[9]
  when nil
  when /[0-3LMNP]/
    @cf_sex = 'M'
  when /[4-7QRST]/
    @cf_sex = 'F'
  else
    @errors << 'Cifra decina giorno di nascita errata'
    nil
  end
end

#to_sObject



38
39
40
# File 'lib/cfita/codice_fiscale.rb', line 38

def to_s
  fiscal_code
end

#valid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cfita/codice_fiscale.rb', line 42

def valid?
  errors.empty?
end