Class: Granite::Action::TransactionManager::TransactionsStack

Inherits:
Object
  • Object
show all
Defined in:
lib/granite/action/transaction_manager/transactions_stack.rb

Overview

A class to manage transaction callbacks stack.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransactionsStack

Returns a new instance of TransactionsStack.



8
9
10
11
# File 'lib/granite/action/transaction_manager/transactions_stack.rb', line 8

def initialize
  @callbacks = []
  @depth = 0
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



6
7
8
# File 'lib/granite/action/transaction_manager/transactions_stack.rb', line 6

def depth
  @depth
end

Instance Method Details

#add_callback(callback) ⇒ Object



23
24
25
26
27
# File 'lib/granite/action/transaction_manager/transactions_stack.rb', line 23

def add_callback(callback)
  fail 'Start a transaction before you add callbacks on it' if depth.zero?

  @callbacks.last << callback
end

#callbacksObject



29
30
31
# File 'lib/granite/action/transaction_manager/transactions_stack.rb', line 29

def callbacks
  @callbacks.flatten
end

#transactionObject



13
14
15
16
17
18
19
20
21
# File 'lib/granite/action/transaction_manager/transactions_stack.rb', line 13

def transaction
  start_new!
  result = yield
  finish_current!
  result
rescue StandardError, ScriptError
  rollback_current!
  raise
end