EntityModel/Transaction.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 81.2% 13 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 16 / 17 Podpod 85.7% 6 / 7 totaltotal 89.0% 89 / 100 CC 11 SCAR 7.2
CC
11
Cov
91%
apply
3.2
DESTROY
3.2
run
3.0
CRAP 2.0
« EntityModel/Plugin.pm ↑ Summary EntityModel/Deferred.pm »
1 package EntityModel::Transaction;
2 {
3 39 $EntityModel::Transaction::VERSION = '0.102';
4 }
5 39 ▶use EntityModel::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use overload '&{}' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
7 4 my $self = shift;
8 4 sap($self, 'apply');
9 39 }, fallback => 1;
10 39 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 EntityModel::Transaction - transaction co-ordinator
15  
16 =head1 VERSION
17  
18 version 0.102
19  
20 =head1 SYNOPSIS
21  
22 See L<EntityModel>.
23  
24 =head1 DESCRIPTION
25  
26 Contacts each L<EntityModel::Storage> instance and requests that they join a new
27 transaction.
28  
29 See L<EntityModel>.
30  
31 =head2 apply
32  
33 Applies the current transaction. Typically called as the last
34 step in the transaction codeblock. The &{} overload will call
35 this method (so you can use C< $tran->() > or C< $tran->apply >
36 interchangeably).
37  
38 Takes no parameters.
39  
40 Returns $self.
41  
42 =cut
43  
44 4 ▶sub apply {
Subroutine apply: called CC=3 SCAR=11.7
Pod documented
45 4 my $self = shift;
46  
47 4 ▶ return $self if $self->{rolled_back};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rolled_back"} 1 3 4
48 3 ▶ return $self unless try {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 # commit
50 3 1;
51 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 $self->mark_failure;
53 0 $self->mark_goodbye;
54 0 0
55 3 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
unless &try(sub { 1; } , &catch(sub { $self->mark_failure; $self->mark_goodbye; 0; } )) 0 3 3
56 3 $self->mark_success;
57 3 $self->mark_goodbye;
58 3 $self;
59 }
60  
61 =head2 mark_failure
62  
63 Mark this transaction as failed, applying rollback if required, and
64 calls the failure coderef.
65  
66 Takes no parameters.
67  
68 Returns $self.
69  
70 =cut
71  
72 2 ▶sub mark_failure {
Subroutine mark_failure: called CC=2 SCAR=7.0
Pod documented
73 2 my $self = shift;
74 # rollback here
75 2 $self->{rolled_back} = 1;
76 2 ▶ $self->{on_failure}->($self) if exists $self->{on_failure};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"on_failure"} 2 0 2
77 2 $self
78 }
79  
80 =head2 mark_success
81  
82 Mark this transaction as successful, committing if required, and
83 calls the success coderef.
84  
85 Takes no parameters.
86  
87 Returns $self.
88  
89 =cut
90  
91 3 ▶sub mark_success {
Subroutine mark_success: called CC=2 SCAR=6.9
Pod documented
92 3 my $self = shift;
93 3 ▶ $self->{on_success}->($self) if exists $self->{on_success};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"on_success"} 2 1 3
94 3 $self
95 }
96  
97 =head2 mark_goodbye
98  
99 Mark this transaction as completed. Calls the C<goodbye> coderef
100 if available.
101  
102 Takes no parameters.
103  
104 Returns $self.
105  
106 =cut
107  
108 5 ▶sub mark_goodbye {
Subroutine mark_goodbye: called CC=2 SCAR=6.9
Pod documented
109 5 my $self = shift;
110 5 ▶ $self->{on_goodbye}->($self) if exists $self->{on_goodbye};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"on_goodbye"} 4 1 5
111 5 $self
112 }
113  
114 =head2 run
115  
116 Takes the following (named) parameters:
117  
118 =over 4
119  
120 =item *
121  
122 =back
123  
124 Returns $self.
125  
126 =cut
127  
128 5 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
129 5 my $self = shift;
130 5 my $code = shift;
131 5 my %args = @_;
132 5 $self->{"on_$_"} = delete $args{$_} for grep exists $args{$_}, qw(success failure goodbye);
133 # Attempt the transaction bit, normally we'd expect
134 # this to set up a few deferred events and return
135 # quickly
136 5 ▶ return $self unless try {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 5 sap($self, $code)->();
138 3 1;
139 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 # Something went wrong, bail
141 2 $self->mark_failure;
142 2 $self->mark_goodbye;
143 2 0;
144 5 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
unless &try(sub { sap($self, $code)->(); 1; } , &catch(sub { $self->mark_failure; $self->mark_goodbye; 0; } )) 2 3 5
145 3 $self;
146 }
147  
148 5 ▶sub commit {
Subroutine commit: called CC=2 SCAR=6.9
Pod undocumented
149 5 my $self = shift;
150 5 ▶ return $self if $self->{rolled_back};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rolled_back"} 2 3 5
151 3 $self->{committed} = 1;
152 3 $self
153 }
154  
155 5 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.7
156 5 my $self = shift;
157 5 ▶ warn "did not finish" unless $self->{committed} || $self->{rolled_back};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"committed"} or $self->{"rolled_back"} 0 5 5
Condition66%2/3
$self->{"committed"} or $self->{"rolled_back"}
l!l&&r!l&&!r
320
MC/DC0%0/2
$self->{"committed"} or $self->{"rolled_back"}
$self->{"committed"} $self->{"rolled_back"}
Truth table66%2/3
A or B
A= $self->{"committed"}B= $self->{"rolled_back"}
ABresult
1X1
011
000
158 }
159  
160 9 ▶sub sap {
Subroutine sap: called CC=1 SCAR=0.0
Pod documented
161 9 my ($self, $sub) = @_;
162 9 Scalar::Util::weaken $self;
163 9 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
164 9 $self->$sub(@_);
165 9 };
166 }
167  
168 39 1;
169  
170 __END__
« EntityModel/Plugin.pm ↑ Summary EntityModel/Deferred.pm »