Class: DoubleEntry::AccountBalance
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- DoubleEntry::AccountBalance
- 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
- .find_by_account(account, options = {}) ⇒ Object
-
.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.
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.find_by_account(account, = {}) scope = where(scope: account.scope_identity, account: account.identifier.to_s) scope = scope.lock(true) if [: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.
50 51 52 |
# File 'lib/double_entry/account_balance.rb', line 50 def self.scopes_with_minimum_balance_for_account(minimum_balance, account_identifier) where(account: account_identifier).where('balance >= ?', minimum_balance.fractional).pluck(:scope) end |
Instance Method Details
#account ⇒ Object
27 28 29 |
# File 'lib/double_entry/account_balance.rb', line 27 def account DoubleEntry.account(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=(account) self[:account] = account.identifier.to_s self[:scope] = account.scope_identity account end |
#balance ⇒ Object
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 |