Class: Ledger::Journal

Inherits:
Object
  • Object
show all
Defined in:
lib/ledgerjournal/journal.rb

Overview

Represents a ledger journal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, ledger_args: nil) ⇒ Journal

Creates a new ledger journal or loads transactions from a ledger journal file.

Parameters:

  • path (String) (defaults to: nil)

    path to a ledger journal to load

  • ledger_args (String) (defaults to: nil)

    when loading from a path, you can pass in arguments to the ledger command (for example to filter transactions)



20
21
22
23
24
25
26
27
28
# File 'lib/ledgerjournal/journal.rb', line 20

def initialize(path: nil, ledger_args: nil)
  @transactions = []
  if path
    @path = path
    raise Error, "#{@path} not found" unless File.exist?(@path)

    read_ledger(ledger_args: ["-f #{@path.shellescape}", ledger_args].compact.join(' '))
  end
end

Instance Attribute Details

#pathString (readonly)

Returns path path to ledger file.

Returns:

  • (String)

    path path to ledger file



15
16
17
# File 'lib/ledgerjournal/journal.rb', line 15

def path
  @path
end

#transactionsArray<Ledger::Transaction>

Returns list of transactions in journal.

Returns:



13
14
15
# File 'lib/ledgerjournal/journal.rb', line 13

def transactions
  @transactions
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the other journal contains equal transactions.

Parameters:

Returns:

  • (Boolean)

    true if the other journal contains equal transactions



32
33
34
# File 'lib/ledgerjournal/journal.rb', line 32

def ==(other)
  transactions == other.transactions
end

#save!Object

If the journal was opened from a file, save/overwrite the file with the transactions from this journal.

Raises:



55
56
57
58
59
# File 'lib/ledgerjournal/journal.rb', line 55

def save!
  raise Error, 'Journal was not read from path, cannot save' unless @path

  File.write(@path, to_s)
end

#to_s(pretty_print: true) ⇒ String

Returns the transactions in the journal formatted as string

Parameters:

  • pretty_print (Boolean) (defaults to: true)

    calls ledger to format the journal if true

Returns:

  • (String)

    returns the transactions in the journal formatted as string



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ledgerjournal/journal.rb', line 38

def to_s(pretty_print: true)
  str = transactions.map(&:to_s).join("\n\n")
  # add a newline at the end of the file - see https://github.com/ledger/ledger/issues/516
  str += "\n"
  if pretty_print
    begin
      str = Ledger.defaults.run('-f - print', stdin: str)
      str = str.lines.map(&:rstrip).join("\n") + "\n"
    rescue StandardError => e
      # return an unformatted string if an error occurs
      puts "Couldn't format transaction log: #{e}"
    end
  end
  return str
end

#transactions_with_account(account) ⇒ Object

returns all transactions that have a posting for one of the given accounts

Parameters:

  • account (String, Array<String>, Set<String>)

    account name(s)



63
64
65
66
# File 'lib/ledgerjournal/journal.rb', line 63

def ()
  accounts = .is_a?(Enumerable) ? Set.new() : Set[]
  @transactions.select { |tx| accounts.intersect?(Set.new(tx.postings.map(&:account))) }
end