Sha256: db14888d6c025d1fa1d42381d6d6d2f7ab6e8a2e15ad41e7496324cfc32d3b6f

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module CodiceFiscale
  class FiscalCode
    include Helpers
    include Configurable

    attr_accessor :italian_citizen
    alias :citizen :italian_citizen

    delegate :name, :surname, :birthdate, :city_name, :province_code, :country_name, :to => :citizen

    def initialize italian_citizen
      @italian_citizen = italian_citizen
    end

    def calculate
      code = surname_part + name_part + birthdate_part + birthplace_part
      code + control_character(code)
    end

    def surname_part
      first_three_consonants_than_vowels surname
    end

    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

    def birthdate_part
      code = birthdate.year.to_s[2..3]
      code << Codes.month_letter(birthdate.month)
      code << day_part
    end

    def day_part
      number = citizen.female? ? birthdate.day + 40 : birthdate.day
      "#{number}".rjust(2, '0')
    end

    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

    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

    def consonants_of_name
      consonants name.upcase
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
codice-fiscale-1.0.1 lib/codice_fiscale/fiscal_code.rb
codice-fiscale-1.0.0 lib/codice_fiscale/fiscal_code.rb
codice-fiscale-0.0.9 lib/codice_fiscale/fiscal_code.rb
codice-fiscale-0.0.8 lib/codice_fiscale/fiscal_code.rb
codice-fiscale-0.0.7 lib/codice_fiscale/fiscal_code.rb
codice-fiscale-0.0.6 lib/codice_fiscale/fiscal_code.rb