Class: Transformers::Formatters::Formatter
- Inherits:
-
Object
- Object
- Transformers::Formatters::Formatter
- 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
Instance Method Summary collapse
-
#field(row) ⇒ String
(also: #date, #payee, #memo, #amount, #outflow, #inflow)
Processes columns for each row.
-
#initialize(headers_indices = {}) ⇒ Formatter
constructor
A new instance of Formatter.
-
#run(row) ⇒ Array<String>
Turns CSV rows into YNAB4 rows (Date, Payee, Memo, Amount or Outflow and Inflow).
Constructor Details
#initialize(headers_indices = {}) ⇒ Formatter
Returns a new instance of Formatter.
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).
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)
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 |