blib/lib/MooX/late.pm

filter: Statementstmt 94.6% 106 / 112 Branchbran 67.8% 19 / 28 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 2 totaltotal 81.1% 151 / 186 CC 26 SCAR 18.3
CC
26
Cov
84%
_handle_lazy_build
18.1
__ANON__
6.0
has
6.0
CRAP 6.2
↑ Summary
1 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package MooX::late;
6 7 our $AUTHORITY = 'cpan:TOBYINK';
7 7 our $VERSION = '0.100';
8  
9 7 ▶use Moo qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Module::Runtime qw( is_module_name );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 package MooX::late::DefinitionContext;
16 7 our $AUTHORITY = 'cpan:TOBYINK';
17 7 our $VERSION = '0.100';
18
19 7 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.2
21 q[""] => 'to_string',
22 0 ▶ q[bool] => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 7 fallback => 1,
24 7 );
25
26 7 has package => (is => 'ro');
27 7 has filename => (is => 'ro');
28 7 has line => (is => 'ro');
29
30 sub to_string
31 0 ▶ {
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my $self = shift;
33 0 sprintf(
34 '%s:%d, package %s',
35 $self->filename,
36 $self->line,
37 $self->package,
38 );
39 }
40
41 sub new_from_caller
42 42 ▶ {
Subroutine new_from_caller: called CC=2 SCAR=7.0
Pod undocumented
43 42 my ($class, $level) = @_;
44 42 ▶ $level = 0 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 42 42
45
46 42 my ($p, $f, $c) = caller($level + 1);
47 42 return $class->new(
48 package => $p,
49 filename => $f,
50 line => $c,
51 );
52 }
53 };
54  
55 # SUBCLASSING
56 # This is a hook for people subclassing MooX::late.
57 # It should be easy to tack on your own handlers
58 # to the end of the list. A handler is only called
59 # if exists($spec{$handler_name}) in the attribute
60 # spec.
61 #
62 sub _handlers
63 42 ▶{
Subroutine _handlers: called CC=1 SCAR=0.0
64 42 qw( isa does lazy_build );
65 }
66  
67 # SUBCLASSING
68 # Not really sure why you'd want to override
69 # this.
70 #
71 sub _definition_context_class
72 42 ▶{
Subroutine _definition_context_class: called CC=1 SCAR=0.0
73 42 "MooX::late::DefinitionContext";
74 }
75  
76 sub import
77 42 ▶{
Subroutine import: called CC=4 SCAR=13.9
78 42 my $me = shift;
79 42 my $caller = caller;
80
81 42 require Sub::HandlesVia;
82 42 require Sub::HandlesVia::Toolkit::Moo;
83 42 'Sub::HandlesVia::Toolkit::Moo'->setup_for($caller);
84
85 42 my $install_tracked;
86 {
87 7 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 42 ▶ if ($Moo::MAKERS{$caller})
Branch: 2 branches
BranchTrueFalseTotal
if ($Moo::MAKERS{$caller}) { } 40 2 42
elsif ($Moo::Role::INFO{$caller}) { } 2 0 2
89 {
90 40 $install_tracked = \&Moo::_install_tracked;
91 }
92 elsif ($Moo::Role::INFO{$caller})
93 {
94 2 $install_tracked = \&Moo::Role::_install_tracked;
95 }
96 else
97 {
98 0 croak "MooX::late applied to a non-Moo package"
99 . "(need: use Moo or use Moo::Role)";
100 }
101 }
102
103 42 ▶ my $orig = $caller->can('has') # lolcat
Branch: 1 branch
BranchTrueFalseTotal
my $orig = $caller->can("has") or croak("Could not locate 'has' function to alter") 42 0 42
104 or croak "Could not locate 'has' function to alter";
105
106 42 my @handlers = $me->_handlers;
107
108 # SUBCLASSING
109 # MooX::late itself does not provide a
110 # `_finalize_attribute` method. Your subclass
111 # can, in which case it will be called right
112 # before setting up the attribute.
113 #
114 42 my $finalize = $me->can("_finalize_attribute");
115
116 $install_tracked->(
117 $caller, has => sub
118 38 ▶ {
Subroutine has: called CC=6 SCAR=18.0
Subroutine __ANON__: called CC=6 SCAR=18.0
119 38 my ($proto, %spec) = @_;
120 38 my $context = $me->_definition_context_class->new_from_caller(0);
121
122 38 ▶ for my $name (ref $proto ? @$proto : $proto)
Branch: 2 branches
BranchTrueFalseTotal
ref $proto ? : 0 38 38
ref $proto ? : 2 2 4
123 {
124 38 my $spec = +{ %spec }; # shallow clone
125
126 38 for my $option (@handlers)
127 {
128 112 ▶ next unless exists $spec->{$option};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $spec->{$option} 74 38 112
unless exists $spec->{$option} 8 10 18
129 38 my $handler = $me->can("_handle_$option");
130
131 # SUBCLASSING
132 # Note that handlers are called as methods, and
133 # get passed:
134 # 1. the attribute name
135 # 2. the attribute spec (hashref, modifiable)
136 # 3. a context object
137 # 4. the name of the caller class/role
138 #
139 38 $me->$handler($name, $spec, $context, $caller);
140 }
141
142 37 ▶ $me->$finalize($name, $spec, $context, $caller) if $finalize;
Branch: 2 branches
BranchTrueFalseTotal
if $finalize 0 37 37
if $finalize 0 6 6
143 37 $orig->($name, %$spec);
144 }
145 37 return;
146 },
147 42 );
148
149 42 $me->_install_sugar($caller, $install_tracked);
150 }
151  
152 # SUBCLASSING
153 # This can be used to install additional functions
154 # into the caller package.
155 #
156 sub _install_sugar
157 42 ▶{
Subroutine _install_sugar: called CC=1 SCAR=0.0
158 42 my $me = shift;
159 42 my ($caller, $installer) = @_;
160 42 $installer->($caller, blessed => \&Scalar::Util::blessed);
161 42 $installer->($caller, confess => \&Carp::confess);
162 }
163  
164 sub _handle_isa
165 42 ▶{
Subroutine _handle_isa: called CC=2 SCAR=6.9
166 42 my $me = shift;
167 42 my ($name, $spec, $context, $class) = @_;
168 42 ▶ return if ref $spec->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec->{"isa"} 1 41 42
169
170 41 require Type::Utils;
171 41 $spec->{isa} = Type::Utils::dwim_type($spec->{isa}, for => $class);
172
173 40 return;
174 }
175  
176 sub _handle_does
177 1 ▶{
Subroutine _handle_does: called CC=2 SCAR=7.0
178 1 my $me = shift;
179 1 my ($name, $spec, $context, $class) = @_;
180 1 ▶ return unless defined $spec->{does};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec->{"does"} 0 1 1
181
182 1 require Types::Standard;
183 1 $spec->{isa} = Types::Standard::ConsumerOf()->of($spec->{does});
184
185 1 return;
186 }
187  
188 sub _handle_lazy_build
189 5 ▶{
Subroutine _handle_lazy_build: called CC=10 SCAR=29.0
190 5 my $me = shift;
191 5 my ($name, $spec, $context, $class) = @_;
192 5 ▶ return unless delete $spec->{lazy_build};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $spec->{"lazy_build"} 0 5 5
193
194 5 ▶ $spec->{is} ||= "ro";
Condition50%1/2
$spec->{"is"} ||= "ro"
l!l
50
MC/DC0%0/1
$spec->{"is"} ||= "ro"
$spec->{"is"}
Truth table50%1/2
A
A= $spec->{"is"}
Aresult
11
00
195 5 ▶ $spec->{lazy} ||= 1;
Condition50%1/2
$spec->{"lazy"} ||= 1
l!l
05
MC/DC0%0/1
$spec->{"lazy"} ||= 1
$spec->{"lazy"}
Truth table50%1/2
A
A= $spec->{"lazy"}
Aresult
11
00
196 5 ▶ $spec->{builder} ||= "_build_$name";
Condition50%1/2
$spec->{"builder"} ||= "_build_$name"
l!l
05
MC/DC0%0/1
$spec->{"builder"} ||= "_build_$name"
$spec->{"builder"}
Truth table50%1/2
A
A= $spec->{"builder"}
Aresult
11
00
197
198 5 ▶ if ($name =~ /^_/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) { } 0 5 5
199 {
200 0 ▶ $spec->{clearer} ||= "_clear$name";
Condition0%0/2
$spec->{"clearer"} ||= "_clear$name"
l!l
00
MC/DC0%0/1
$spec->{"clearer"} ||= "_clear$name"
$spec->{"clearer"}
Truth table0%0/2
A
A= $spec->{"clearer"}
Aresult
11
00
201 0 ▶ $spec->{predicate} ||= "_has$name";
Condition0%0/2
$spec->{"predicate"} ||= "_has$name"
l!l
00
MC/DC0%0/1
$spec->{"predicate"} ||= "_has$name"
$spec->{"predicate"}
Truth table0%0/2
A
A= $spec->{"predicate"}
Aresult
11
00
202 }
203 else
204 {
205 5 ▶ $spec->{clearer} ||= "clear_$name";
Condition50%1/2
$spec->{"clearer"} ||= "clear_$name"
l!l
05
MC/DC0%0/1
$spec->{"clearer"} ||= "clear_$name"
$spec->{"clearer"}
Truth table50%1/2
A
A= $spec->{"clearer"}
Aresult
11
00
206 5 ▶ $spec->{predicate} ||= "has_$name";
Condition50%1/2
$spec->{"predicate"} ||= "has_$name"
l!l
05
MC/DC0%0/1
$spec->{"predicate"} ||= "has_$name"
$spec->{"predicate"}
Truth table50%1/2
A
A= $spec->{"predicate"}
Aresult
11
00
207 }
208
209 5 return;
210 }
211  
212 7 1;
213  
214 __END__
↑ Summary