Class: Ledger::Posting

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account:, currency:, amount:, balance_assignment: nil, metadata: {}) ⇒ Posting

Returns a new instance of Posting.



15
16
17
18
19
20
21
# File 'lib/ledgerjournal/posting.rb', line 15

def initialize(account:, currency:, amount:, balance_assignment: nil, metadata: {})
  @account = 
  @currency = currency
  @amount = amount
  @balance_assignment = balance_assignment
  @metadata = 
end

Instance Attribute Details

#accountString

Returns the current value of account.

Returns:

  • (String)

    the current value of account



12
13
14
# File 'lib/ledgerjournal/posting.rb', line 12

def 
  @account
end

#amountBigDecimal

Returns the current value of amount.

Returns:

  • (BigDecimal)

    the current value of amount



12
13
14
# File 'lib/ledgerjournal/posting.rb', line 12

def amount
  @amount
end

#balance_assignmentBigDecimal

if a balance_assignment is set, ledger-cli checks the balance of the account after this posting

Returns:

  • (BigDecimal)

    the current value of balance_assignment



12
13
14
# File 'lib/ledgerjournal/posting.rb', line 12

def balance_assignment
  @balance_assignment
end

#currencyString

Returns the current value of currency.

Returns:

  • (String)

    the current value of currency



12
13
14
# File 'lib/ledgerjournal/posting.rb', line 12

def currency
  @currency
end

#metadataHash<String, String>

Returns the current value of metadata.

Returns:

  • (Hash<String, String>)

    the current value of metadata



12
13
14
# File 'lib/ledgerjournal/posting.rb', line 12

def 
  @metadata
end

Class Method Details

.parse_xml(xml) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ledgerjournal/posting.rb', line 23

def self.parse_xml(xml)
  balance_assignment = nil
  currency = xml.xpath('post-amount/amount/commodity/symbol').text
  if (xml_balance = xml.xpath('balance-assignment').first)
    balance_currency = xml_balance.xpath('commodity/symbol').text
    raise Error, "Posting currency #{currency} doesn't match assignment currency #{balance_currency}" if currency != balance_currency

    balance_assignment = Ledger.defaults.parse_amount(xml_balance.xpath('quantity').text)
  end
  Posting.new(
    account: xml.xpath('account/name').text,
    currency: currency,
    amount: Ledger.defaults.parse_amount(xml.xpath('post-amount/amount/quantity').text),
    balance_assignment: balance_assignment,
    metadata: Hash[xml.xpath('metadata/value').collect { |m| [m['key'], m.xpath('string').text] }]
  )
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/ledgerjournal/posting.rb', line 41

def ==(other)
  self.class == other.class && all_fields == other.all_fields
end

#to_sObject



45
46
47
48
49
50
51
52
# File 'lib/ledgerjournal/posting.rb', line 45

def to_s
  posting_line = "#{}   "
  posting_line += "#{currency} #{Ledger.defaults.format(amount)}".rjust(48 - posting_line.length, ' ')
  posting_line += " = #{currency} #{Ledger.defaults.format(balance_assignment)}" if balance_assignment
  lines = [posting_line]
  lines += .to_a.collect { |m| "; #{m[0]}: #{m[1]}" } unless .empty?
  return lines.join("\n")
end