Class: Ledger::Journal
- Inherits:
-
Object
- Object
- Ledger::Journal
- Defined in:
- lib/ledgerjournal/journal.rb
Overview
Represents a ledger journal
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Path path to ledger file.
-
#transactions ⇒ Array<Ledger::Transaction>
List of transactions in journal.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the other journal contains equal transactions.
-
#initialize(path: nil, ledger_args: nil) ⇒ Journal
constructor
Creates a new ledger journal or loads transactions from a ledger journal file.
-
#save! ⇒ Object
If the journal was opened from a file, save/overwrite the file with the transactions from this journal.
-
#to_s(pretty_print: true) ⇒ String
Returns the transactions in the journal formatted as string.
-
#transactions_with_account(account) ⇒ Object
returns all transactions that have a posting for one of the given accounts.
Constructor Details
#initialize(path: nil, ledger_args: nil) ⇒ Journal
Creates a new ledger journal or loads transactions from a ledger journal file.
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
#path ⇒ String (readonly)
Returns path path to ledger file.
15 16 17 |
# File 'lib/ledgerjournal/journal.rb', line 15 def path @path end |
#transactions ⇒ Array<Ledger::Transaction>
Returns list of transactions in journal.
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.
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.
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
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
63 64 65 66 |
# File 'lib/ledgerjournal/journal.rb', line 63 def transactions_with_account(account) accounts = account.is_a?(Enumerable) ? Set.new(account) : Set[account] @transactions.select { |tx| accounts.intersect?(Set.new(tx.postings.map(&:account))) } end |