Class: CodiceFiscale::FiscalCode
- Inherits:
-
Object
- Object
- CodiceFiscale::FiscalCode
show all
- Includes:
- Configurable, Helpers
- Defined in:
- lib/codice_fiscale/fiscal_code.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
config, #config
Methods included from Helpers
#consonants, #first_three_consonants, #first_three_consonants_than_vowels, #first_three_vowels, #intersection, #truncate_and_right_pad_with_three_x
Constructor Details
#initialize(italian_citizen) ⇒ FiscalCode
Returns a new instance of FiscalCode.
11
12
13
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 11
def initialize italian_citizen
@italian_citizen = italian_citizen
end
|
Instance Attribute Details
#italian_citizen ⇒ Object
Also known as:
citizen
Returns the value of attribute italian_citizen.
6
7
8
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 6
def italian_citizen
@italian_citizen
end
|
Instance Method Details
#birthdate_part ⇒ Object
29
30
31
32
33
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 29
def birthdate_part
code = birthdate.year.to_s[2..3]
code << Codes.month_letter(birthdate.month)
code << day_part
end
|
#birthplace_part ⇒ Object
40
41
42
43
44
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 40
def birthplace_part
code = citizen.born_in_italy? && Codes.city(city_name, province_code) || Codes.country(country_name)
raise "Cannot find a valid code for #{[country_name, city_name, province_code].compact.join ', '}" unless code
code
end
|
#calculate ⇒ Object
15
16
17
18
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 15
def calculate
code = surname_part + name_part + birthdate_part + birthplace_part
code + control_character(code)
end
|
#consonants_of_name ⇒ Object
54
55
56
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 54
def consonants_of_name
consonants name.upcase
end
|
#control_character(partial_fiscal_code) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 46
def control_character partial_fiscal_code
numeric_value = 0
partial_fiscal_code.split('').each_with_index do |chr, index|
numeric_value += (index+1).even? ? Codes.even_character(chr) : Codes.odd_character(chr)
end
Codes.control_character numeric_value % 26
end
|
#day_part ⇒ Object
35
36
37
38
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 35
def day_part
number = citizen.female? ? birthdate.day + 40 : birthdate.day
"#{number}".rjust(2, '0')
end
|
#name_part ⇒ Object
24
25
26
27
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 24
def name_part
return "#{consonants_of_name[0]}#{consonants_of_name[2..3]}" if consonants_of_name.size >= 4
first_three_consonants_than_vowels name
end
|
#surname_part ⇒ Object
20
21
22
|
# File 'lib/codice_fiscale/fiscal_code.rb', line 20
def surname_part
first_three_consonants_than_vowels surname
end
|