Class: LedgerRest::Ledger
- Inherits:
-
Object
- Object
- LedgerRest::Ledger
- Defined in:
- lib/ledger-rest/ledger.rb,
lib/ledger-rest/ledger/entry.rb,
lib/ledger-rest/ledger/budget.rb,
lib/ledger-rest/ledger/parser.rb,
lib/ledger-rest/ledger/balance.rb,
lib/ledger-rest/ledger/register.rb,
lib/ledger-rest/ledger/transaction.rb
Defined Under Namespace
Classes: Balance, Budget, Entry, Parser, Register, Transaction
Class Attribute Summary collapse
-
.append_file ⇒ Object
Returns the value of attribute append_file.
-
.bin ⇒ Object
Returns the value of attribute bin.
-
.file ⇒ Object
Returns the value of attribute file.
-
.home ⇒ Object
Returns the value of attribute home.
-
.rcfile ⇒ Object
Returns the value of attribute rcfile.
Class Method Summary collapse
-
.accounts(query = nil) ⇒ Object
Return an array of accounts.
-
.accounts_with_usage ⇒ Object
Return an Array of accounts with their respective usage count.
-
.append(transaction) ⇒ Object
Append a new transaction to the append_file.
-
.balance(query = nil) ⇒ Object
Returns a hash with the balance for given query.
- .configure(options) ⇒ Object
-
.entry(description) ⇒ Object
Get a new transaction entry based on previous entries found in the append file.
-
.exec(cmd, options = {}) ⇒ Object
Execute ledger command with given parameters.
-
.payees(query = nil) ⇒ Object
Return a list of payees.
-
.payees_with_usage ⇒ Object
Returns an Array of payees with their respective usage count.
-
.transactions(query) ⇒ Object
Returns a list of transactions as JSON.
-
.version ⇒ Object
Return the ledger version.
Class Attribute Details
.append_file ⇒ Object
Returns the value of attribute append_file.
7 8 9 |
# File 'lib/ledger-rest/ledger.rb', line 7 def append_file @append_file end |
.bin ⇒ Object
Returns the value of attribute bin.
7 8 9 |
# File 'lib/ledger-rest/ledger.rb', line 7 def bin @bin end |
.file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/ledger-rest/ledger.rb', line 7 def file @file end |
.home ⇒ Object
Returns the value of attribute home.
7 8 9 |
# File 'lib/ledger-rest/ledger.rb', line 7 def home @home end |
.rcfile ⇒ Object
Returns the value of attribute rcfile.
7 8 9 |
# File 'lib/ledger-rest/ledger.rb', line 7 def rcfile @rcfile end |
Class Method Details
.accounts(query = nil) ⇒ Object
Return an array of accounts.
75 76 77 |
# File 'lib/ledger-rest/ledger.rb', line 75 def accounts(query = nil) exec("accounts #{query if query}").split("\n") end |
.accounts_with_usage ⇒ Object
Return an Array of accounts with their respective usage count.
90 |
# File 'lib/ledger-rest/ledger.rb', line 90 def accounts_with_usage; end |
.append(transaction) ⇒ Object
Append a new transaction to the append_file.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ledger-rest/ledger.rb', line 36 def append(transaction) File.open(append_file, 'a+') do |f| if f.pos == 0 last_char = "\n" else f.pos = f.pos - 1 last_char = f.getc end f.write "\n" f.write(transaction.to_ledger) end end |
.balance(query = nil) ⇒ Object
Returns a hash with the balance for given query
51 52 53 |
# File 'lib/ledger-rest/ledger.rb', line 51 def balance(query = nil) Ledger::Balance.get(query) end |
.configure(options) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/ledger-rest/ledger.rb', line 9 def configure() @bin = [:ledger_bin] || '/usr/bin/ledger' @file = [:ledger_file] || ENV['LEDGER_FILE'] || 'main.ledger' @append_file = [:ledger_append_file] || ENV['LEDGER_FILE'] || 'append.ledger' @home = [:ledger_home] || '' end |
.entry(description) ⇒ Object
Get a new transaction entry based on previous entries found in the append file.
62 63 64 |
# File 'lib/ledger-rest/ledger.rb', line 62 def entry(description) result = exec "entry #{description}", '-f' => append_file end |
.exec(cmd, options = {}) ⇒ Object
Execute ledger command with given parameters
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ledger-rest/ledger.rb', line 18 def exec(cmd, = {}) Git.invoke :before_read = { '-f' => @file }.merge() params = '' .each do |key, val| params << " #{key} #{Escape.shell_single_word(val)}" end command = "#{bin} #{params} #{cmd}" `#{command}`.rstrip end |
.payees(query = nil) ⇒ Object
Return a list of payees.
67 68 69 |
# File 'lib/ledger-rest/ledger.rb', line 67 def payees(query = nil) exec("payees #{query if query}").split("\n") end |
.payees_with_usage ⇒ Object
Returns an Array of payees with their respective usage count.
72 |
# File 'lib/ledger-rest/ledger.rb', line 72 def payees_with_usage; end |
.transactions(query) ⇒ Object
Returns a list of transactions as JSON
80 81 82 83 84 85 86 87 |
# File 'lib/ledger-rest/ledger.rb', line 80 def transactions(query) # This is a most ugly hack, which gathers the ledger # information via python. # TODO: write ruby-ledger bindings to retrieve reports with # ruby directly! query.gsub!('-', '\-') if query `#{bin} python #{File.join(File.dirname(__FILE__), 'transactions.py')} "#{@file}" "#{query}"` end |
.version ⇒ Object
Return the ledger version.
56 57 58 |
# File 'lib/ledger-rest/ledger.rb', line 56 def version exec('--version').match(/^Ledger (.*),/)[1] end |