blib/lib/MooseX/RoleQR.pm

filter: Statementstmt 94.9% 188 / 198 Branchbran 50.0% 9 / 18 Conditioncond 30.0% 3 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 95.0% 58 / 61 Podpod 66.6% 4 / 6 totaltotal 87.9% 263 / 299 CC 20 SCAR 9.4
CC
20
Cov
88%
__ANON__
12.0
matches_name
8.7
apply_deferred_method_modifiers
6.0
CRAP 2.6
↑ Summary
1 package MooseX::RoleQR;
2  
3 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 $MooseX::RoleQR::AUTHORITY = 'cpan:TOBYINK';
10 2 $MooseX::RoleQR::VERSION = '0.004';
11 }
12  
13 2 ▶use Moose ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Does qw( does blessed -constants );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 Moose::Exporter->setup_import_methods(
18 with_meta => [qw/ before after around /],
19 also => 'Moose::Role',
20 );
21  
22 2 my %ROLE_METAROLES = (
23 role => ['MooseX::RoleQR::Trait::Role'],
24 application_to_class => ['MooseX::RoleQR::Trait::Application::ToClass'],
25 application_to_role => ['MooseX::RoleQR::Trait::Application::ToRole'],
26 );
27 2 my %ARGH;
28  
29 sub _add_method_modifier
30 4 ▶{
Subroutine _add_method_modifier: called CC=3 SCAR=12.3
31 4 my $type = shift;
32 4 my $meta = shift;
33  
34 4 ▶ if (does($_[0], REGEXP) or does($_[0], CODE))
Branch: 1 branch
BranchTrueFalseTotal
if (&does($_[0], "qr") or &does($_[0], "CODE")) 4 0 4
Condition33%1/3
&does($_[0], "qr") or &does($_[0], "CODE")
l!l&&r!l&&!r
400
MC/DC0%0/2
&does($_[0], "qr") or &does($_[0], "CODE")
&does($_[0], "qr") &does($_[0], "CODE")
Truth table33%1/3
A or B
A= &does($_[0], "qr")B= &does($_[0], "CODE")
ABresult
1X1
011
000
35 {
36 4 my $pusher = "add_deferred_${type}_method_modifier";
37 4 return $meta->$pusher(@_);
38 }
39  
40 0 Moose::Util::add_method_modifier($meta, $type, \@_);
41 }
42  
43 4 ▶sub before { _add_method_modifier(before => @_) }
Subroutine before: called CC=1 SCAR=0.0
Pod documented
44 0 ▶sub after { _add_method_modifier(after => @_) }
Subroutine after: not called CC=1 SCAR=6.9
Pod documented
45 0 ▶sub around { _add_method_modifier(around => @_) }
Subroutine around: not called CC=1 SCAR=6.9
Pod documented
46  
47 sub init_meta
48 4 ▶{
Subroutine init_meta: called CC=1 SCAR=0.0
Pod documented
49 4 my $class = shift;
50 4 my %options = @_;
51 4 Moose::Role->init_meta(%options);
52
53 Moose::Util::MetaRole::apply_metaroles(
54 for => $options{for_class},
55 4 role_metaroles => \%ROLE_METAROLES,
56 );
57 }
58  
59 {
60 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 my $orig = Moose::Meta::Role->can('combine');
62 4 ▶ *Moose::Meta::Role::combine = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 4 my ($meta, @role_specs) = @_;
64 4 my $combo = $meta->$orig(@role_specs);
65 4 return bless(
66 $combo,
67 Moose::Util::with_traits(
68 ref($combo),
69 'MooseX::RoleQR::Trait::Role::Composite',
70 ),
71 );
72 }
73 2 };
74  
75 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
76 package MooseX::RoleQR::Meta::DeferredModifier;
77 2 ▶ no thanks;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶ use Scalar::Does -constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 our $AUTHORITY = 'cpan:TOBYINK';
85 2 our $VERSION = '0.004';
86 };
87  
88 2 has [qw/ expression body /] => (is => 'ro', required => 1);
89
90 # sub matches_name
91 # {
92 # my ($meta, $name) = @_;
93 # my $return = $meta->_matches_name($name);
94 # print ($return ? "@{[$meta->expression]} matches $name\n" : "@{[$meta->expression]} does not match $name\n");
95 # return $return;
96 # }
97 #
98 # sub _matches_name
99 sub matches_name
100 140 ▶ {
Subroutine matches_name: called CC=4 SCAR=21.7
Pod undocumented
101 140 my ($meta, $name, $hints) = @_;
102 140 my $expr = $meta->expression;
103 140 ▶ return $name =~ $expr if does($expr, REGEXP);
Branch: 1 branch
BranchTrueFalseTotal
if &does($expr, "qr") 140 0 140
104 0 ▶ return $expr->($name, @{$hints||[]}) if does($expr, CODE); # ssh... secret!
Branch: 1 branch
BranchTrueFalseTotal
if &does($expr, "CODE") 0 0 0
Condition0%0/2
$hints || []
l!l
00
MC/DC0%0/1
$hints || []
$hints
Truth table0%0/2
A
A= $hints
Aresult
11
00
105 0 return;
106 }
107 };
108  
109 2 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.2
110 package MooseX::RoleQR::Trait::Role;
111 2 ▶ no thanks;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶ use Scalar::Does -constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
116
117 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 our $AUTHORITY = 'cpan:TOBYINK';
120 2 our $VERSION = '0.004';
121 };
122  
123 has deferred_modifier_class => (
124 is => 'ro',
125 isa => 'ClassName',
126 lazy => 1,
127 4 ▶ default => sub { 'MooseX::RoleQR::Meta::DeferredModifier' },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 2 );
129
130 2 for my $type (qw( after around before )) #override
131 {
132 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 6 my $attr = "deferred_${type}_method_modifiers";
134 has $attr => (
135 traits => ['Array'],
136 is => 'ro',
137 isa => 'ArrayRef[MooseX::RoleQR::Meta::DeferredModifier]',
138 lazy => 1,
139 15 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
140 6 handles => {
141 "has_deferred_${type}_method_modifiers" => "count",
142 },
143 );
144
145 6 my $pusher = "add_deferred_${type}_method_modifier";
146 1 ▶ *$pusher = sub {
Subroutine add_deferred_after_method_modifier: called CC=2 SCAR=7.0
Subroutine add_deferred_around_method_modifier: called CC=2 SCAR=7.0
147 1 my ($meta, $expression, $body) = @_;
148 1 ▶ my $modifier = does($expression, 'MooseX::RoleQR::Meta::DeferredModifier')
Branch: 2 branches
BranchTrueFalseTotal
&does($expression, 'MooseX::RoleQR::Meta::DeferredModifier') ? : 0 1 1
&does($expression, 'MooseX::RoleQR::Meta::DeferredModifier') ? : 0 3 3
149 ? $expression
150 : $meta->deferred_modifier_class->new(expression => $expression, body => $body);
151 1 push @{ $meta->$attr }, $modifier;
152 6 };
153
154 0 ▶ around "add_${type}_method_modifier" => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
155 0 my ($orig, $meta, $expression, $body) = @_;
156 0 ▶ if (does($expression, 'MooseX::RoleQR::Meta::DeferredModifier')
Branch: 1 branch
BranchTrueFalseTotal
if (&does($expression, 'MooseX::RoleQR::Meta::DeferredModifier') or &does($expression, 'qr')) { } 0 0 0
Condition0%0/3
&does($expression, "MooseX::RoleQR::Meta::DeferredModifier") or &does($expression, "qr")
l!l&&r!l&&!r
000
MC/DC0%0/2
&does($expression, "MooseX::RoleQR::Meta::DeferredModifier") or &does($expression, "qr")
&does($expression, "MooseX::RoleQR::Meta::DeferredModifier") &does($expression, "qr")
Truth table0%0/3
A or B
A= &does($expression, "MooseX::RoleQR::Meta::DeferredModifier")B= &does($expression, "qr")
ABresult
1X1
011
000
157 or does($expression, REGEXP))
158 0 { return $meta->$pusher($expression, $body) }
159 else
160 0 { return $meta->$orig($expression, $body) }
161 6 };
162
163 6 ▶ next if $type eq 'override';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "override" 0 6 6
164 6 ▶ *{"get_deferred_${type}_method_modifiers"} = sub {
Subroutine get_deferred_before_method_modifiers: called CC=1 SCAR=0.0
165 474 my ($meta, $name, $hints) = @_;
166 474 grep { $_->matches_name($name, $hints) } @{ $meta->$attr };
167 6 };
168 }
169
170 # sub get_deferred_override_method_modifier
171 # {
172 # my ($meta, $name) = @_;
173 # my @r = grep { $_->matches_name($name) } @{ $meta->deferred_override_method_modifiers };
174 # carp sprintf(
175 # "%s has multiple override modifiers for method %s",
176 # $meta->name,
177 # $name,
178 # ) if @r > 1;
179 # return $r[0];
180 # }
181 };
182  
183 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
184 package MooseX::RoleQR::Trait::Role::Composite;
185 2 ▶ no thanks;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 2 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 2 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
188
189 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
190 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
191 2 our $AUTHORITY = 'cpan:TOBYINK';
192 2 our $VERSION = '0.004';
193 };
194  
195 4 ▶ after apply => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
196 4 my ($meta, $class) = @_;
197 4 ▶ if ($class->isa('Moose::Meta::Class'))
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa('Moose::Meta::Class')) { } 3 1 4
198 {
199 3 foreach my $role (@{ $meta->get_roles })
200 {
201 6 foreach my $modifier_type (qw( before after around ))
202 {
203 18 MooseX::RoleQR::Trait::Application::ToClass->apply_deferred_method_modifiers(
204 $modifier_type,
205 $role,
206 $class,
207 );
208 }
209 }
210 }
211 else
212 {
213 1 push @{$ARGH{$class->name}}, map { $_->name } @{ $meta->get_roles };
214 ### Commenting the stuff below out helped pass t/03classattribute.t.
215 ### Not completely sure why. :-(
216 ###
217 # Moose::Util::MetaRole::apply_metaroles(
218 # for => $class->name,
219 # role_metaroles => \%ROLE_METAROLES,
220 # );
221 # bless(
222 # $class,
223 # Moose::Util::with_traits(
224 # ref($class),
225 # 'MooseX::RoleQR::Trait::Role',
226 # ),
227 # );
228 }
229 2 };
230 };
231  
232 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
233 package MooseX::RoleQR::Trait::Application::ToClass;
234 2 ▶ no thanks;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 2 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 2 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
239 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
240 2 our $AUTHORITY = 'cpan:TOBYINK';
241 2 our $VERSION = '0.004';
242 };
243  
244 2 ▶ before apply => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
245 2 my ($self, $role, $class) = @_;
246 2 };
247  
248 # after apply_override_method_modifiers => sub {
249 # my ($self, $role, $class) = @_;
250 # my $modifier_type = 'override';
251 #
252 # my $add = "add_${modifier_type}_method_modifier";
253 # my $get = "get_deferred_${modifier_type}_method_modifiers";
254 #
255 # my @roles = ($role, map { $_->meta } @{$ARGH{$role->name} || []});
256 #
257 # METHOD: for my $method ( $class->get_all_method_names )
258 # {
259 # ROLE: for my $r (@roles)
260 # {
261 # next ROLE unless $r->can($get);
262 # MODIFIER: for ($r->$get($method))
263 # {
264 # $class->$add($method, $_->body);
265 # }
266 # }
267 # }
268 # };
269
270 6 ▶ after apply_method_modifiers => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 6 my ($self, $modifier_type, $role, $class) = @_;
272 6 $self->apply_deferred_method_modifiers(
273 $modifier_type,
274 $role,
275 $class,
276 );
277 2 };
278
279 sub apply_deferred_method_modifiers
280 24 ▶ {
Subroutine apply_deferred_method_modifiers: called CC=6 SCAR=17.9
Pod undocumented
281 24 my ($self, $modifier_type, $role, $class) = @_;
282 24 my $add = "add_${modifier_type}_method_modifier";
283 24 my $get = "get_deferred_${modifier_type}_method_modifiers";
284
285 24 ▶ my @roles = ($role, map { $_->meta } @{$ARGH{$role->name} || []});
Condition100%2/2
$ARGH{$role->name} || []
l!l
618
MC/DC100%1/1
$ARGH{$role->name} || []
$ARGH{$role->name}
Truth table100%2/2
A
A= $ARGH{$role->name}
Aresult
11
00
286
287 24 METHOD: for my $method ( $class->get_all_method_names )
288 {
289 411 ROLE: for my $r (@roles)
290 {
291 636 ▶ next ROLE unless $r->can($get);
Branch: 1 branch
BranchTrueFalseTotal
unless $r->can($get) 162 474 636
292 474 MODIFIER: for ($r->$get($method, \@_))
293 {
294 # warn "@{[$role->name]} modifying @{[$class->name]} method $method";
295 11 $class->$add($method, $_->body);
296 }
297 }
298 }
299 }
300 };
301  
302 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
303 package MooseX::RoleQR::Trait::Application::ToRole;
304 2 ▶ no thanks;
Subroutine BEGIN: called CC=1 SCAR=0.0
305 2 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
306 2 ▶ use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 2 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
309 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
310 2 our $AUTHORITY = 'cpan:TOBYINK';
311 2 our $VERSION = '0.004';
312 };
313  
314 1 ▶ before apply => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
315 1 my ($self, $role1, $role2) = @_;
316 1 push @{$ARGH{$role2->name}}, $role1->name;
317 1 Moose::Util::MetaRole::apply_metaroles(
318 for => $role2->name,
319 role_metaroles => \%ROLE_METAROLES,
320 );
321 1 bless(
322 $role2,
323 Moose::Util::with_traits(
324 ref($role2),
325 'MooseX::RoleQR::Trait::Role',
326 ),
327 );
328 2 };
329 };
330  
331 2 1;
332  
333 __END__
↑ Summary