Class: Subledger::Domain::Balance
Defined Under Namespace
Classes: Entity
Instance Method Summary
collapse
#patch_hash, #post_hash, #serializable_hash, #to_json
#collection_name, #entity_name, included, #to_s
Constructor Details
#initialize(args = { :debit_value => Zero.new, :credit_value => Zero.new }) ⇒ Balance
Returns a new instance of Balance.
14
15
16
17
18
19
20
21
|
# 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
|
Instance Method Details
#+(other) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# 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) ⇒ Object
74
75
76
|
# File 'lib/subledger/domain/balance.rb', line 74
def == other
debit_value == other.debit_value and credit_value == other.credit_value
end
|
#amount ⇒ Object
45
46
47
|
# File 'lib/subledger/domain/balance.rb', line 45
def amount
value.amount
end
|
#attributes ⇒ Object
78
79
80
81
82
|
# File 'lib/subledger/domain/balance.rb', line 78
def attributes
{ :debit_value => debit_value,
:credit_value => credit_value,
:value => value }
end
|
#balanced? ⇒ Boolean
31
32
33
|
# File 'lib/subledger/domain/balance.rb', line 31
def balanced?
value == Zero.new
end
|
#credit_value ⇒ Object
27
28
29
|
# File 'lib/subledger/domain/balance.rb', line 27
def credit_value
credit_amount.zero? ? Zero.new : Credit.new( credit_amount )
end
|
#debit_value ⇒ Object
23
24
25
|
# File 'lib/subledger/domain/balance.rb', line 23
def debit_value
debit_amount.zero? ? Zero.new : Debit.new( debit_amount )
end
|
#value ⇒ Object
35
36
37
38
39
40
41
42
43
|
# 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
|