class Rledger::Balance

Balance computes the balance of leaves (at the moment)

Public Class Methods

new(transactions) click to toggle source
# File lib/rledger/report/balance.rb, line 4
def initialize(transactions)
  @transactions = transactions
end

Public Instance Methods

compute() click to toggle source
# 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_s() click to toggle source
# File lib/rledger/report/balance.rb, line 22
def to_s
  @balance.keys.sort.map { |k| puts "#{k} #{@balance[k]}" }
  ""
end

Private Instance Methods

disaggregate(voice) click to toggle source

return an array of all the components of a voice Expenses:Dining:Fish -> [Expenses, Expenses:Dining, Expenses:Dining:Fish ]

# File lib/rledger/report/balance.rb, line 31
def disaggregate voice
  output = []
  aggregator = ""
  voice.split(":").each do |element|
    aggregator = aggregator == "" ? element : aggregator + ":" + element
    output << aggregator
  end
  output
end