blib/lib/MooX/SetOnce.pm

filter: Statementstmt 84.2% 32 / 38 Branchbran 87.5% 7 / 8 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 76.0% 57 / 75 SCAR 24.8
CC
11
Cov
80%
__ANON__
9.2
__ANON__
6.0
__ANON__
3.0
CRAP 12.0
↑ Summary
1 package MooX::SetOnce;
2 2 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '0.001002';
5 2 $VERSION = eval $VERSION;
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Class::Method::Modifiers qw(install_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub import {
11 2 ▶ my ($class) = @_;
Subroutine import: called CC=1 SCAR=0.2
12 2 my $target = caller;
13  
14 install_modifier $target, 'around', 'has', sub {
15 14 ▶ my $orig = shift;
Subroutine __ANON__: called CC=8 SCAR=22.2
16 14 my ($attr, %opts) = @_;
17 return $orig->($attr, %opts)
18 14 ▶ unless delete $opts{once};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $opts{"once"} 2 12 14
19  
20 12 my $is = $opts{is};
21 12 my $writer = $opts{writer};
22 12 ▶ if ($is eq 'rw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($is eq 'rw') { } 10 2 12
elsif ($is eq 'rwp') { } 2 0 2
23 10 ▶ $writer ||= $attr;
Condition66%2/3
$writer ||= $attr
l!l&&r!l&&!r
280
MC/DC0%0/2
$writer ||= $attr
$writer $attr
Truth table66%2/3
A ||= B
A= $writerB= $attr
ABresult
1X1
011
000
24 }
25 elsif ($is eq 'rwp') {
26 2 ▶ $writer ||= "_set_$attr";
Condition50%1/2
$writer ||= "_set_$attr"
l!l
02
MC/DC0%0/1
$writer ||= "_set_$attr"
$writer
Truth table50%1/2
A
A= $writer
Aresult
11
00
27 }
28 else {
29 0 croak "SetOnce can't be used on read-only accessors";
30 }
31 12 ▶ my $predicate = $opts{predicate} ||= '_has_' . $attr;
Condition100%2/2
$opts{"predicate"} ||= '_has_' . $attr
l!l
48
MC/DC100%1/1
$opts{"predicate"} ||= '_has_' . $attr
$opts{"predicate"}
Truth table100%2/2
A
A= $opts{"predicate"}
Aresult
11
00
32  
33 12 ▶ $opts{moosify} ||= [];
Condition50%1/2
$opts{"moosify"} ||= []
l!l
012
MC/DC0%0/1
$opts{"moosify"} ||= []
$opts{"moosify"}
Truth table50%1/2
A
A= $opts{"moosify"}
Aresult
11
00
34 12 push @{$opts{moosify}}, sub {
35 0 ▶ my ($spec) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
36 require # hide from CPANTS
37 0 MooseX::SetOnce;
38 0 ▶ $spec->{traits} ||= [];
Condition0%0/2
$spec->{"traits"} ||= []
l!l
00
MC/DC0%0/1
$spec->{"traits"} ||= []
$spec->{"traits"}
Truth table0%0/2
A
A= $spec->{"traits"}
Aresult
11
00
39 0 push @{$spec->{traits}}, 'SetOnce';
40 12 };
41  
42 12 $orig->($attr, %opts);
43  
44 $target->can('before')->($writer, sub {
45 24 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
46 24 ▶ if (@_ > 1 && $self->$predicate) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1 and $self->$predicate) 10 14 24
Condition100%3/3
@_ > 1 and $self->$predicate
!ll&&!rl&&r
21210
MC/DC100%2/2
@_ > 1 and $self->$predicate
@_ > 1 $self->$predicate
Truth table100%3/3
A and B
A= @_ > 1B= $self->$predicate
ABresult
0X0
100
111
47 10 croak "cannot change value of SetOnce attribute $attr";
48 }
49 12 });
50 }
51 2 }
52  
53 2 1;
54  
55 __END__
↑ Summary