blib/lib/Jojo/Role.pm

filter: Statementstmt 100% 91 / 91 Branchbran 58.3% 7 / 12 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 95.6% 22 / 23 Podpod 75.0% 3 / 4 totaltotal 88.8% 128 / 144 SCAR 26.5
CC
14
Cov
92%
make_role
6.3
import
4.0
apply_roles_to_package
2.0
CRAP 14.1
↑ Summary Build »
1  
2 package Jojo::Role;
3 17 $Jojo::Role::VERSION = '0.5.0';
4 # ABSTRACT: Role::Tiny + lexical "with"
5 17 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use feature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use experimental ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 BEGIN {
13 17 ▶ require Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 Role::Tiny->VERSION('2.000006');
15 17 our @ISA = qw(Role::Tiny);
16 }
17  
18 17 ▶use Sub::Inject 0.3.0 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Aliasing of Role::Tiny symbols
21 BEGIN {
22 17 ▶ *INFO = \%Role::Tiny::INFO;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 *APPLIED_TO = \%Role::Tiny::APPLIED_TO;
24 17 *COMPOSED = \%Role::Tiny::COMPOSED;
25 17 *COMPOSITE_INFO = \%Role::Tiny::COMPOSITE_INFO;
26 17 *ON_ROLE_CREATE = \@Role::Tiny::ON_ROLE_CREATE;
27  
28 17 *_getstash = \&Role::Tiny::_getstash;
29 }
30  
31 17 our %INFO;
32 17 our %APPLIED_TO;
33 17 our %COMPOSED;
34 17 our %COMPOSITE_INFO;
35 17 our @ON_ROLE_CREATE;
36  
37 17 our %EXPORT_TAGS;
38 17 our %EXPORT_GEN;
39  
40  
41 # Jojo::Role->apply_roles_to_package('Some::Package', qw(Some::Role +Other::Role));
42 sub apply_roles_to_package {
43 79 ▶ my ($self, $target) = (shift, shift);
Subroutine apply_roles_to_package: called CC=2 SCAR=7.1
Pod documented
44 return $self->Role::Tiny::apply_roles_to_package($target,
45 79 ▶ map { /^\+(.+)$/ ? "${target}::Role::$1" : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)$/u ? : 0 90 90
46 }
47  
48 # Jojo::Role->create_class_with_roles('Some::Base', qw(Some::Role1 +Role2));
49 sub create_class_with_roles {
50 41 ▶ my ($self, $target) = (shift, shift);
Subroutine create_class_with_roles: called CC=2 SCAR=7.1
Pod documented
51 return $self->Role::Tiny::create_class_with_roles($target,
52 41 ▶ map { /^\+(.+)$/ ? "${target}::Role::$1" : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)$/u ? : 0 46 46
53 }
54  
55 sub import {
56 74 ▶ my $target = caller;
Subroutine import: called CC=4 SCAR=13.9
57 74 my $me = shift;
58  
59 # Jojo modules are strict!
60 74 $_->import for qw(strict warnings utf8);
61 74 feature->import(':5.18');
62 74 experimental->import('lexical_subs');
63  
64 74 my $flag = shift;
65 74 ▶ if (!$flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 49 25 74
66 49 $me->make_role($target);
67 49 $flag = '-role';
68 }
69  
70 74 ▶ my @exports = @{$EXPORT_TAGS{$flag} // []};
Condition50%1/2
$Jojo::Role::EXPORT_TAGS{$flag} // []
l!l
740
MC/DC0%0/1
$Jojo::Role::EXPORT_TAGS{$flag} // []
$Jojo::Role::EXPORT_TAGS{$flag}
Truth table50%1/2
A
A= $Jojo::Role::EXPORT_TAGS{$flag}
Aresult
11
00
71 74 @_ = $me->_generate_subs($target, @exports);
72 74 goto &Sub::Inject::sub_inject;
73 }
74  
75 74 ▶sub role_provider { $_[0] }
Subroutine role_provider: called CC=1 SCAR=0.0
Pod undocumented
76  
77 sub make_role {
78 50 ▶ my ($me, $target) = @_;
Subroutine make_role: called CC=6 SCAR=18.4
Pod documented
79 50 ▶ return if $me->is_role($target); # already exported into this package
Branch: 1 branch
BranchTrueFalseTotal
if $me->is_role($target) 0 50 50
80 50 $INFO{$target}{is_role} = 1;
81  
82 # get symbol table reference
83 50 my $stash = _getstash($target);
84  
85 # grab all *non-constant* (stash slot is not a scalarref) subs present
86 # in the symbol table and store their refaddrs (no need to forcibly
87 # inflate constant subs into real subs) with a map to the coderefs in
88 # case of copying or re-use
89 my @not_methods
90 50 ▶ = map +(ref $_ eq 'CODE' ? $_ : ref $_ ? () : *$_{CODE} || ()),
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 0 51 51
ref $_ ? : 0 51 51
Condition66%2/3
*$_{"CODE"} or ()
l!l&&r!l&&!r
1500
MC/DC0%0/2
*$_{"CODE"} or ()
*$_{"CODE"} ()
Truth table66%2/3
A or B
A= *$_{"CODE"}B= ()
ABresult
1X1
011
000
91 values %$stash;
92 50 @{$INFO{$target}{not_methods} = {}}{@not_methods} = @not_methods;
93  
94 # a role does itself
95 50 $APPLIED_TO{$target} = {$target => undef};
96 50 foreach my $hook (@ON_ROLE_CREATE) {
97 2 $hook->($target);
98 }
99 50 return;
100 }
101  
102 BEGIN {
103 17 ▶ %EXPORT_TAGS = ( #
Subroutine BEGIN: called CC=2 SCAR=6.9
104 -role => [qw(after around before requires with)],
105 -with => [qw(with)],
106 );
107  
108 %EXPORT_GEN = (
109 requires => sub {
110 49 ▶ my (undef, $target) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
111 return sub {
112 11 ▶ push @{$INFO{$target}{requires} ||= []}, @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$Jojo::Role::INFO{$target}{"requires"} ||= []
l!l
011
MC/DC0%0/1
$Jojo::Role::INFO{$target}{"requires"} ||= []
$Jojo::Role::INFO{$target}{"requires"}
Truth table50%1/2
A
A= $Jojo::Role::INFO{$target}{"requires"}
Aresult
11
00
113 11 return;
114 49 };
115 },
116 with => sub {
117 74 ▶ my ($me, $target) = (shift->role_provider, shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
118 return sub {
119 33 ▶ $me->apply_roles_to_package($target, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
120 27 return;
121 74 };
122 },
123 17 );
124  
125 # before/after/around
126 17 foreach my $type (qw(before after around)) {
127 $EXPORT_GEN{$type} = sub {
128 147 ▶ my (undef, $target) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
129 return sub {
130 2 ▶ push @{$INFO{$target}{modifiers} ||= []}, [$type => @_];
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$Jojo::Role::INFO{$target}{"modifiers"} ||= []
l!l
02
MC/DC0%0/1
$Jojo::Role::INFO{$target}{"modifiers"} ||= []
$Jojo::Role::INFO{$target}{"modifiers"}
Truth table50%1/2
A
A= $Jojo::Role::INFO{$target}{"modifiers"}
Aresult
11
00
131 2 return;
132 147 };
133 51 ▶ };
Subroutine BEGIN: not called CC=1 SCAR=0.0
134 }
135 }
136  
137 sub _generate_subs {
138 74 ▶ my ($class, $target) = (shift, shift);
Subroutine _generate_subs: called CC=1 SCAR=0.0
139 74 return map { my $cb = $EXPORT_GEN{$_}; $_ => $class->$cb($target) } @_;
140 }
141  
142 17 1;
143  
144 #pod =encoding utf8
145 #pod
146 #pod =head1 SYNOPSIS
147 #pod
148 #pod package Some::Role {
149 #pod use Jojo::Role; # requires perl 5.18+
150 #pod
151 #pod sub foo {...}
152 #pod sub bar {...}
153 #pod around baz => sub {...};
154 #pod }
155 #pod
156 #pod package Some::Class {
157 #pod use Jojo::Role -with;
158 #pod with 'Some::Role';
159 #pod
160 #pod # bar gets imported, but not foo
161 #pod sub foo {...}
162 #pod
163 #pod # baz is wrapped in the around modifier by Class::Method::Modifiers
164 #pod sub baz {...}
165 #pod }
166 #pod
167 #pod =head1 DESCRIPTION
168 #pod
169 #pod L<Jojo::Role> works kind of like L<Role::Tiny> but C<with>, C<requires>,
170 #pod C<before>, C<after> and C<around> are exported
171 #pod as lexical subroutines.
172 #pod
173 #pod This is a companion to L<Jojo::Base>.
174 #pod
175 #pod L<Jojo::Role> may be used in two ways. First, to declare a role, which is done
176 #pod with
177 #pod
178 #pod use Jojo::Base;
179 #pod use Jojo::Base -role; # Longer version
180 #pod
181 #pod Second, to compose one or more roles into a class, via
182 #pod
183 #pod use Jojo::Base -with;
184 #pod
185 #pod =head1 IMPORTED SUBROUTINES: TAG C<-role>
186 #pod
187 #pod The C<-role> tag exports the following subroutines into the caller.
188 #pod
189 #pod =head2 after
190 #pod
191 #pod after foo => sub { ... };
192 #pod
193 #pod Declares an
194 #pod L<< "after" | Class::Method::Modifiers/after method(s) => sub { ... } >>
195 #pod modifier to be applied to the named method at composition time.
196 #pod
197 #pod =head2 around
198 #pod
199 #pod around => sub { ... };
200 #pod
201 #pod Declares an
202 #pod L<< "around" | Class::Method::Modifiers/around method(s) => sub { ... } >>
203 #pod modifier to be applied to the named method at composition time.
204 #pod
205 #pod =head2 before
206 #pod
207 #pod before => sub { ... };
208 #pod
209 #pod Declares a
210 #pod L<< "before" | Class::Method::Modifiers/before method(s) => sub { ... } >>
211 #pod modifier to be applied to the named method at composition time.
212 #pod
213 #pod =head2 requires
214 #pod
215 #pod requires qw(foo bar);
216 #pod
217 #pod Declares a list of methods that must be defined to compose the role.
218 #pod
219 #pod =head2 with
220 #pod
221 #pod with 'Some::Role';
222 #pod
223 #pod with 'Some::Role1', 'Some::Role2';
224 #pod
225 #pod Composes one or more roles into the current role.
226 #pod
227 #pod =head1 IMPORTED SUBROUTINES: TAG C<-with>
228 #pod
229 #pod The C<-with> tag exports the following subroutine into the caller.
230 #pod It is equivalent to using L<Role::Tiny::With>.
231 #pod
232 #pod =head2 with
233 #pod
234 #pod with 'Some::Role1', 'Some::Role2';
235 #pod
236 #pod Composes one or more roles into the current class.
237 #pod
238 #pod =head1 METHODS
239 #pod
240 #pod L<Jojo::Role> inherits all methods from L<Role::Tiny> and implements the
241 #pod following new ones.
242 #pod
243 #pod =head2 apply_roles_to_package
244 #pod
245 #pod Jojo::Role->apply_roles_to_package('Some::Package', qw(Some::Role +Other::Role));
246 #pod
247 #pod =head2 create_class_with_roles
248 #pod
249 #pod Jojo::Role->create_class_with_roles('Some::Base', qw(Some::Role1 +Role2));
250 #pod
251 #pod =head2 import
252 #pod
253 #pod Jojo::Role->import();
254 #pod Jojo::Role->import(-role);
255 #pod Jojo::Role->import(-with);
256 #pod
257 #pod =head2 make_role
258 #pod
259 #pod Role::Tiny->make_role('Some::Package');
260 #pod
261 #pod Promotes a given package to a role.
262 #pod No subroutines are imported into C<'Some::Package'>.
263 #pod
264 #pod =head1 CAVEATS
265 #pod
266 #pod =over 4
267 #pod
268 #pod =item *
269 #pod
270 #pod L<Jojo::Role> requires perl 5.18 or newer
271 #pod
272 #pod =item *
273 #pod
274 #pod Because a lexical sub does not behave like a package import,
275 #pod some code may need to be enclosed in blocks to avoid warnings like
276 #pod
277 #pod "state" subroutine &with masks earlier declaration in same scope at...
278 #pod
279 #pod =back
280 #pod
281 #pod =head1 SEE ALSO
282 #pod
283 #pod L<Role::Tiny>, L<Jojo::Base>.
284 #pod
285 #pod =head1 ACKNOWLEDGMENTS
286 #pod
287 #pod Thanks to the authors of L<Role::Tiny>, which hold
288 #pod the copyright over the original code.
289 #pod
290 #pod =cut
291  
292 __END__
↑ Summary Build »