class Subledger::Domain::Balance

Attributes

credit_amount[R]
debit_amount[R]

Public Class Methods

new(args={ :debit_value => Zero.new, :credit_value => Zero.new }) click to toggle source
# File lib/subledger/domain/balance.rb, line 14
def initialize args={ :debit_value => Zero.new, :credit_value => Zero.new }
  if args[:debit_value].nil? or args[:credit_value].nil?
    raise BalanceError, ':debit_value and :credit_value required'
  end

  @debit_amount  = args[:debit_value].amount
  @credit_amount = args[:credit_value].amount
end

Public Instance Methods

+(other) click to toggle source
# File lib/subledger/domain/balance.rb, line 49
def + other
  klass = other.class

  debit_amount  = @debit_amount
  credit_amount = @credit_amount

  if klass == Balance
    debit_amount  += other.debit_value.amount
    credit_amount += other.credit_value.amount
  elsif klass == Debit
    debit_amount += other.amount
  elsif klass == Credit
    credit_amount += other.amount
  elsif klass == Zero
    return self.dup
  elsif other.kind_of? Line
    return self + other.value
  else
    raise BalanceError, "cannot add #{other.class.name} to a Balance"
  end

  self.class.new :debit_value  => debit_amount.zero?  ? Zero.new : Debit.new(debit_amount),
                 :credit_value => credit_amount.zero? ? Zero.new : Credit.new(credit_amount)
end
==(other) click to toggle source
# File lib/subledger/domain/balance.rb, line 74
def == other
  debit_value == other.debit_value and credit_value == other.credit_value
end
amount() click to toggle source
# File lib/subledger/domain/balance.rb, line 45
def amount
  value.amount
end
attributes() click to toggle source
# File lib/subledger/domain/balance.rb, line 78
def attributes
  { :debit_value  => debit_value,
    :credit_value => credit_value,
    :value        => value }
end
balanced?() click to toggle source
# File lib/subledger/domain/balance.rb, line 31
def balanced?
  value == Zero.new
end
credit_value() click to toggle source
# File lib/subledger/domain/balance.rb, line 27
def credit_value
  credit_amount.zero? ? Zero.new : Credit.new( credit_amount )
end
debit_value() click to toggle source
# File lib/subledger/domain/balance.rb, line 23
def debit_value
  debit_amount.zero? ? Zero.new : Debit.new( debit_amount )
end
value() click to toggle source
# File lib/subledger/domain/balance.rb, line 35
def value
  if debit_amount == credit_amount
    Zero.new
  elsif debit_amount > credit_amount
    Debit.new( debit_amount - credit_amount )
  else
    Credit.new( credit_amount - debit_amount )
  end
end