Class: Bookkeeping::Account

Inherits:
ActiveRecord::Base
  • Object
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

Raises:



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  = 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) != .class

  .update_attributes! overdraft_enabled: overdraft if overdraft != .overdraft_enabled?

  return 
end

.balanced?Boolean

Returns:

  • (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

.total_balanceObject

Raises:

  • (NoMethodError)


52
53
54
55
56
# File 'app/models/bookkeeping/account.rb', line 52

def total_balance
  raise(NoMethodError, "undefined method 'total_balance'") unless self == Bookkeeping::Account

  Bookkeeping::AssetAccount.balance + Bookkeeping::ExpenseAccount.balance - Bookkeeping::LiabilityAccount.balance - Bookkeeping::EquityAccount.balance - Bookkeeping::IncomeAccount.balance
end

Instance Method Details

#credits_balanceObject



75
76
77
# File 'app/models/bookkeeping/account.rb', line 75

def credits_balance
  credit_amounts.sum(:amount)
end

#debits_balanceObject



71
72
73
# File 'app/models/bookkeeping/account.rb', line 71

def debits_balance
  debit_amounts.sum(:amount)
end

#overdraft?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/bookkeeping/account.rb', line 79

def overdraft?
  balance < 0
end