Module: CodiceFiscale::Helpers

Included in:
FiscalCode
Defined in:
lib/codice_fiscale/helpers.rb

Instance Method Summary collapse

Instance Method Details

#consonants(string) ⇒ Object



10
11
12
# File 'lib/codice_fiscale/helpers.rb', line 10

def consonants string
  intersection string, Alphabet.consonants
end

#first_three_consonants(string) ⇒ Object



14
15
16
# File 'lib/codice_fiscale/helpers.rb', line 14

def first_three_consonants string
  intersection(string, Alphabet.consonants)[0..2]
end

#first_three_consonants_than_vowels(string) ⇒ Object



26
27
28
29
30
31
# File 'lib/codice_fiscale/helpers.rb', line 26

def first_three_consonants_than_vowels string
  string.upcase!
  code = first_three_consonants string
  code << first_three_vowels(string)
  truncate_and_right_pad_with_three_x code
end

#first_three_vowels(string) ⇒ Object



18
19
20
# File 'lib/codice_fiscale/helpers.rb', line 18

def first_three_vowels string
  intersection(string, Alphabet.vowels)[0..2]
end

#intersection(string_a, string_or_array_b) ⇒ Object



3
4
5
6
7
8
# File 'lib/codice_fiscale/helpers.rb', line 3

def intersection string_a, string_or_array_b
  letters_a = string_a.split ''
  letters_b = string_or_array_b.respond_to?(:split) ? string_or_array_b.split('') : string_or_array_b
  selected_letters = letters_a.select { |letter| letters_b.include? letter }
  selected_letters.join ''
end

#truncate_and_right_pad_with_three_x(string) ⇒ Object



22
23
24
# File 'lib/codice_fiscale/helpers.rb', line 22

def truncate_and_right_pad_with_three_x string
  string[0..2].ljust 3, 'X'
end