Class: FileTransactions::Transaction
- Inherits:
-
Object
- Object
- FileTransactions::Transaction
- 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
- #backrolled? ⇒ Boolean
-
#initialize(&block) ⇒ Transaction
constructor
A new instance of Transaction.
- #register(command) ⇒ Object
- #rollback ⇒ Object (also: #undo)
Constructor Details
#initialize(&block) ⇒ Transaction
Returns a new instance of Transaction.
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 |
.scope ⇒ Object
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
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 |
#rollback ⇒ Object 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 |