Class: DoubleEntry::AccountBalance

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/double_entry/account_balance.rb

Overview

Account balance records cache the current balance for each account. They also provide a database representation of an account that we can use to do DB level locking.

See DoubleEntry::Locking for more info on locking.

Account balances are created on demand when transfers occur.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_account(account, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/double_entry/account_balance.rb', line 33

def self.(, options = {})
  scope = where(:scope => .scope_identity, :account => .identifier.to_s)
  scope = scope.lock(true) if options[:lock]
  scope.first
end

Instance Method Details

#accountObject



29
30
31
# File 'lib/double_entry/account_balance.rb', line 29

def 
  DoubleEntry.(self[:account].to_sym, :scope => self[:scope])
end

#account=(account) ⇒ Object



23
24
25
26
27
# File 'lib/double_entry/account_balance.rb', line 23

def account=()
  self[:account] = .identifier.to_s
  self[:scope] = .scope_identity
  
end

#balanceObject



15
16
17
# File 'lib/double_entry/account_balance.rb', line 15

def balance
  self[:balance] && Money.new(self[:balance], currency)
end

#balance=(money) ⇒ Object



19
20
21
# File 'lib/double_entry/account_balance.rb', line 19

def balance=(money)
  self[:balance] = (money && money.fractional)
end