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 66.6% 2 / 3 totaltotal 88.8% 127 / 143 SCAR 26.5
CC
14
Cov
92%
_become_role
6.3
import
4.0
apply_roles_to_package
2.0
CRAP 14.1
↑ Summary Build »
1  
2 package Jojo::Role;
3 16 $Jojo::Role::VERSION = '0.4.0';
4 # ABSTRACT: Role::Tiny + lexical "with"
5 16 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use feature ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use experimental ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 BEGIN {
13 16 ▶ require Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 Role::Tiny->VERSION('2.000005');
15 16 our @ISA = qw(Role::Tiny);
16 }
17  
18 16 ▶use Sub::Inject 0.3.0 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Aliasing of Role::Tiny symbols
21 BEGIN {
22 16 ▶ *INFO = \%Role::Tiny::INFO;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 *APPLIED_TO = \%Role::Tiny::APPLIED_TO;
24 16 *COMPOSED = \%Role::Tiny::COMPOSED;
25 16 *COMPOSITE_INFO = \%Role::Tiny::COMPOSITE_INFO;
26 16 *ON_ROLE_CREATE = \@Role::Tiny::ON_ROLE_CREATE;
27  
28 16 *_getstash = \&Role::Tiny::_getstash;
29 }
30  
31 16 our %INFO;
32 16 our %APPLIED_TO;
33 16 our %COMPOSED;
34 16 our %COMPOSITE_INFO;
35 16 our @ON_ROLE_CREATE;
36  
37 16 our %EXPORT_TAGS;
38 16 our %EXPORT_GEN;
39  
40 # Jojo::Role->apply_roles_to_package('Some::Package', qw(Some::Role +Other::Role));
41 sub apply_roles_to_package {
42 78 ▶ my ($self, $target) = (shift, shift);
Subroutine apply_roles_to_package: called CC=2 SCAR=7.1
Pod documented
43 return $self->Role::Tiny::apply_roles_to_package($target,
44 78 ▶ map { /^\+(.+)$/ ? "${target}::Role::$1" : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)$/u ? : 0 89 89
45 }
46  
47 # Jojo::Role->create_class_with_roles('Some::Base', qw(Some::Role1 +Role2));
48 sub create_class_with_roles {
49 41 ▶ my ($self, $target) = (shift, shift);
Subroutine create_class_with_roles: called CC=2 SCAR=7.1
Pod documented
50 return $self->Role::Tiny::create_class_with_roles($target,
51 41 ▶ map { /^\+(.+)$/ ? "${target}::Role::$1" : $_ } @_);
Branch: 1 branch
BranchTrueFalseTotal
/^\+(.+)$/u ? : 0 46 46
52 }
53  
54 sub import {
55 74 ▶ my $target = caller;
Subroutine import: called CC=4 SCAR=13.9
56 74 my $me = shift;
57  
58 # Jojo modules are strict!
59 74 $_->import for qw(strict warnings utf8);
60 74 feature->import(':5.18');
61 74 experimental->import('lexical_subs');
62  
63 74 my $flag = shift;
64 74 ▶ if (!$flag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 49 25 74
65 49 $me->_become_role($target);
66 49 $flag = '-role';
67 }
68  
69 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
70 74 @_ = $me->_generate_subs($target, @exports);
71 74 goto &Sub::Inject::sub_inject;
72 }
73  
74 33 ▶sub role_provider { $_[0] }
Subroutine role_provider: called CC=1 SCAR=0.0
Pod undocumented
75  
76 sub _become_role {
77 49 ▶ my ($me, $target) = @_;
Subroutine _become_role: called CC=6 SCAR=18.4
78 49 ▶ return if $me->is_role($target); # already exported into this package
Branch: 1 branch
BranchTrueFalseTotal
if $me->is_role($target) 0 49 49
79 49 $INFO{$target}{is_role} = 1;
80  
81 # get symbol table reference
82 49 my $stash = _getstash($target);
83  
84 # grab all *non-constant* (stash slot is not a scalarref) subs present
85 # in the symbol table and store their refaddrs (no need to forcibly
86 # inflate constant subs into real subs) with a map to the coderefs in
87 # case of copying or re-use
88 my @not_methods
89 49 ▶ = map +(ref $_ eq 'CODE' ? $_ : ref $_ ? () : *$_{CODE} || ()),
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 0 50 50
ref $_ ? : 0 50 50
Condition66%2/3
*$_{"CODE"} or ()
l!l&&r!l&&!r
1490
MC/DC0%0/2
*$_{"CODE"} or ()
*$_{"CODE"} ()
Truth table66%2/3
A or B
A= *$_{"CODE"}B= ()
ABresult
1X1
011
000
90 values %$stash;
91 49 @{$INFO{$target}{not_methods} = {}}{@not_methods} = @not_methods;
92  
93 # a role does itself
94 49 $APPLIED_TO{$target} = {$target => undef};
95 49 foreach my $hook (@ON_ROLE_CREATE) {
96 2 $hook->($target);
97 }
98 49 return;
99 }
100  
101 BEGIN {
102 16 ▶ %EXPORT_TAGS = ( #
Subroutine BEGIN: called CC=2 SCAR=6.9
103 -role => [qw(after around before requires with)],
104 -with => [qw(with)],
105 );
106  
107 %EXPORT_GEN = (
108 requires => sub {
109 49 ▶ my (undef, $target) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
110 return sub {
111 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
112 11 return;
113 49 };
114 },
115 with => sub {
116 74 ▶ my ($me, $target) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
117 return sub {
118 33 ▶ $me->role_provider->apply_roles_to_package($target, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
119 27 return;
120 74 };
121 },
122 16 );
123  
124 # before/after/around
125 16 foreach my $type (qw(before after around)) {
126 $EXPORT_GEN{$type} = sub {
127 147 ▶ my (undef, $target) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
128 return sub {
129 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
130 2 return;
131 147 };
132 48 ▶ };
Subroutine BEGIN: not called CC=1 SCAR=0.0
133 }
134 }
135  
136 sub _generate_subs {
137 74 ▶ my ($class, $target) = (shift, shift);
Subroutine _generate_subs: called CC=1 SCAR=0.0
138 74 return map { my $cb = $EXPORT_GEN{$_}; $_ => $class->$cb($target) } @_;
139 }
140  
141 16 1;
142  
143 #pod =encoding utf8
144 #pod
145 #pod =head1 SYNOPSIS
146 #pod
147 #pod package Some::Role {
148 #pod use Jojo::Role; # requires perl 5.18+
149 #pod
150 #pod sub foo {...}
151 #pod sub bar {...}
152 #pod around baz => sub {...};
153 #pod }
154 #pod
155 #pod package Some::Class {
156 #pod use Jojo::Role -with;
157 #pod with 'Some::Role';
158 #pod
159 #pod # bar gets imported, but not foo
160 #pod sub foo {...}
161 #pod
162 #pod # baz is wrapped in the around modifier by Class::Method::Modifiers
163 #pod sub baz {...}
164 #pod }
165 #pod
166 #pod =head1 DESCRIPTION
167 #pod
168 #pod L<Jojo::Role> works kind of like L<Role::Tiny> but C<with>, C<require>,
169 #pod C<before>, C<after> and C<around> are imported
170 #pod as lexical subroutines.
171 #pod
172 #pod This is a companion to L<Jojo::Base>.
173 #pod
174 #pod =head1 CAVEATS
175 #pod
176 #pod =over 4
177 #pod
178 #pod =item *
179 #pod
180 #pod L<Jojo::Role> requires perl 5.18 or newer
181 #pod
182 #pod =item *
183 #pod
184 #pod Because a lexical sub does not behave like a package import,
185 #pod some code may need to be enclosed in blocks to avoid warnings like
186 #pod
187 #pod "state" subroutine &has masks earlier declaration in same scope at...
188 #pod
189 #pod =back
190 #pod
191 #pod =head1 SEE ALSO
192 #pod
193 #pod L<Role::Tiny>, L<Jojo::Base>.
194 #pod
195 #pod =cut
196  
197 __END__
↑ Summary Build »