class LedgerTillerExport::Exporter

Public Class Methods

new(rules:, spreadsheet:, worksheet: 'Transactions', default_account: 'Expenses:Misc', ledger_pretty_print_options: '--sort=date', ledger_date_format: '%m/%d') click to toggle source
Calls superclass method
# File lib/ledger_tiller_export.rb, line 90
def initialize(rules:, spreadsheet:, worksheet: 'Transactions', default_account: 'Expenses:Misc', ledger_pretty_print_options: '--sort=date', ledger_date_format: '%m/%d')
  super(
    rules: rules,
    spreadsheet: spreadsheet,
    worksheet: worksheet,
    default_account: default_account,
    ledger_pretty_print_options: ledger_pretty_print_options,
  )

  self.journal.date_format = ledger_date_format
end

Public Instance Methods

account_for_row(row) click to toggle source
# File lib/ledger_tiller_export.rb, line 151
def account_for_row(row)
  rules.each do |rule|
    account = rule.account_for_row(row)
    return account if account
  end

  default_account
end
fetch_known_transactions() click to toggle source
# File lib/ledger_tiller_export.rb, line 131
def fetch_known_transactions
  cmd = [
    'ledger',
    %q{--register-format=%(tag("tiller_id"))\n},
    'reg',
    'expr',
    'has_tag(/tiller_id/)',
  ]

  raw_tags, err, status = Open3.capture3(*cmd)

  if status.exitstatus && status.exitstatus != 0
    STDERR.puts(err)
    exit T.must(status.exitstatus)
  end

  Set.new(raw_tags.strip.split(/(\n|,)/).map(&:strip))
end
journal_transaction(row) click to toggle source
# File lib/ledger_tiller_export.rb, line 161
def journal_transaction(row)
  journal.transaction do |txn|
    txn.cleared!
    txn.date row.txn_date
    txn.payee row.description
    txn.comment "tiller_id: #{row.txn_id}"

    txn.posting account_for_row(row), row.amount * -1
    txn.posting row.account
  end
end
run() click to toggle source
# File lib/ledger_tiller_export.rb, line 103
def run
  @known_transactions = T.let(fetch_known_transactions, T.nilable(T::Set[String]))
  @known_transactions = T.must(@known_transactions)

  worksheets = session.spreadsheet_by_key(spreadsheet).worksheets

  ws = worksheets.detect { |w| w.title == worksheet }
  raw_csv = ws.export_as_string.force_encoding('utf-8')
  csv = CSV.parse(raw_csv, headers: true)

  T.must(csv).each do |raw_row|
    row = Row.from_csv_row(raw_row.to_h)

    next if @known_transactions.include?(row.txn_id)
    next if skip_row?(row)

    journal_transaction(row)
  end

  puts journal.pretty_print(ledger_pretty_print_options)
end
skip_row?(row) click to toggle source
# File lib/ledger_tiller_export.rb, line 126
def skip_row?(row)
  false
end