Sha256: 008ff4624e44c912691fcb824a7ad925f06f7e52b387e482c7ab8562313e56c5

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

module CodiceFiscale
  class ItalianCitizen
    include ActiveModel::Validations

    MANDATORY_ATTRIBUTES = [:name, :surname, :birthdate, :gender]

    attr_accessor :city_name, :country_name, :province_code, *MANDATORY_ATTRIBUTES

    # Validations

    validates_presence_of *MANDATORY_ATTRIBUTES
    validates_inclusion_of :gender, :in => [:male, :female]
    validates_length_of :province_code, :is => 2, :allow_blank => true
    validates_presence_of :city_name, :province_code, :if => lambda{ |obj| obj.born_in_italy? }
    validate do
      errors.add(:birthdate, :invalid) unless birthdate.respond_to? :year
    end


    # Instance methods

    def initialize attributes
      @attributes = default_attributes.merge attributes
      @attributes.each { |name, value| send("#{name}=", value) }
    end

    def default_attributes
      {:country_name => Codes::ITALY}
    end

    def born_in_italy?
      Codes.italy? country_name
    end

    def female?
      gender == :female
    end

    def fiscal_code
      FiscalCode.new(self).calculate
    end

    # This method exists to support ActiveModel::Validations integration
    def read_attribute_for_validation key
      @attributes[key]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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