File Coverage

blib/lib/DBIx/Class/Storage/TxnEndHook.pm
Criterion Covered Total %
statement 38 40 95.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 11 12 91.6
pod 4 5 80.0
total 59 64 92.1


line stmt bran cond sub pod time code
1             package DBIx::Class::Storage::TxnEndHook;
2 2     2   238810 use 5.008005;
  2         20  
3 2     2   10 use strict;
  2         4  
  2         40  
4 2     2   9 use warnings;
  2         7  
  2         61  
5              
6 2     2   588 use Try::Tiny;
  2         2247  
  2         133  
7 2     2   13 use base 'DBIx::Class::Storage';
  2         6  
  2         1508  
8              
9             __PACKAGE__->mk_group_accessors(simple => qw/_hooks/);
10              
11             our $VERSION = "0.02";
12              
13             our $WARN_PREFIX = "";
14              
15             sub new {
16 9     9 1 37497 my $self = shift->next::method(@_);
17 9         599 $self->_hooks([]);
18 9         234 $self;
19             }
20              
21             sub add_txn_end_hook {
22 15     15 1 53282 my ($self, $hook) = @_;
23              
24 15 100       59 unless ( $self->transaction_depth > 0 ) {
25 1         194 $self->throw_exception('only can call add_txn_end_hook in transaction');
26             }
27              
28 14         36 push @{ $self->_hooks }, $hook;
  14         57  
29             }
30              
31             sub txn_commit {
32 8     8 1 9787 my $self = shift;
33 8         21 my $is_last_txn = $self->transaction_depth == 1;
34 8         21 my @ret = $self->next::method(@_);
35              
36 8 100 66     406 if ( $is_last_txn && @{ $self->_hooks } ) {
  7         31  
37             try {
38 7     7   313 while ( my $hook = shift @{ $self->_hooks } ) {
  18         113  
39 12         35 $hook->();
40             }
41             }
42             catch {
43 1     1   29 $self->_hooks([]);
44 1         63 warn $WARN_PREFIX . $_;
45 7         48 };
46             }
47              
48 8         105 @ret;
49             }
50              
51             sub txn_rollback {
52 1     1 1 1027 my $self = shift;
53 1         6 my @ret = $self->next::method(@_);
54 1         50 $self->_hooks([]);
55 1         5 @ret;
56             }
57              
58             sub warn_prefix {
59 0     0 0   my $class = shift;
60 0           $WARN_PREFIX = shift;
61             }
62              
63             1;
64             __END__