TaggedAttributes.pm

filter: Statementstmt 88.1% 112 / 127 Branchbran 68.7% 33 / 48 Conditioncond 66.6% 18 / 27 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 87.5% 21 / 24 Podpod 100% 4 / 4 totaltotal 79.1% 193 / 244 CC 36 SCAR 21.6
CC
36
Cov
81%
install_tags
16.5
role_import
14.5
import
11.4
CRAP 8.7
↑ Summary TaggedAttributes/Cache.pm »
1 package MooX::TaggedAttributes;
2  
3 # ABSTRACT: Add a tag with an arbitrary value to a an attribute
4  
5 7 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our $VERSION = '0.18';
11  
12 7 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Sub::Name ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Role::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 ▶use Moo::_Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use MooX::TaggedAttributes::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 our %TAGSTORE;
22 7 our %TAGCACHE;
23 7 our %TAGHANDLER;
24  
25 # when using -propagate, make sure we don't duplicate role application.
26 # can't use simply role checks because of inheritance
27 7 our %APPLIED_ROLE;
28  
29 7 my %ARGS = ( -tags => 1, -handler => 1, -propagate => undef );
30  
31 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
32 0 require Carp;
33 0 goto \&Carp::croak;
34 }
35  
36 65 ▶sub _install_on_application {
Subroutine _install_on_application: called CC=2 SCAR=6.9
37 65 my ( $target ) = @_;
38  
39 return
40 65 ▶ if Moo::Role::does_role( $target, 'MooX::TaggedAttributes::Propagate' );
Branch: 1 branch
BranchTrueFalseTotal
if Moo::Role::does_role($target, "MooX::TaggedAttributes::Propagate") 57 8 65
41  
42 8 Moo::Role->apply_roles_to_package( $target,
43 'MooX::TaggedAttributes::Role' );
44 8 Moo::Role->apply_roles_to_package( $target,
45 'MooX::TaggedAttributes::Propagate' );
46  
47 Role::Hooks->after_apply(
48 $target,
49 62 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
50 62 my ( $role, $ltarget ) = @_;
51  
52 # Multiple instances of this hook may get installed
53 # through application of multiple tag roles to a class
54 # but, we don't want to repeat installation of tags or
55 # another instance of this hook. (the latter
56 # this is guarded against in _install_on_application
57 # but this'll take care of it as a sideeffect)
58  
59 62 ▶ return if $APPLIED_ROLE{$ltarget}{$role}++;
Branch: 1 branch
BranchTrueFalseTotal
if $MooX::TaggedAttributes::APPLIED_ROLE{$ltarget}{$role}++ 5 57 62
60  
61 # this is guarded in _install_on_application so that
62 # the modifier is done only once for the target
63 57 _install_on_application( $ltarget );
64  
65  
66 57 role_import( $role, $ltarget );
67 8 } );
68  
69 }
70  
71 18 ▶sub import {
Subroutine import: called CC=11 SCAR=24.3
72  
73 18 my ( $class, @args ) = @_;
74 18 my $target = caller;
75  
76 18 my %args;
77  
78 18 ▶ while ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 30 18 48
79 30 my $arg = shift @args;
80 _croak( "unknown argument to ", __PACKAGE__, ": $arg" )
81 30 ▶ unless exists $ARGS{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ARGS{$arg} 0 30 30
82 30 ▶ $args{$arg} = defined $ARGS{$arg} ? shift @args : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $ARGS{$arg} ? : 22 8 30
83 }
84  
85 18 ▶ Moo::Role->apply_roles_to_package( $target, 'MooX::TaggedAttributes::Role' )
Branch: 1 branch
BranchTrueFalseTotal
unless Moo::Role::does_role($target, "MooX::TaggedAttributes::Role") 18 0 18
86 unless Moo::Role::does_role( $target, 'MooX::TaggedAttributes::Role' );
87  
88 18 ▶ return unless %args;
Branch: 1 branch
BranchTrueFalseTotal
unless %args 0 18 18
89  
90 18 ▶ if ( defined $args{-tags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"-tags"}) 18 0 18
91 $args{-tags} = [ $args{-tags} ]
92 18 ▶ unless 'ARRAY' eq ref $args{-tags};
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $args{"-tags"} 4 14 18
93  
94 18 $args{-class} = $class;
95 install_tags( $target, %args )
96 18 ▶ if @{ $args{-tags} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$args{"-tags"};} 18 0 18
97 }
98  
99 18 ▶ if ( defined $args{-propagate} && $args{-propagate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"-propagate"} and $args{"-propagate"}) 8 10 18
Condition66%2/3
defined $args{"-propagate"} and $args{"-propagate"}
!ll&&!rl&&r
1008
MC/DC50%1/2
defined $args{"-propagate"} and $args{"-propagate"}
defined $args{"-propagate"} $args{"-propagate"}
Truth table66%2/3
A and B
A= defined $args{"-propagate"}B= $args{"-propagate"}
ABresult
0X0
100
111
100 8 _install_on_application( $target );
101 }
102  
103 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
104 18 *${ \"${target}::import" } = \&role_import;
105 }
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131 97 ▶sub role_import {
Subroutine role_import: called CC=9 SCAR=26.8
Pod documented
132 97 my $role = shift;
133 97 ▶ return unless Moo::Role->is_role( $role );
Branch: 1 branch
BranchTrueFalseTotal
unless "Moo::Role"->is_role($role) 0 97 97
134 97 ▶ my $target = shift // caller;
Condition66%2/3
shift() // caller
l!l&&r!l&&!r
57400
MC/DC0%0/2
shift() // caller
shift() caller
Truth table66%2/3
A // B
A= shift()B= caller
ABresult
1X1
011
000
135  
136 97 ▶ unless ( Moo::Role::does_role( $target, $role ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Moo::Role::does_role($target, $role)) 40 57 97
137  
138 40 ▶ if ( Moo::Role->is_role( $target ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Moo::Role'->is_role($target)) { } 13 27 40
139 13 Moo::Role->apply_roles_to_package( $target, $role );
140 }
141 else {
142 # Prevent installation of the import routine from a tagged role
143 # into the consumer. Roles won't overwrite an existing method,
144 # so create one which goes away when this block exits.
145  
146 # localized globs don't seem work on 5.10.1, result in an error
147 # Attempt to free unreferenced scalar: SV 0x564fc668eb60
148 # at [...]MooX/TaggedAttributes.pm line 147.
149  
150 27 ▶ if ( $^V lt v5.14 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^V lt v5.14) { } 0 27 27
151 0 require Package::Stash;
152 0 my $pkg = Package::Stash->new( $target );
153 0 ▶ if ( $pkg->has_symbol( '&import' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->has_symbol('&import')) { } 0 0 0
154 0 Moo::Role->apply_roles_to_package( $target, $role );
155 }
156 else {
157 0 ▶ $pkg->add_symbol( '&import', sub { } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 eval {
159 0 Moo::Role->apply_roles_to_package( $target, $role );
160 };
161 0 my $e = $@;
162 0 $pkg->remove_symbol( '&import' );
163 0 ▶ die $e if $e ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $e ne "" 0 0 0
164 }
165 }
166 else {
167 7 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
168 27 my $glob = *${ \"${target}::import" };
169 27 !defined *{$glob}{CODE}
170 27 ▶ and local *{$glob} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
defined *{$glob;}{"CODE"} or local *{$glob;} = sub { } 0 27 27
171 27 Moo::Role->apply_roles_to_package( $target, $role );
172 }
173 }
174 }
175 97 install_tags( $target, -class => $role );
176 }
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194 115 ▶sub install_tags {
Subroutine install_tags: called CC=13 SCAR=28.0
Pod documented
195 115 my ( $target, %opt ) = @_;
196  
197 my $tags = $opt{-tags}
198 // ( defined( $opt{-class} ) && $TAGSTORE{ $opt{-class} } )
199 115 ▶ || do {
Condition44%4/9
($opt{'-tags'} // defined $opt{'-class'} && $MooX::TaggedAttributes::TAGSTORE{$opt{'-class'}}) || do { my $class = $opt{'-class'}; _croak('-tags or -class not specified') unless defined $class; _croak("Class '${class}' has not yet been registered") }
l!l&&r!l&&!r
11500
$opt{"-tags"} // defined $opt{"-class"} && $MooX::TaggedAttributes::TAGSTORE{$opt{"-class"}}
l!l&&r!l&&!r
18970
defined $opt{"-class"} and $MooX::TaggedAttributes::TAGSTORE{$opt{"-class"}}
!ll&&!rl&&r
0097
MC/DC0%0/4
($opt{'-tags'} // defined $opt{'-class'} && $MooX::TaggedAttributes::TAGSTORE{$opt{'-class'}}) || do { my $class = $opt{'-class'}; _croak('-tags or -class not specified') unless defined $class; _croak("Class '${class}' has not yet been registered") }
$opt{"-tags"} defined $opt{"-class"} $MooX::TaggedAttributes::TAGSTORE{$opt{"-class"}} do { my $class = $opt{'-class'}; _croak('-tags or -class not specified') unless defined $class; _croak("Class '${class}' has not yet been registered") }
Truth table33%2/6
A // B and C || D
A= $opt{"-tags"}B= defined $opt{"-class"}C= $MooX::TaggedAttributes::TAGSTORE{$opt{"-class"}}D= do { my $class = $opt{'-class'}; _croak('-tags or -class not specified') unless defined $class; _croak("Class '${class}' has not yet been registered") }
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
200 0 my $class = $opt{-class};
201 0 ▶ _croak( "-tags or -class not specified" ) if !defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
202 0 _croak( "Class '$class' has not yet been registered" );
203 };
204  
205 # first time importing a tag role, install our tag handler
206 install_tag_handler( $target, \&make_tag_handler )
207 115 ▶ if !exists $TAGSTORE{$target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $MooX::TaggedAttributes::TAGSTORE{$target} 88 27 115
208  
209 # add the tags.
210 115 ▶ push @{ $TAGSTORE{$target} //= [] }, @$tags;
Condition100%2/2
$MooX::TaggedAttributes::TAGSTORE{$target} //= []
l!l
2788
MC/DC100%1/1
$MooX::TaggedAttributes::TAGSTORE{$target} //= []
$MooX::TaggedAttributes::TAGSTORE{$target}
Truth table100%2/2
A
A= $MooX::TaggedAttributes::TAGSTORE{$target}
Aresult
11
00
211  
212 # if an extra handler has been specified, or the tag role class
213 # $opt{-class} has one install that as well.
214 115 ▶ if ( my $handler = $opt{-handler}
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $opt{"-handler"} // defined $opt{"-class"} && $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}) 22 93 115
Condition83%5/6
$opt{"-handler"} // defined $opt{"-class"} && $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}
l!l&&r!l&&!r
41893
defined $opt{"-class"} and $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}
!ll&&!rl&&r
09318
MC/DC66%2/3
$opt{"-handler"} // defined $opt{"-class"} && $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}
$opt{"-handler"} defined $opt{"-class"} $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}
Truth table75%3/4
A // B and C
A= $opt{"-handler"}B= defined $opt{"-class"}C= $MooX::TaggedAttributes::TAGHANDLER{$opt{"-class"}}
ABCresult
1XX1
0111
00X0
0100
215 // ( defined $opt{-class} && $TAGHANDLER{ $opt{-class} } ) )
216 {
217 22 ▶ my @handlers = 'ARRAY' eq ref $handler ? @$handler : $handler;
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $handler ? : 18 4 22
218 22 install_tag_handler( $target, $_ ) for @handlers;
219 22 ▶ push @{ $TAGHANDLER{$target} //= [] }, @handlers;
Condition100%2/2
$MooX::TaggedAttributes::TAGHANDLER{$target} //= []
l!l
418
MC/DC100%1/1
$MooX::TaggedAttributes::TAGHANDLER{$target} //= []
$MooX::TaggedAttributes::TAGHANDLER{$target}
Truth table100%2/2
A
A= $MooX::TaggedAttributes::TAGHANDLER{$target}
Aresult
11
00
220 }
221 }
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233 116 ▶sub install_tag_handler {
Subroutine install_tag_handler: called CC=1 SCAR=0.0
Pod documented
234 116 my ( $target, $handler ) = @_;
235 116 Moo::_Utils::_install_modifier( $target,
236 around => has => $handler->( $target ) );
237 }
238  
239  
240  
241  
242  
243  
244  
245  
246  
247 51 ▶sub _install_role_modifier {
Subroutine _install_role_modifier: called CC=2 SCAR=7.0
248 51 my $target = shift;
249 51 ▶ push @{ $Moo::Role::INFO{$target}{modifiers} ||= [] }, [@_];
Condition50%1/2
$Moo::Role::INFO{$target}{"modifiers"} ||= []
l!l
510
MC/DC0%0/1
$Moo::Role::INFO{$target}{"modifiers"} ||= []
$Moo::Role::INFO{$target}{"modifiers"}
Truth table50%1/2
A
A= $Moo::Role::INFO{$target}{"modifiers"}
Aresult
11
00
250 51 Moo::Role->_maybe_reset_handlemoose( $target );
251 }
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263 88 ▶sub make_tag_handler {
Subroutine make_tag_handler: called CC=1 SCAR=0.0
Pod documented
264  
265 # we need to
266 # 1) use the target package's around() function, and
267 # 2) call it in that package's context.
268  
269 # create a closure which knows about the target's around
270 # so that if namespace::clean is called on the target class
271 # we don't lose access to it.
272  
273 88 my $target = shift;
274 88 my $is_role = Moo::Role->is_role( $target );
275  
276 103 ▶ return Sub::Name::subname "${target}::tag_handler" => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
277  
278 103 my ( $orig, $attrs, %opt ) = @_;
279 103 $orig->( $attrs, %opt );
280  
281 103 ▶ $attrs = ref $attrs ? $attrs : [$attrs];
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs ? : 0 103 103
282 103 my @tags = @{ $TAGSTORE{$target} };
283  
284 my @args = (
285 $target,
286 394 ▶ around => "_tag_list" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
287 394 my $orig = shift;
288 ## no critic (ProhibitAccessOfPrivateData)
289 my @ret = (
290 394 @{&$orig},
291 352 map { [ $_, $attrs, $opt{$_} ] }
292 394 grep { exists $opt{$_} } @tags,
293 );
294 394 return \@ret;
295 103 } );
296  
297 103 ▶ $is_role
Branch: 1 branch
BranchTrueFalseTotal
$is_role ? : 51 52 103
298 ? _install_role_modifier( @args )
299 : Moo::_Utils::_install_modifier( @args );
300 }
301 88 }
302  
303  
304 7 1;
305  
306 #
307 # This file is part of MooX-TaggedAttributes
308 #
309 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
310 #
311 # This is free software, licensed under:
312 #
313 # The GNU General Public License, Version 3, June 2007
314 #
315  
316 __END__
↑ Summary TaggedAttributes/Cache.pm »