PluginKit/Consumer.pm

filter: Statementstmt 82.1% 120 / 146 Branchbran 66.0% 37 / 56 Conditioncond 66.6% 16 / 24 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 84.0% 21 / 25 Podpod 100% 3 / 3 totaltotal 74.8% 202 / 270 CC 37 SCAR 19.3
CC
37
Cov
77%
_normalize_class_builder
14.4
has_pluggable_class
12.0
_normalize_args_builder
8.1
CRAP 6.9
↑ Summary PluginKit/Core.pm »
1 package MooX::PluginKit::Consumer;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 our $VERSION = '0.06';
5  
6 =head1 NAME
7  
8 MooX::PluginKit::Consumer - Declare a class as a consumer of
9 PluginKit plugins.
10  
11 =head1 SYNOPSIS
12  
13 package My::Class;
14 use Moo;
15 use MooX::PluginKit::Consumer;
16
17 # Optional, defaults to just 'My::Class'.
18 plugin_namespace 'My::Class::Plugin';
19
20 has_pluggable_object some_object => (
21 class => 'Some::Object',
22 );
23
24 my $object = My::Class->new(
25 plugins => [...],
26 some_object=>{...},
27 );
28  
29 =head1 DESCRIPTION
30  
31 This module, when C<use>d, sets the callers base class to the
32 L<MooX::PluginKit::ConsumerBase> class, applies the
33 L<MooX::PluginKit::ConsumerRole> role to the caller, and
34 exports several candy functions (see L</CANDY>) into the
35 caller.
36  
37 Some higher-level documentation about how to consume plugins can
38 be found at L<MooX::PluginKit/CONSUMING PLUGINS>.
39  
40 =cut
41  
42 3 ▶use MooX::PluginKit::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use MooX::PluginKit::ConsumerRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Types::Common::String -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use Class::Method::Modifiers qw( install_modifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use Exporter qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 3 our @EXPORT = qw(
55 plugin_namespace
56 has_pluggable_object
57 has_pluggable_class
58 );
59  
60 12 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
61 {
62 12 my $caller = (caller())[0];
63 12 init_consumer( $caller );
64 12 get_consumer_moo_extends( $caller )->('MooX::PluginKit::ConsumerBase');
65 12 get_consumer_moo_with( $caller )->('MooX::PluginKit::ConsumerRole');
66 }
67  
68 12 goto &Exporter::import;
69 }
70  
71 =head1 CANDY
72  
73 =head2 plugin_namespace
74  
75 plugin_namespace 'Location::Of::My::Plugins';
76  
77 When the L<MooX::PluginKit::ConsumerRole/plugins> argument is set
78 the user may choose to pass relative plugins. Setting this namespace
79 changes the default root namespace used to resolve these relative
80 plugin names to absolute ones.
81  
82 This defaults to the package name of the class which uses this module.
83  
84 Read more about this at L<MooX::PluginKit/Relative Plugin Namespace>.
85  
86 =cut
87  
88 1 ▶sub plugin_namespace {
Subroutine plugin_namespace: called CC=1 SCAR=0.0
Pod documented
89 1 my ($consumer) = caller();
90 1 local $Carp::Internal{ (__PACKAGE__) } = 1;
91 1 set_consumer_namespace( $consumer, @_ );
92 1 return;
93 }
94  
95 =head2 has_pluggable_object
96  
97 has_pluggable_object foo_bar => (
98 class => 'Foo::Bar',
99 );
100  
101 This function acts like L<Moo/has> but adds a bunch of functionality,
102 making it easy to cascade the creation of objects which automatically
103 have applicable plugins applied to them, at run-time.
104  
105 In the above C<foo_bar> example, the user of your class can then specify
106 the C<foo_bar> argument as a hashref. This hashref will be used to
107 create an object of the C<Foo::Bar> class, but not until after any
108 applicable plugins set on the consumer class have been applied to it.
109  
110 Documented below are the L<Moo/has> argument which are supported as well
111 as several custom arguments (like C<class>, above).
112  
113 Note that you MUST set either L</class>, L<class_arg>, or L<class_builder>.
114  
115 Read more about this at L<MooX::PluginKit/Object Attributes>.
116  
117 =head3 Moo Arguments
118  
119 This function only supports a subset of the arguments that L<Moo/has>
120 supports. They are:
121  
122 builder
123 default
124 handles
125 init_arg
126 isa
127 required
128 weak_ref
129  
130 =head3 class
131  
132 Setting this to a class name does two things, 1) it declares the C<isa> on
133 the attributes to validate that the final value is an instance of the class
134 or a subclass of it, and 2) sets L</default_class> to it.
135  
136 =head3 default_class
137  
138 If no class is specified this will be the default class used. A common idiom
139 of using both L</class> and this is:
140  
141 has_pluggable_object foo => (
142 class => 'Foo',
143 default_class => 'Foo::SubClass',
144 );
145  
146 Meaning, in the above example, that the final object may be any subclass of the
147 C<Foo> class, but if no class is specified, it will be constructed from the
148 C<Foo::SubClass> class.
149  
150 =head3 class_arg
151  
152 If the class to be instantiated can be derived from the hashref argument to
153 this attribute then set this to the name of the key in the hashref to get the
154 class from. Setting this to a C<1> is the same as setting it to C<class>. So,
155 these are the same:
156  
157 has_pluggable_object foo => ( class_arg=>1 );
158 has_pluggable_object foo => ( class_arg=>'class' );
159  
160 Then when passing the hashref the class can be declared as part of it:
161  
162 my $thing = YourClass->new( foo=>{ class=>'Foo::Stuff', ... } );
163  
164 =head3 class_builder
165  
166 Set this to a method name or a code ref which will be used to build the
167 class name. This sub will be called as a method and passed the args hashref
168 and is expected to return a class name.
169  
170 If this is set to C<1> then the method name will be automatically generated
171 based on the attribute name. So, these are identical:
172  
173 has_pluggable_object foo => ( class_builder=>1 );
174 has_pluggable_object foo => ( class_builder=>'_foo_build_class' );
175  
176 Then make the sub:
177  
178 sub _foo_build_class { my ($self, $args) = @_; ...; return $class }
179  
180 Note that the class builder will not be called if L</class_arg> if set and
181 the user has specified a class argument.
182  
183 =head3 class_namespace
184  
185 Set this to allow the class to be relative. This way if the class starts with
186 C<::> then this namespace will be automatically prefixed to it.
187  
188 =head3 args_builder
189  
190 Set this to a method name or a code ref which will be used to adjust the
191 hashref arguments before the object is constructed from them. This sub will
192 be called as a method and passed the args hashref and is epxected to return
193 an args hashref.
194  
195 If this is set to C<1> then the method name will be automatically generated
196 based on the attribute name. So, these are identical:
197  
198 has_pluggable_object foo => ( args_builder=>1 );
199 has_pluggable_object foo => ( args_builder=>'_foo_build_args' );
200  
201 Then make the sub:
202  
203 sub _foo_build_args { my ($self, $args) = @_; ...; return $args }
204  
205 =cut
206  
207 9 ▶sub has_pluggable_object {
Subroutine has_pluggable_object: called CC=5 SCAR=16.3
Pod documented
208 9 my ($name, %args) = @_;
209 9 my $consumer_class = (caller())[0];
210 9 local $Carp::Internal{ (__PACKAGE__) } = 1;
211  
212 9 my $has = get_consumer_moo_has( $consumer_class );
213  
214 9 my $class_builder = _normalize_class_builder( $name, $consumer_class, %args );
215 9 my $args_builder = _normalize_args_builder( $name, $consumer_class, %args );
216  
217 9 my $isa = delete $args{isa};
218 9 my $class = delete $args{class};
219  
220 9 ▶ if (!defined $isa) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $isa) 9 0 9
221 9 ▶ $isa = InstanceOf[ $class ] if defined $class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class 4 5 9
222 9 ▶ $isa ||= Object;
Condition66%2/3
$isa ||= &Object()
l!l&&r!l&&!r
450
MC/DC0%0/2
$isa ||= &Object()
$isa &Object()
Truth table66%2/3
A ||= B
A= $isaB= &Object()
ABresult
1X1
011
000
223 }
224  
225 9 my $init_name = "_init_$name";
226 9 my $init_isa = $isa | HashRef;
227  
228 $has->(
229 $init_name,
230 init_arg => $name,
231 is => 'ro',
232 isa => $init_isa,
233 lazy => 1,
234 (
235 0 map { $_ => $args{$_} }
236 9 grep { exists $args{$_} }
237 qw( default builder required weak_ref init_arg )
238 ),
239 );
240  
241 9 my $attr_isa = $isa;
242 9 ▶ $attr_isa = $attr_isa | Undef if !$args{required};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"required"} 9 0 9
243  
244 $has->(
245 $name,
246 init_arg => undef,
247 is => 'lazy',
248 isa => $attr_isa,
249 (
250 0 map { $_ => $args{$_} }
251 9 grep { exists $args{$_} }
252 qw( handles weak_ref )
253 ),
254 builder => _build_attr_builder(
255 $init_name, $args_builder, $class_builder,
256 ),
257 );
258  
259 9 return;
260 }
261  
262 # Avoid circular references by making this anonymous sub into a separate closure.
263 9 ▶sub _build_attr_builder {
Subroutine _build_attr_builder: called CC=1 SCAR=1.2
264 9 my ($init_name, $args_builder, $class_builder) = @_;
265  
266 0 ▶ return sub{
Subroutine __ANON__: not called CC=3 SCAR=15.0
Subroutine _build_foo: called CC=3 SCAR=15.0
267 0 my ($self) = @_;
268  
269 0 my $args = $self->$init_name();
270 0 ▶ return $args if ref($args) ne 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $args ne "HASH" 0 0 0
if ref $args ne "HASH" 0 9 9
271 0 ▶ $args = $self->$args_builder({ %$args }) if defined $args_builder;
Branch: 2 branches
BranchTrueFalseTotal
if defined $args_builder 0 0 0
if defined $args_builder 3 6 9
272  
273 0 my $class = $self->$class_builder( $args );
274  
275 0 return $self->class_new_with_plugins(
276 $class, $args,
277 );
278 9 };
279 }
280  
281 9 ▶sub _normalize_class_builder {
Subroutine _normalize_class_builder: called CC=14 SCAR=26.7
282 9 my ($name, $consumer_class, %args) = @_;
283  
284 9 my $class = delete $args{class};
285 9 my $default_class = delete $args{default_class};
286 9 my $class_arg = delete $args{class_arg};
287 9 my $class_builder = delete $args{class_builder};
288 9 my $class_namespace = delete $args{class_namespace};
289  
290 9 ▶ $default_class = $class if !defined $default_class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_class 9 0 9
291 9 ▶ $class_arg = undef if defined($class_arg) and "$class_arg" eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if defined $class_arg and "$class_arg" eq 0 0 9 9
Condition66%2/3
defined $class_arg and "$class_arg" eq 0
!ll&&!rl&&r
720
MC/DC0%0/2
defined $class_arg and "$class_arg" eq 0
defined $class_arg "$class_arg" eq 0
Truth table66%2/3
A and B
A= defined $class_argB= "$class_arg" eq 0
ABresult
0X0
100
111
292 9 ▶ $class_builder = undef if defined($class_builder) and "$class_builder" eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if defined $class_builder and "$class_builder" eq 0 0 9 9
Condition66%2/3
defined $class_builder and "$class_builder" eq 0
!ll&&!rl&&r
630
MC/DC0%0/2
defined $class_builder and "$class_builder" eq 0
defined $class_builder "$class_builder" eq 0
Truth table66%2/3
A and B
A= defined $class_builderB= "$class_builder" eq 0
ABresult
0X0
100
111
293  
294 9 ▶ $class_arg = 'class' if defined($class_arg) and "$class_arg" eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if defined $class_arg and "$class_arg" eq 1 1 8 9
Condition100%3/3
defined $class_arg and "$class_arg" eq 1
!ll&&!rl&&r
711
MC/DC100%2/2
defined $class_arg and "$class_arg" eq 1
defined $class_arg "$class_arg" eq 1
Truth table100%3/3
A and B
A= defined $class_argB= "$class_arg" eq 1
ABresult
0X0
100
111
295  
296 9 my $class_builder_sub;
297 9 ▶ if (ref($class_builder) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $class_builder eq 'CODE') { } 1 8 9
elsif (not defined $class_builder) { } 6 2 8
298 1 $class_builder_sub = $class_builder;
299 1 $class_builder = 1;
300 }
301 elsif (!defined $class_builder) {
302 6 ▶ $class_builder_sub = sub{ undef };
Subroutine __ANON__: called CC=1 SCAR=0.0
303 6 $class_builder = 1;
304 }
305  
306 9 ▶ if (defined($class_builder) and "$class_builder" eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class_builder and "$class_builder" eq 1) 8 1 9
Condition66%2/3
defined $class_builder and "$class_builder" eq 1
!ll&&!rl&&r
018
MC/DC50%1/2
defined $class_builder and "$class_builder" eq 1
defined $class_builder "$class_builder" eq 1
Truth table66%2/3
A and B
A= defined $class_builderB= "$class_builder" eq 1
ABresult
0X0
100
111
307 8 $class_builder = $name . '_build_class';
308 8 ▶ $class_builder = '_' . $class_builder if $class_builder !~ m{^_};
Branch: 1 branch
BranchTrueFalseTotal
unless $class_builder =~ /^_/ 8 0 8
309 }
310  
311 9 ▶ if ($class_builder_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_builder_sub) 7 2 9
312 7 install_modifier(
313 $consumer_class, 'fresh',
314 $class_builder => $class_builder_sub,
315 );
316 }
317  
318 install_modifier(
319 $consumer_class, 'around',
320 9 ▶ $class_builder => sub{
Subroutine __ANON__: called CC=6 SCAR=18.0
321 9 my ($orig, $self, $args) = @_;
322  
323 9 ▶ my $class = defined($class_arg) ? $args->{$class_arg} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $class_arg ? : 2 7 9
324 9 ▶ $class = $self->$orig( $args ) if !defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 7 2 9
325 9 ▶ $class = $default_class if !defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 4 5 9
326 9 ▶ return undef if !defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 9 9
327  
328 9 ▶ $class = $class_namespace . $class if $class =~ m{^::};
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^::/ 0 9 9
329 9 return $class;
330 },
331 9 );
332  
333 9 return $class_builder;
334 }
335  
336 9 ▶sub _normalize_args_builder {
Subroutine _normalize_args_builder: called CC=8 SCAR=20.9
337 9 my ($name, $consumer_class, %args) = @_;
338  
339 9 my $args_builder = delete $args{args_builder};
340  
341 9 ▶ $args_builder = undef if defined($args_builder) and "$args_builder" eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args_builder and "$args_builder" eq 0 0 9 9
Condition66%2/3
defined $args_builder and "$args_builder" eq 0
!ll&&!rl&&r
630
MC/DC0%0/2
defined $args_builder and "$args_builder" eq 0
defined $args_builder "$args_builder" eq 0
Truth table66%2/3
A and B
A= defined $args_builderB= "$args_builder" eq 0
ABresult
0X0
100
111
342  
343 9 my $args_builder_sub;
344  
345 9 ▶ if (ref($args_builder) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args_builder eq "CODE") 1 8 9
346 1 $args_builder_sub = $args_builder;
347 1 $args_builder = 1;
348 }
349  
350 9 ▶ if (defined($args_builder) and "$args_builder" eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_builder and "$args_builder" eq 1) 2 7 9
Condition100%3/3
defined $args_builder and "$args_builder" eq 1
!ll&&!rl&&r
612
MC/DC100%2/2
defined $args_builder and "$args_builder" eq 1
defined $args_builder "$args_builder" eq 1
Truth table100%3/3
A and B
A= defined $args_builderB= "$args_builder" eq 1
ABresult
0X0
100
111
351 2 $args_builder = $name . '_build_args';
352 2 ▶ $args_builder = '_' . $args_builder if $args_builder !~ m{^_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args_builder =~ /^_/ 2 0 2
353 }
354  
355 9 ▶ if ($args_builder_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_builder_sub) 1 8 9
356 1 install_modifier(
357 $consumer_class, 'fresh',
358 $args_builder => $args_builder_sub,
359 );
360 }
361  
362 9 return $args_builder;
363 }
364  
365 =head2 has_pluggable_class
366  
367 has_pluggable_class foo_bar_class => (
368 default => 'Foo::Bar',
369 );
370  
371 This function acts like L<Moo/has> but adds a bunch of functionality,
372 making it easy to refer to a class that gets plugins applied to it
373 at run-time.
374  
375 In the above C<foo_bar_class> example, the user of your class can then specify
376 the C<foo_bar_class> argument, if they wish, and the class they pass in will
377 have any relevant plugins applied to it.
378  
379 This function only supports a subset of the arguments that L<Moo/has>
380 supports. They are:
381  
382 builder
383 default
384 init_arg
385 isa
386 required
387  
388 =cut
389  
390 0 ▶sub has_pluggable_class {
Subroutine has_pluggable_class: not called CC=3 SCAR=24.8
Pod documented
391 0 my ($name, %args) = @_;
392 0 my $consumer_class = (caller())[0];
393 0 local $Carp::Internal{ (__PACKAGE__) } = 1;
394  
395 0 my $has = get_consumer_moo_has( $consumer_class );
396  
397 0 my $init_name = "_init_$name";
398  
399 $has->(
400 $init_name,
401 init_arg => $name,
402 is => 'ro',
403 isa => NonEmptySimpleStr,
404 lazy => 1,
405 (
406 0 map { $_ => $args{$_} }
407 0 grep { exists $args{$_} }
408 qw( default builder required init_arg )
409 ),
410 );
411  
412 0 ▶ my $isa = $args{isa} || ClassName;
Condition0%0/3
$args{'isa'} || &ClassName()
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'isa'} || &ClassName()
$args{'isa'} &ClassName()
Truth table0%0/3
A || B
A= $args{'isa'}B= &ClassName()
ABresult
1X1
011
000
413 0 ▶ $isa = $isa | Undef if !$args{required};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"required"} 0 0 0
414  
415 0 $has->(
416 $name,
417 init_arg => undef,
418 is => 'lazy',
419 isa => $isa,
420 builder => _build_class_attr_builder(
421 $init_name,
422 ),
423 );
424  
425 0 return;
426 }
427  
428 0 ▶sub _build_class_attr_builder {
Subroutine _build_class_attr_builder: not called CC=1 SCAR=6.9
429 0 my ($init_name) = @_;
430  
431 0 ▶ return sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
432 0 my ($self) = @_;
433  
434 0 my $class = $self->$init_name();
435  
436 0 ▶ require_module $class if !$class->can('new');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 0 0
437  
438 0 return $self->plugin_factory->build_class( $class );
439 0 };
440 }
441  
442 3 1;
443 __END__
↑ Summary PluginKit/Core.pm »