| 1 | package EntityModel::Util; | ||||||||||
| 2 | { | ||||||||||
| 3 | 2 | $EntityModel::Util::VERSION = '0.102'; | |||||||||
| 4 | } | ||||||||||
| 5 | 2 | ▶ | use EntityModel::Class {}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use parent qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | our @EXPORT = qw(as_transaction); | |||||||||
| 8 | 2 | our @EXPORT_OK = qw(as_transaction); | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | EntityModel::Util - helper functions for L<EntityModel> | ||||||||||
| 13 | |||||||||||
| 14 | =head1 VERSION | ||||||||||
| 15 | |||||||||||
| 16 | version 0.102 | ||||||||||
| 17 | |||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | 2 | ▶ | use EntityModel::Transaction; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | =head1 METHODS | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | =head2 as_transaction | ||||||||||
| 29 | |||||||||||
| 30 | Helper function to run the given block as a transaction. | ||||||||||
| 31 | |||||||||||
| 32 | Takes a block, which will be run under a transaction, and the | ||||||||||
| 33 | following optional named parameters: | ||||||||||
| 34 | |||||||||||
| 35 | =over 4 | ||||||||||
| 36 | |||||||||||
| 37 | =item * success - coderef to call on successful completion. The transaction will be committed | ||||||||||
| 38 | before this is called. | ||||||||||
| 39 | |||||||||||
| 40 | =item * failure - coderef to call on failure. This will be called after the transaction has | ||||||||||
| 41 | been rolled back. | ||||||||||
| 42 | |||||||||||
| 43 | =item * goodbye - coderef to call after success/failure. This will always be called regardless | ||||||||||
| 44 | of status, and can be used to chain events similar to L<CPS>. | ||||||||||
| 45 | |||||||||||
| 46 | =back | ||||||||||
| 47 | |||||||||||
| 48 | Returns the transaction in list or scalar context, and in void | ||||||||||
| 49 | context will clean up the transaction automatically. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 5 | ▶ | sub as_transaction(&;@) { | ||||||||
|
Subroutine
as_transaction: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 5 | my $code = shift; | |||||||||
| 55 | 5 | my %args = @_; | |||||||||
| 56 | 5 | my $tran = EntityModel::Transaction->new; | |||||||||
| 57 | 5 | $tran->run($code, %args); | |||||||||
| 58 | # unless we're in void context, pass the transaction back for something else | ||||||||||
| 59 | # to take a crack at it | ||||||||||
| 60 | 5 | ▶ | return $tran if defined wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | $tran->commit; | |||||||||
| 62 | 2 | undef; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 2 | 1; | |||||||||