Class: Bookkeeping::Account
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Bookkeeping::Account
show all
- Defined in:
- app/models/bookkeeping/account.rb
Defined Under Namespace
Classes: BadKind, NotFound
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.[](name, kind = nil, overdraft = true) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'app/models/bookkeeping/account.rb', line 35
def [](name, kind = nil, overdraft = true)
unless account = find_by(name: name)
raise NotFound, "account #{name} not found. Provide kind to create a new one" unless kind
raise BadKind if kind && !class_exists?("Bookkeeping::#{kind.to_s.classify}Account")
return by_kind(kind).create!(name: name.to_s, overdraft_enabled: overdraft)
end
raise BadKind if kind && by_kind(kind) != account.class
account.update_attributes! overdraft_enabled: overdraft if overdraft != account.overdraft_enabled?
return account
end
|
.balanced? ⇒ Boolean
58
59
60
|
# File 'app/models/bookkeeping/account.rb', line 58
def balanced?
total_balance == 0
end
|
.by_kind(kind) ⇒ Object
31
32
33
|
# File 'app/models/bookkeeping/account.rb', line 31
def by_kind(kind)
Bookkeeping.const_get "#{kind.to_s.capitalize}Account"
end
|
Instance Method Details
#credits_balance ⇒ Object
75
76
77
|
# File 'app/models/bookkeeping/account.rb', line 75
def credits_balance
credit_amounts.sum(:amount)
end
|
#debits_balance ⇒ Object
71
72
73
|
# File 'app/models/bookkeeping/account.rb', line 71
def debits_balance
debit_amounts.sum(:amount)
end
|
#overdraft? ⇒ Boolean
79
80
81
|
# File 'app/models/bookkeeping/account.rb', line 79
def overdraft?
balance < 0
end
|