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
-
.amount_valid?(row) ⇒ Boolean
Indicates whether the amount on the row is valid.
-
.payee_valid?(row) ⇒ Boolean
Validates the Payee value.
-
.row_format(row) ⇒ :flows, :amounts
Indicates which format the row is in (:flows or :amounts).
-
.transaction_date_valid?(row) ⇒ Boolean
Validates the Date value.
-
.valid?(row) ⇒ Boolean
Validates a row.
-
.value_valid?(value) ⇒ Boolean
Indicates whether a value is valid.
Class Method Details
.amount_valid?(row) ⇒ Boolean
Indicates whether the amount on the row is valid
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
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)
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
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
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
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 |