Class: LedgerRest::Ledger

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Class Attribute Details

.append_fileObject

Returns the value of attribute append_file.



7
8
9
# File 'lib/ledger-rest/ledger.rb', line 7

def append_file
  @append_file
end

.binObject

Returns the value of attribute bin.



7
8
9
# File 'lib/ledger-rest/ledger.rb', line 7

def bin
  @bin
end

.fileObject

Returns the value of attribute file.



7
8
9
# File 'lib/ledger-rest/ledger.rb', line 7

def file
  @file
end

.homeObject

Returns the value of attribute home.



7
8
9
# File 'lib/ledger-rest/ledger.rb', line 7

def home
  @home
end

.rcfileObject

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_usageObject

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(options)
  @bin = options[:ledger_bin] || '/usr/bin/ledger'
  @file = options[:ledger_file] || ENV['LEDGER_FILE'] || 'main.ledger'
  @append_file =
    options[:ledger_append_file] || ENV['LEDGER_FILE'] || 'append.ledger'
  @home = options[: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, options = {})
  Git.invoke :before_read

  options = {
    '-f' => @file
  }.merge(options)

  params = ''
  options.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_usageObject

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

.versionObject

Return the ledger version.



56
57
58
# File 'lib/ledger-rest/ledger.rb', line 56

def version
  exec('--version').match(/^Ledger (.*),/)[1]
end