Class: Ledger::Transaction
- Inherits:
-
Object
- Object
- Ledger::Transaction
- Defined in:
- lib/ledgerjournal/transaction.rb
Instance Attribute Summary collapse
-
#date ⇒ Date
The current value of date.
-
#metadata ⇒ Hash<String, String>
The current value of metadata.
-
#payee ⇒ String
The current value of payee.
-
#postings ⇒ Array<Ledger::Posting>
The current value of postings.
-
#state ⇒ Symbol
state of transaction, can be :cleared or :pending.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(date:, state: :cleared, payee:, metadata: {}, postings:) ⇒ Transaction
constructor
A new instance of Transaction.
-
#posting_for_account(accounts) ⇒ Ledger::Posting
Returns the first posting that matches one of the given account names.
- #to_s ⇒ Object
Constructor Details
#initialize(date:, state: :cleared, payee:, metadata: {}, postings:) ⇒ Transaction
Returns a new instance of Transaction.
14 15 16 17 18 19 20 |
# File 'lib/ledgerjournal/transaction.rb', line 14 def initialize(date:, state: :cleared, payee:, metadata: {}, postings:) @date = date @state = state @payee = payee @metadata = @postings = postings end |
Instance Attribute Details
#date ⇒ Date
Returns the current value of date.
11 12 13 |
# File 'lib/ledgerjournal/transaction.rb', line 11 def date @date end |
#metadata ⇒ Hash<String, String>
Returns the current value of metadata.
11 12 13 |
# File 'lib/ledgerjournal/transaction.rb', line 11 def @metadata end |
#payee ⇒ String
Returns the current value of payee.
11 12 13 |
# File 'lib/ledgerjournal/transaction.rb', line 11 def payee @payee end |
#postings ⇒ Array<Ledger::Posting>
Returns the current value of postings.
11 12 13 |
# File 'lib/ledgerjournal/transaction.rb', line 11 def postings @postings end |
#state ⇒ Symbol
state of transaction, can be :cleared or :pending
11 12 13 |
# File 'lib/ledgerjournal/transaction.rb', line 11 def state @state end |
Class Method Details
.parse_xml(xml) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/ledgerjournal/transaction.rb', line 22 def self.parse_xml(xml) Transaction.new( date: Date.strptime(xml.xpath('date').text, Ledger.defaults.date_format), payee: xml.xpath('payee').text, state: xml['state'].to_sym, metadata: Hash[xml.xpath('metadata/value').collect { |m| [m['key'], m.xpath('string').text] }], postings: xml.xpath('postings/posting').map { |posting_xml| Posting.parse_xml(posting_xml) } ) end |
Instance Method Details
#==(other) ⇒ Object
32 33 34 |
# File 'lib/ledgerjournal/transaction.rb', line 32 def ==(other) self.class == other.class && all_fields == other.all_fields end |
#posting_for_account(accounts) ⇒ Ledger::Posting
Returns the first posting that matches one of the given account names
50 51 52 53 54 55 56 57 58 |
# File 'lib/ledgerjournal/transaction.rb', line 50 def posting_for_account(accounts) accounts = [accounts] unless accounts.is_a?(Enumerable) accounts.each do |account| postings.each do |posting| return posting if account == posting.account end end return nil end |
#to_s ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ledgerjournal/transaction.rb', line 36 def to_s date_str = Ledger.defaults.format(date) states = { pending: '!', cleared: '*' } lines = ["#{date_str} #{states[state]} #{payee}"] lines += .to_a.collect { |m| " ; #{m[0]}: #{m[1]}" } unless .empty? lines += postings.map { |posting| posting.to_s.lines.map { |line| ' ' + line }.join } return lines.join("\n") end |