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



31
32
33
34
35
# File 'lib/double_entry/account_balance.rb', line 31

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

.scopes_with_minimum_balance_for_account(minimum_balance, account_identifier) ⇒ Array<String>

Identify the scopes with the given account identifier holding at least the provided minimum balance.

Examples:

Find users with at least $1,000,000 in their savings accounts

DoubleEntry::AccountBalance.(
  1_000_000.dollars,
  :savings,
) # might return the user ids: [ '1423', '12232', '34729' ]

Parameters:

  • minimum_balance (Money)

    Minimum account balance a scope must have to be included in the result set.

  • account_identifier (Symbol)

Returns:

  • (Array<String>)

    Scopes



50
51
52
# File 'lib/double_entry/account_balance.rb', line 50

def self.(minimum_balance, )
  where(account: ).where('balance >= ?', minimum_balance.fractional).pluck(:scope)
end

Instance Method Details

#accountObject



27
28
29
# File 'lib/double_entry/account_balance.rb', line 27

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

#account=(account) ⇒ Object



21
22
23
24
25
# File 'lib/double_entry/account_balance.rb', line 21

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

#balanceObject



13
14
15
# File 'lib/double_entry/account_balance.rb', line 13

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

#balance=(money) ⇒ Object



17
18
19
# File 'lib/double_entry/account_balance.rb', line 17

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