Class: Ledger::Transaction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dateDate

Returns the current value of date.

Returns:

  • (Date)

    the current value of date



11
12
13
# File 'lib/ledgerjournal/transaction.rb', line 11

def date
  @date
end

#metadataHash<String, String>

Returns the current value of metadata.

Returns:

  • (Hash<String, String>)

    the current value of metadata



11
12
13
# File 'lib/ledgerjournal/transaction.rb', line 11

def 
  @metadata
end

#payeeString

Returns the current value of payee.

Returns:

  • (String)

    the current value of payee



11
12
13
# File 'lib/ledgerjournal/transaction.rb', line 11

def payee
  @payee
end

#postingsArray<Ledger::Posting>

Returns the current value of postings.

Returns:



11
12
13
# File 'lib/ledgerjournal/transaction.rb', line 11

def postings
  @postings
end

#stateSymbol

state of transaction, can be :cleared or :pending

Returns:

  • (Symbol)

    the current value of state



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

Parameters:

  • accounts (String, Array<String>)

    name(s)

Returns:

  • (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 (accounts)
  accounts = [accounts] unless accounts.is_a?(Enumerable)
  accounts.each do ||
    postings.each do |posting|
      return posting if  == posting.
    end
  end
  return nil
end

#to_sObject



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