Class: LedgerTillerExport::Exporter
- Inherits:
-
Object
- Object
- LedgerTillerExport::Exporter
- Extended by:
- T::Sig
- Defined in:
- lib/ledger_tiller_export.rb
Instance Attribute Summary collapse
-
#default_account ⇒ Object
readonly
Returns the value of attribute default_account.
-
#journal ⇒ Object
readonly
Returns the value of attribute journal.
-
#ledger_pretty_print_options ⇒ Object
readonly
Returns the value of attribute ledger_pretty_print_options.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#spreadsheet ⇒ Object
readonly
Returns the value of attribute spreadsheet.
-
#worksheet ⇒ Object
readonly
Returns the value of attribute worksheet.
Instance Method Summary collapse
- #account_for_row(row) ⇒ Object
- #fetch_known_transactions ⇒ Object
-
#initialize(rules:, spreadsheet:, worksheet: 'Transactions', default_account: 'Expenses:Misc', ledger_pretty_print_options: '--sort=date', ledger_date_format: '%m/%d') ⇒ Exporter
constructor
A new instance of Exporter.
- #journal_transaction(row) ⇒ Object
- #run ⇒ Object
- #skip_row?(row) ⇒ Boolean
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(default_account, String) @ledger_pretty_print_options = T.let(, 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_account ⇒ Object (readonly)
Returns the value of attribute default_account.
74 75 76 |
# File 'lib/ledger_tiller_export.rb', line 74 def default_account @default_account end |
#journal ⇒ Object (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_options ⇒ Object (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 end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
65 66 67 |
# File 'lib/ledger_tiller_export.rb', line 65 def rules @rules end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
80 81 82 |
# File 'lib/ledger_tiller_export.rb', line 80 def session @session end |
#spreadsheet ⇒ Object (readonly)
Returns the value of attribute spreadsheet.
68 69 70 |
# File 'lib/ledger_tiller_export.rb', line 68 def spreadsheet @spreadsheet end |
#worksheet ⇒ Object (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 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 ⇒ Object
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/)', ] , err, status = Open3.capture3(*cmd) if status.exitstatus && status.exitstatus != 0 STDERR.puts(err) exit T.must(status.exitstatus) end Set.new(.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 account_for_row(row), row.amount * -1 txn.posting row.account end end |
#run ⇒ Object
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
132 133 134 |
# File 'lib/ledger_tiller_export.rb', line 132 def skip_row?(row) false end |