Class: Rledger::Balance

Inherits:
Object
  • Object
show all
Defined in:
lib/rledger/report/balance.rb

Overview

Balance computes the balance of leaves (at the moment)

Instance Method Summary collapse

Constructor Details

#initialize(transactions) ⇒ Balance

Returns a new instance of Balance.



4
5
6
# File 'lib/rledger/report/balance.rb', line 4

def initialize(transactions)
  @transactions = transactions
end

Instance Method Details

#computeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rledger/report/balance.rb', line 8

def compute
  @balance = Hash.new

  @transactions.each do |t|
    t.posts.each do |p|
      elements = disaggregate p.voice

      elements.each do |element| 
        @balance[element] ? @balance[element].add!(p.amount) : @balance[element] = p.amount
      end
    end
  end
end

#to_sObject



22
23
24
25
# File 'lib/rledger/report/balance.rb', line 22

def to_s
  @balance.keys.sort.map { |k| puts "#{k} #{@balance[k]}" }
  ""
end