Class: Ledger::Posting
- Inherits:
-
Object
- Object
- Ledger::Posting
- Defined in:
- lib/ledgerjournal/posting.rb
Instance Attribute Summary collapse
-
#account ⇒ String
The current value of account.
-
#amount ⇒ BigDecimal
The current value of amount.
-
#balance_assignment ⇒ BigDecimal
if a balance_assignment is set, ledger-cli checks the balance of the account after this posting.
-
#currency ⇒ String
The current value of currency.
-
#metadata ⇒ Hash<String, String>
The current value of metadata.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(account:, currency:, amount:, balance_assignment: nil, metadata: {}) ⇒ Posting
constructor
A new instance of Posting.
- #to_s ⇒ Object
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 = account @currency = currency @amount = amount @balance_assignment = balance_assignment @metadata = end |
Instance Attribute Details
#account ⇒ String
Returns the current value of account.
12 13 14 |
# File 'lib/ledgerjournal/posting.rb', line 12 def account @account end |
#amount ⇒ BigDecimal
Returns the current value of amount.
12 13 14 |
# File 'lib/ledgerjournal/posting.rb', line 12 def amount @amount end |
#balance_assignment ⇒ BigDecimal
if a balance_assignment is set, ledger-cli checks the balance of the account after this posting
12 13 14 |
# File 'lib/ledgerjournal/posting.rb', line 12 def balance_assignment @balance_assignment end |
#currency ⇒ String
Returns the current value of currency.
12 13 14 |
# File 'lib/ledgerjournal/posting.rb', line 12 def currency @currency end |
#metadata ⇒ Hash<String, String>
Returns 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_s ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/ledgerjournal/posting.rb', line 45 def to_s posting_line = "#{account} " 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 |