Class: FileTransactions::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/file_transactions/transaction.rb

Overview

A class that keeps track of commands and nested transactions. If an exception is raised inside the transaction, then all commands and nested transactions get undone.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Transaction

Returns a new instance of Transaction.

Raises:



23
24
25
26
27
28
# File 'lib/file_transactions/transaction.rb', line 23

def initialize(&block)
  raise Error, 'A block must be given' unless block_given?

  @block = block
  @commands = []
end

Class Method Details

.run(&block) ⇒ Object



9
10
11
# File 'lib/file_transactions/transaction.rb', line 9

def run(&block)
  new(&block).__send__(:run)
end

.scopeObject



17
18
19
# File 'lib/file_transactions/transaction.rb', line 17

def scope
  Thread.current['FT.scope']
end

.scope=(scope) ⇒ Object



13
14
15
# File 'lib/file_transactions/transaction.rb', line 13

def scope=(scope)
  Thread.current['FT.scope'] = scope
end

Instance Method Details

#backrolled?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/file_transactions/transaction.rb', line 42

def backrolled?
  !!backrolled
end

#register(command) ⇒ Object



30
31
32
# File 'lib/file_transactions/transaction.rb', line 30

def register(command)
  commands << command
end

#rollbackObject Also known as: undo



34
35
36
37
38
39
# File 'lib/file_transactions/transaction.rb', line 34

def rollback
  return if backrolled?

  commands.reverse_each(&:undo)
  self.backrolled = true
end