Class: GitDS::Transaction

Inherits:
ExecCmd
  • Object
show all
Defined in:
lib/git-ds/transaction.rb

Overview

A Transaction takes a code block and guarantees that all of its commands, or none, are executed. Transactions can be nested.

Usage:

db.transaction { |trans, idx| ... }

Constant Summary

Constants inherited from ExecCmd

ExecCmd::DEFAULT_MESSAGE

Instance Attribute Summary collapse

Attributes inherited from ExecCmd

#block, #commit_author, #commit_msg, #database, #nested

Instance Method Summary collapse

Methods inherited from ExecCmd

#actor=, #author, #commit, #initialize, #message

Constructor Details

This class inherits a constructor from GitDS::ExecCmd

Instance Attribute Details

#propagate_exceptionsObject (readonly)

Returns the value of attribute propagate_exceptions.



28
29
30
# File 'lib/git-ds/transaction.rb', line 28

def propagate_exceptions
  @propagate_exceptions
end

Instance Method Details

#indexObject

Overrides ExecCmd#index accessor as the Transaction index will change if batch mode is used.



71
72
73
# File 'lib/git-ds/transaction.rb', line 71

def index
  database.index
end

#performObject

A transaction is considered successful if the transaction block executes with no exceptions. If a block must exit prematurely and abort the transaction, it should use the rollback method.

Note that exceptions are propagated to the parent if transaction is nested; this permits an inner transaction to abort an outer transaction when rollback() is called.



39
40
41
42
43
44
45
46
47
48
# File 'lib/git-ds/transaction.rb', line 39

def perform
  @propagate_exceptions = false # propagation off by default

  return perform_top_level if not self.nested

  instance_eval(&self.block)
  index.build 

  true
end

#propagateObject

Throw all non-rollback exceptions after aborting the transaction. This is useful for debugging transaction blocks.

By default, all exceptions are caught and discarded.



56
57
58
# File 'lib/git-ds/transaction.rb', line 56

def propagate
  @propagate_exceptions = true
end

#rollbackObject

Abort the transaction.



63
64
65
# File 'lib/git-ds/transaction.rb', line 63

def rollback
  raise TransactionRollback.new
end