class AspireBudget::Worksheets::Transactions

Constants

WS_TITLE

Public Instance Methods

all() click to toggle source

@return [Array<AspireBudget::Transaction>] all transactions

# File lib/aspire_budget/worksheets/transactions.rb, line 12
def all
  rows.map do |row|
    klass.from_row(header, row)
  end
end
insert(record, sync: true) click to toggle source

Inserts a transaction to the spreadsheet. Accepts either a transaction record or a hash (that is passed to the transaction initializer) @see AspireBudget::Models::Transaction#initialize @param record [AspireBudget::Transaction, Hash] @return [AspireBudget::Transaction] a transaction

# File lib/aspire_budget/worksheets/transactions.rb, line 23
def insert(record, sync: true)
  record = klass.new(**record) if record.is_a?(Hash)
  row = record.to_row(header)
  ws.update_cells(*next_row_col, [row])
  ws.synchronize if sync
  record
end

Private Instance Methods

header() click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 57
def header
  @header ||=
    ws.rows(header_location - 1)
      .first
      .drop(margin_left)
      .map(&:downcase)
      .map(&:to_sym)
end
header_location() click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 66
def header_location
  @header_location ||=
    (1..ws.num_rows).find { |i| ws[i, margin_left + 1].casecmp?('date') }
end
klass() click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 33
def klass
  Models::Transaction
end
margin_left() click to toggle source

There is a 1 cell margin before the spreadsheet content

# File lib/aspire_budget/worksheets/transactions.rb, line 38
def margin_left
  1
end
next_row_col() click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 42
def next_row_col
  [ws.num_rows + 1, margin_left + 1]
end
rows() click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 52
def rows
  ws.rows_with_numerics(header_location)
    .map(&method(:sanitize)).compact
end
sanitize(row) click to toggle source
# File lib/aspire_budget/worksheets/transactions.rb, line 46
def sanitize(row)
  return if row.all? { |cell| cell == '' }

  row.drop(margin_left)
end