File Coverage

blib/lib/EntityModel/Util.pm
Criterion Covered Total %
statement 16 16 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             package EntityModel::Util;
2             {
3             $EntityModel::Util::VERSION = '0.102';
4             }
5 1     1   35584 use EntityModel::Class {};
  1         94356  
  1         9  
6 1     1   2570 use parent qw(Exporter);
  1         3  
  1         7  
7             our @EXPORT = qw(as_transaction);
8             our @EXPORT_OK = qw(as_transaction);
9              
10             =head1 NAME
11              
12             EntityModel::Util - helper functions for L
13              
14             =head1 VERSION
15              
16             version 0.102
17              
18             =head1 SYNOPSIS
19              
20             =cut
21              
22 1     1   685 use EntityModel::Transaction;
  1         3  
  1         201  
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.
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             sub as_transaction(&;@) {
54 5     5 1 4431 my $code = shift;
55 5         22 my %args = @_;
56 5         210 my $tran = EntityModel::Transaction->new;
57 5         119 $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 100       36 return $tran if defined wantarray;
61 2         35 $tran->commit;
62 2         9 undef;
63             }
64              
65             1;