Class: DoubleEntry::Locking::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/double_entry/locking.rb

Constant Summary collapse

@@locks =
{}

Instance Method Summary collapse

Constructor Details

#initialize(accounts) ⇒ Lock

Returns a new instance of Lock.



55
56
57
58
# File 'lib/double_entry/locking.rb', line 55

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



84
85
86
87
88
# File 'lib/double_entry/locking.rb', line 84

def balance_for()
  ensure_locked!

  locks[]
end

#ensure_locked!Object



76
77
78
79
80
81
82
# File 'lib/double_entry/locking.rb', line 76

def ensure_locked!
  @accounts.each do ||
    unless lock?()
      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.

Returns:

  • (Boolean)


72
73
74
# File 'lib/double_entry/locking.rb', line 72

def in_a_locked_transaction?
  !locks.nil?
end

#perform_lock(&block) ⇒ Object

Lock the given accounts, creating account balance records for them if needed.



62
63
64
65
66
67
68
69
# File 'lib/double_entry/locking.rb', line 62

def perform_lock(&block)
  ensure_outermost_transaction!

  unless lock_and_call(&block)
    
    fail LockDisaster unless lock_and_call(&block)
  end
end