Class: DoubleEntry::Locking::Lock
- Inherits:
-
Object
- Object
- DoubleEntry::Locking::Lock
- Defined in:
- lib/double_entry/locking.rb
Constant Summary collapse
- @@locks =
{}
Instance Method Summary collapse
- #balance_for(account) ⇒ Object
- #ensure_locked! ⇒ Object
-
#in_a_locked_transaction? ⇒ Boolean
Return true if we're inside a lock_accounts block.
-
#initialize(accounts) ⇒ Lock
constructor
A new instance of Lock.
-
#perform_lock(&block) ⇒ Object
Lock the given accounts, creating account balance records for them if needed.
Constructor Details
#initialize(accounts) ⇒ Lock
Returns a new instance of Lock.
62 63 64 65 |
# File 'lib/double_entry/locking.rb', line 62 def initialize(accounts) # Make sure we always lock in the same order, to avoid deadlocks. @accounts = accounts.flatten.sort end |
Instance Method Details
#balance_for(account) ⇒ Object
91 92 93 94 95 |
# File 'lib/double_entry/locking.rb', line 91 def balance_for(account) ensure_locked! locks[account] end |
#ensure_locked! ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/double_entry/locking.rb', line 83 def ensure_locked! @accounts.each do |account| unless lock?(account) fail LockNotHeld, "No lock held for account: #{account.identifier}, scope #{account.scope}" end end end |
#in_a_locked_transaction? ⇒ Boolean
Return true if we're inside a lock_accounts block.
79 80 81 |
# File 'lib/double_entry/locking.rb', line 79 def in_a_locked_transaction? !locks.nil? end |
#perform_lock(&block) ⇒ Object
Lock the given accounts, creating account balance records for them if needed.
69 70 71 72 73 74 75 76 |
# File 'lib/double_entry/locking.rb', line 69 def perform_lock(&block) ensure_outermost_transaction! unless lock_and_call(&block) create_missing_account_balances fail LockDisaster unless lock_and_call(&block) end end |