EntityModel/Collection.pm

filter: Statementstmt 87.8% 65 / 74 Branchbran 53.1% 17 / 32 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 81.4% 105 / 129 CC 19 SCAR 15.0
CC
19
Cov
78%
commit
10.0
__ANON__
7.0
add_handler
3.0
CRAP 4.5
« EntityModel/Cache/Perl.pm ↑ Summary EntityModel/Query/Join.pm »
1 package EntityModel::Collection;
2 {
3 4 $EntityModel::Collection::VERSION = '0.102';
4 }
5 4 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 pending => 'int',
7 event_handler => 'hash',
8 4 };
9 4 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 EntityModel::Collection - manage entity model definitions
14  
15 =head1 VERSION
16  
17 version 0.102
18  
19 =head1 SYNOPSIS
20  
21 =cut
22  
23 =head2 OPERATORS
24  
25 The coderef operator is overloaded by default, allowing syntax such as C< $self->(event => @data) >.
26  
27 =cut
28  
29 use overload
30 # Allow ->(event => @parameter) for laziness
31 4 ▶ '&{}' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
32 8 my $self = shift;
33 6 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=19.5
34 6 my $event = shift;
35 6 my @param = @_;
36 6 logDebug("[%s] %s", $event, join ', ', @param);
37 6 ▶ if(my $handler = $self->{event_handler}->{$event}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $self->{'event_handler'}{$event}) { } 6 0 6
38 try {
39 # Use a temporary so we don't end up in void context
40 6 my $rslt;
41 6 $rslt = $_->($self, @param) for @$handler;
42 6 } catch {
43 6 my $failure = $_;
44 6 logError("Handler on %s for [%s] threw error %s", $self, $event, $failure);
45 6 ▶ die "No failure handler available" unless $self->has_event_handlers_for('fail');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_event_handlers_for("fail") 0 6 6
46 # Raise a failure event, but avoid loops
47 3 try { $self->(fail => $failure); }
48 0 ▶ catch { logError("Also failed in failure handler, this time [%s] original [%s]", $_, $failure) }
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "fail" 3 3 6
49 unless $event eq 'fail';
50 };
51 } else {
52 0 logWarning("No handler for [%s]", $event);
53 }
54 return defined wantarray
55 0 ▶ ? $self
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
56 : $self->commit;
57 }
58 8 },
59 # Could dump out more info on "" perhaps
60 4 fallback => 1;
61  
62 =head2 each
63  
64 Execute the given code for each item that matches the current chain.
65  
66 =cut
67  
68 2 ▶sub each {
Subroutine each: called CC=2 SCAR=7.0
Pod documented
69 2 my $self = shift;
70 2 my $code = shift;
71 2 logDebug("Each");
72 2 $self->add_handler(item => $code);
73 return defined wantarray
74 2 ▶ ? $self
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 2 0 2
75 : $self->commit
76 }
77  
78 =head2 done
79  
80 Supply a coderef which will be called on successful completion of the chain so far, guaranteed
81 to be after any items have been processed.
82  
83 =cut
84  
85 2 ▶sub done {
Subroutine done: called CC=2 SCAR=7.0
Pod documented
86 2 my $self = shift;
87 2 my $code = shift;
88 2 logDebug("Done");
89 2 $self->add_handler(done => $code);
90 return defined wantarray
91 2 ▶ ? $self
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 2 0 2
92 : $self->commit
93 }
94  
95 =head2 fail
96  
97 Supply a coderef which will be called on error. Default behaviour is to die().
98  
99 =cut
100  
101 2 ▶sub fail {
Subroutine fail: called CC=2 SCAR=7.0
Pod documented
102 2 my $self = shift;
103 2 my $code = shift;
104 2 logDebug("Set handler for failed");
105 2 $self->add_handler(fail => $code);
106 return defined wantarray
107 2 ▶ ? $self
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 2 0 2
108 : $self->commit
109 }
110  
111 =head2 commit
112  
113 =cut
114  
115 1 ▶sub commit {
Subroutine commit: called CC=6 SCAR=23.0
Pod documented
116 1 my $self = shift;
117 1 my $code = shift;
118 # Protect against unnecessary calls
119 1 ▶ unless($self->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pending"}) 0 1 1
120 0 logDebug("Commit with nothing pending");
121 0 ▶ $code->() if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
122 0 return $self;
123 }
124  
125 1 logDebug("Commit");
126 # Apply anything we can - and make sure we're not in void context to avoid commit loops
127 1 ▶ if(my $apply = $self->can('apply')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $apply = $self->can("apply")) 0 1 1
128 0 my $x; $x = $apply->($self);
129 }
130 1 $self->pending(0);
131 1 ▶ logDebug("Has pending? %s", ($self->has_pending ? 'yes' : 'no'));
Branch: 1 branch
BranchTrueFalseTotal
$self->has_pending ? : 0 1 1
132  
133 1 ▶ $code->($self) if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 1 1
134 1 $self->('done' => $self);
135 0 return $self;
136 }
137  
138 =head2 add_handler
139  
140 =cut
141  
142 6 ▶sub add_handler {
Subroutine add_handler: called CC=3 SCAR=11.0
Pod documented
143 6 my $self = shift;
144 6 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 6 12
145 6 my ($event, $code) = splice @_, 0, 2;
146 6 logDebug("Defining handler %s for event %s", "$code", $event);
147 6 push @{$self->{event_handler}->{$event}}, $code;
148 6 ▶ ++$self->{pending} if $event ~~ [qw(item done)];
Branch: 1 branch
BranchTrueFalseTotal
if $event ~~ ["item", "done"] 4 2 6
149 }
150 6 return $self;
151 }
152  
153 =head2 has_event_handlers_for
154  
155 Returns how many event handlers are defined for this event.
156  
157 =cut
158  
159 6 ▶sub has_event_handlers_for {
Subroutine has_event_handlers_for: called CC=2 SCAR=7.0
Pod documented
160 6 my $self = shift;
161 6 ▶ my $event = shift or die "Invalid event passed";
Branch: 1 branch
BranchTrueFalseTotal
my $event = shift() or die "Invalid event passed" 6 0 6
162 6 return scalar @{$self->{event_handler}->{$event}};
163 }
164  
165 =head2 has_pending
166  
167 Returns true if there's anything pending, false otherwise.
168  
169 =cut
170  
171 2 ▶sub has_pending { (shift->pending || 0) > 0 }
Subroutine has_pending: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
(shift())->pending || 0
l!l
11
MC/DC100%1/1
(shift())->pending || 0
(shift())->pending
Truth table100%2/2
A
A= (shift())->pending
Aresult
11
00
172  
173 =head2 DESTROY
174  
175 When we go out of scope, we want any pending actions to be applied immediately.
176  
177 =cut
178  
179 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
180 1 my $self = shift;
181 1 ▶ $self->commit if $self->has_pending;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_pending 1 0 1
182 }
183  
184 4 1;
« EntityModel/Cache/Perl.pm ↑ Summary EntityModel/Query/Join.pm »