Class: ItaxCode::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/itax_code/encoder.rb,
lib/itax_code/error.rb

Overview

Handles the tax code generation logic.

Examples:

ItaxCode::Encoder.new(
  surname: "Rossi",
  name: "Mario",
  gender: "M",
  birthdate: "1980-01-01",
  birthplace: "Milano"
).encode

Returns:

  • (String)

    The encoded tax code

Constant Summary collapse

Error =
Class.new(Error)
InvalidBirthdateError =
Class.new(Error)
MissingDataError =
Class.new(Error)

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, utils = Utils) ⇒ Encoder

Returns a new instance of Encoder.

Parameters:

  • data (Hash) (defaults to: {})

    The user attributes

  • utils (Utils) (defaults to: Utils)

Options Hash (data):

  • :surname (String)

    The user first name

  • :name (String)

    The user last name

  • :gender (String)

    The user gender

  • :birthdate (String, Date)

    The user birthdate

  • :birthplace (String)

    The user birthplace



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/itax_code/encoder.rb', line 25

def initialize(data = {}, utils = Utils)
  @utils = utils

  @surname    = data[:surname]
  @name       = data[:name]
  @gender     = data[:gender]&.upcase
  @birthdate  = data[:birthdate].to_s
  @birthplace = data[:birthplace]
  validate_data_presence!

  @birthdate = parse_birthdate!
end

Instance Method Details

#encodeString

Computes the tax code from its components.

Returns:

  • (String)

    The calculated tax code



41
42
43
44
45
46
47
48
# File 'lib/itax_code/encoder.rb', line 41

def encode
  code  = encode_surname
  code += encode_name
  code += encode_birthdate
  code += encode_birthplace
  code += utils.encode_cin code
  code
end