Module: Validators::YNAB4Row

Defined in:
lib/ynab_convert/validators/ynab4_row_validator.rb

Overview

Checks YNAB4 row for validity. A row is valid if it has a Date, Payee, and one of Amount, Outflow, Inflow.

Class Method Summary collapse

Class Method Details

.amount_valid?(row) ⇒ Boolean

Indicates whether the amount on the row is valid

Parameters:

  • row (CSV::Row)

    the row to check

Returns:

  • (Boolean)

    whether the amount is invalid



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 33

def self.amount_valid?(row)
  format = row_format(row)
  indices = [3]
  indices << 4 if format == :flows

  if format == :amounts
    return indices.reduce(true) do |valid, i|
      valid && value_valid?(row[i])
    end
  end

  indices.reduce(false) do |valid, i|
    valid || value_valid?(row[i])
  end
end

.payee_valid?(row) ⇒ Boolean

Note:

Prefer using the #valid? method

Validates the Payee value

Parameters:

  • row (Array<String, Numeric, Date] The row to validate)

    ow [Array<String, Numeric, Date] The row to validate

Returns:

  • (Boolean)

    Whether the row’s Payee is valid



76
77
78
79
80
81
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 76

def self.payee_valid?(row)
  payee_index = 1
  payee = row[payee_index]

  value_valid?(payee)
end

.row_format(row) ⇒ :flows, :amounts

Indicates which format the row is in (:flows or :amounts)

Parameters:

  • row (CSV::Row)

    the row to check

Returns:

  • (:flows, :amounts)

    the row’s format



21
22
23
24
25
26
27
28
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 21

def self.row_format(row)
  format = :flows
  # :flows has 5 columns: Date, Payee, Memo, Outflow, Inflow
  # :amounts has 4 columns: Date, Payee, Memo, Amount
  format = :amounts if row.length == 4

  format
end

.transaction_date_valid?(row) ⇒ Boolean

Note:

Prefer using the #valid? method

Validates the Date value

Parameters:

  • row (Array<String, Numeric, Date] The row to validate)

    ow [Array<String, Numeric, Date] The row to validate

Returns:

  • (Boolean)

    Whether the row’s Date is invalid



65
66
67
68
69
70
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 65

def self.transaction_date_valid?(row)
  date_index = 0
  date = row[date_index]

  value_valid?(date)
end

.valid?(row) ⇒ Boolean

Validates a row

Parameters:

  • row (Array<String, Numeric, Date>)

    The row to validate

Returns:

  • (Boolean)

    Whether the row is valid



10
11
12
13
14
15
16
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 10

def self.valid?(row)
  # we are dealing with a YNAB4 row:
  # %w[Date Payee Memo Amount|Outflow Inflow]
  amount_valid?(row) &&
    transaction_date_valid?(row) &&
    payee_valid?(row)
end

.value_valid?(value) ⇒ Boolean

Note:

Prefer using the #valid? method

Indicates whether a value is valid

Parameters:

  • value (#zero?, #nil?, #to_s)

    the value to check

Returns:

  • (Boolean)

    whether the value is valid



53
54
55
56
57
58
59
# File 'lib/ynab_convert/validators/ynab4_row_validator.rb', line 53

def self.value_valid?(value)
  if value.respond_to? :zero?
    !value.zero?
  else
    !value.nil? && !value.to_s.empty?
  end
end