Class: LedgerTillerExport::Exporter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ledger_tiller_export.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:, spreadsheet:, worksheet: 'Transactions', default_account: 'Expenses:Misc', ledger_pretty_print_options: '--sort=date', ledger_date_format: '%m/%d') ⇒ Exporter

Returns a new instance of Exporter.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ledger_tiller_export.rb', line 95

def initialize(rules:, spreadsheet:, worksheet: 'Transactions', default_account: 'Expenses:Misc', ledger_pretty_print_options: '--sort=date', ledger_date_format: '%m/%d')
  @rules = T.let(rules, T::Array[RuleInterface])
  @spreadsheet = T.let(spreadsheet, String)
  @worksheet = T.let(worksheet, String)
  @default_account = T.let(, String)
  @ledger_pretty_print_options = T.let(ledger_pretty_print_options, String)

  @session = T.let(GoogleDrive::Session.from_config('.config.json'), GoogleDrive::Session)
  @journal = T.let(LedgerGen::Journal.new, LedgerGen::Journal)

  @journal.date_format = ledger_date_format
end

Instance Attribute Details

#default_accountObject (readonly)

Returns the value of attribute default_account.



74
75
76
# File 'lib/ledger_tiller_export.rb', line 74

def 
  @default_account
end

#journalObject (readonly)

Returns the value of attribute journal.



83
84
85
# File 'lib/ledger_tiller_export.rb', line 83

def journal
  @journal
end

#ledger_pretty_print_optionsObject (readonly)

Returns the value of attribute ledger_pretty_print_options.



77
78
79
# File 'lib/ledger_tiller_export.rb', line 77

def ledger_pretty_print_options
  @ledger_pretty_print_options
end

#rulesObject (readonly)

Returns the value of attribute rules.



65
66
67
# File 'lib/ledger_tiller_export.rb', line 65

def rules
  @rules
end

#sessionObject (readonly)

Returns the value of attribute session.



80
81
82
# File 'lib/ledger_tiller_export.rb', line 80

def session
  @session
end

#spreadsheetObject (readonly)

Returns the value of attribute spreadsheet.



68
69
70
# File 'lib/ledger_tiller_export.rb', line 68

def spreadsheet
  @spreadsheet
end

#worksheetObject (readonly)

Returns the value of attribute worksheet.



71
72
73
# File 'lib/ledger_tiller_export.rb', line 71

def worksheet
  @worksheet
end

Instance Method Details

#account_for_row(row) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/ledger_tiller_export.rb', line 157

def (row)
  rules.each do |rule|
     = rule.(row)
    return  if 
  end

  
end

#fetch_known_transactionsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ledger_tiller_export.rb', line 137

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) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ledger_tiller_export.rb', line 167

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 (row), row.amount * -1
    txn.posting row.
  end
end

#runObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ledger_tiller_export.rb', line 109

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) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/ledger_tiller_export.rb', line 132

def skip_row?(row)
  false
end