Class: Transformers::Formatters::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ynab_convert/transformers/formatters/formatter.rb

Overview

Formats Statements rows into YNAB4 rows (Date, Payee, Memo, Amount or Outflow and Inflow.)

Direct Known Subclasses

Example, N26, UBSChequing, UBSCredit, Wise

Instance Method Summary collapse

Constructor Details

#initialize(headers_indices = {}) ⇒ Formatter

Returns a new instance of Formatter.

Parameters:

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

    the indices at which to find each header’s name

Options Hash (headers_indices):

  • :date (Array<Numeric>)

    transaction date

  • :payee (Array<Numeric>)

    transaction payee/description

  • :memo (Array<Numeric>)

    transaction memo or currency if currency conversion will be performed

  • :amount (Array<Numeric>)

    transaction amount (if Statement is using the :amounts format)

  • :outflow (Array<Numeric>)

    transaction outflow (if using the :flows format)

  • :inflow (Array<Numeric>)

    transaction inflow (if using the :flows format)



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ynab_convert/transformers/formatters/formatter.rb', line 21

def initialize(headers_indices = {})
  default_values = {
    memo: [] # The Memo field tends to be empty for most institutions
  }

  @format = :flows
  unless headers_indices[:amount].nil? || headers_indices[:amount].empty?
    @format = :amounts
  end
  @headers_indices = default_values.merge(headers_indices)
end

Instance Method Details

#field(row) ⇒ String Also known as: date, payee, memo, amount, outflow, inflow

Note:

In more complex cases, some heuristics are required to format some of the columns. In that case, any of the aliased #field methods (#date, #payee, #memo, #amount, #outflow, #inflow) can be overridden in the child.

Processes columns for each row. Based on the method name that is called, it will extract the corresponding column (field).

Parameters:

  • row (CSV::Row)

    The row to process

Returns:

  • (String)

    The corresponding field(s)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ynab_convert/transformers/formatters/formatter.rb', line 58

def field(row)
  # Figure out the aliased name the method was called with, to derive
  # which field to return from the row.
  requested_field = __callee__.to_sym
  assembled_field = @headers_indices[requested_field].reduce([]) do
    |fields_data, i|
    fields_data << row[i]
  end

  # Avoid turning Dates and Numerics back to strings
  # If the assembled_field isn't a composite from several Statement
  # fields, there is no need to join(' ') and turn it into a String
  formatted_field = assembled_field[0]
  if assembled_field.length > 1
    formatted_field = assembled_field.join(' ')
  end

  # Avoid "nil" values in the output
  return '' if formatted_field.nil?

  formatted_field
end

#run(row) ⇒ Array<String>

Turns CSV rows into YNAB4 rows (Date, Payee, Memo, Amount or Outflow and Inflow)

Parameters:

  • row (CSV::Row)

    The CSV row to parse

Returns:

  • (Array<String>)

    The YNAB4 formatted row



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ynab_convert/transformers/formatters/formatter.rb', line 37

def run(row)
  ynab_row = [date(row), payee(row), memo(row)]

  if @format == :amounts
    ynab_row << amount(row)
  else
    ynab_row << outflow(row)
    ynab_row << inflow(row)
  end

  ynab_row
end