File Coverage

blib/lib/DBIx/Class/Storage/TxnEndHook.pm
Criterion Covered Total %
statement 39 39 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 11 11 100.0
pod 4 4 100.0
total 60 61 98.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Storage::TxnEndHook;
2 2     2   143324 use 5.008005;
  2         7  
  2         72  
3 2     2   9 use strict;
  2         2  
  2         57  
4 2     2   19 use warnings;
  2         3  
  2         64  
5              
6 2     2   606 use Try::Tiny;
  2         2583  
  2         140  
7 2     2   13 use base 'DBIx::Class::Storage';
  2         2  
  2         1226  
8              
9             __PACKAGE__->mk_group_accessors(simple => qw/_hooks/);
10              
11             our $VERSION = "0.01";
12              
13             sub new {
14 9     9 1 23867 my $self = shift->next::method(@_);
15 9         457 $self->_hooks([]);
16 9         135 $self;
17             }
18              
19             sub add_txn_end_hook {
20 15     15 1 39033 my ($self, $hook) = @_;
21              
22 15 100       50 unless ( $self->transaction_depth > 0 ) {
23 1         139 $self->throw_exception('only can call add_txn_end_hook in transaction');
24             }
25              
26 14         17 push @{ $self->_hooks }, $hook;
  14         35  
27             }
28              
29             sub txn_commit {
30 8     8 1 6136 my $self = shift;
31 8         16 my $is_last_txn = $self->transaction_depth == 1;
32 8         24 my @ret = $self->next::method(@_);
33              
34 8 100 66     471 if ( $is_last_txn && @{ $self->_hooks } ) {
  7         36  
35             try {
36 7     7   185 while ( my $hook = shift @{ $self->_hooks } ) {
  18         75  
37 12         22 $hook->();
38             }
39             }
40             catch {
41 1     1   19 $self->_hooks([]);
42 1         102 warn $_;
43 7         35 };
44             }
45              
46 8         80 @ret;
47             }
48              
49             sub txn_rollback {
50 1     1 1 709 my $self = shift;
51 1         4 my @ret = $self->next::method(@_);
52 1         65 $self->_hooks([]);
53 1         3 @ret;
54             }
55              
56             1;
57             __END__