blib/lib/Catmandu/Pluggable.pm

filter: Statementstmt 100% 24 / 24 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 80.8% 38 / 47 CC 6 SCAR 14.3
CC
6
Cov
86%
with_plugins
6.3
BEGIN
1.0
BEGIN
1.0
CRAP 4.2
« Build ↑ Summary blib/lib/Catmandu/Plugin/Versioning.pm »
1 package Catmandu::Pluggable;
2  
3 15 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 15 our $VERSION = '1.0201_02';
6  
7 15 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶sub plugin_namespace {'Catmandu::Plugin'}
Subroutine plugin_namespace: called CC=1 SCAR=0.0
Pod documented
11  
12 sub with_plugins {
13 7 ▶ my $class = shift;
Subroutine with_plugins: called CC=6 SCAR=18.4
Pod documented
14 7 ▶ $class = ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
160
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
15 7 ▶ my @plugins = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 6 1 7
16 7 @plugins = split /,/, join ',', @plugins;
17 7 ▶ @plugins || return $class;
Branch: 1 branch
BranchTrueFalseTotal
@plugins or return $class 0 7 7
18 7 my $ns = $class->plugin_namespace;
19 Moo::Role->create_class_with_roles(
20 $class,
21 map {
22 7 my $pkg = $_;
23 7 ▶ if ($pkg !~ s/^\+// && $pkg !~ /^$ns/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg =~ s/^\+// or $pkg =~ /^$ns/) 7 0 7
Condition33%1/3
$pkg =~ s/^\+// or $pkg =~ /^$ns/
l!l&&r!l&&!r
007
MC/DC0%0/2
$pkg =~ s/^\+// or $pkg =~ /^$ns/
$pkg =~ s/^\+// $pkg =~ /^$ns/
Truth table33%1/3
A or B
A= $pkg =~ s/^\+//B= $pkg =~ /^$ns/
ABresult
1X1
011
000
24 7 $pkg = "${ns}::${pkg}";
25 }
26 7 $pkg;
27 } @plugins
28 );
29 }
30  
31 15 1;
32  
33 __END__
34  
35 =pod
36  
37 =head1 NAME
38  
39 Catmandu::Pluggable - A role for classes that need plugin capabilities
40  
41 =head1 SYNOPSIS
42  
43 package My::Foo::Bar;
44  
45 use Role::Tiny;
46  
47 before foo => sub {
48 print "Before foo!\n";
49 };
50  
51 after foo => sub {
52 print "After foo!\n";
53 };
54  
55 sub extra {
56 print "I can do extra too\n";
57 }
58  
59 package My::Foo;
60  
61 use Moo;
62  
63 with 'Catmandu::Pluggable';
64  
65 sub plugin_namespace {
66 'My::Foo';
67 }
68  
69 sub foo {
70 print "Foo!\n";
71 }
72  
73 package main;
74  
75 my $x = My::Foo->with_plugins('Bar')->new;
76  
77 # prints:
78 # Before foo!
79 # Foo!
80 # After foo!
81 $x->foo;
82  
83 # prints:
84 # I can do extra too
85 $x->extra;
86  
87 =head1 METHODS
88  
89 =head2 plugin_namespace
90  
91 Returns the namespace where all plugins for your class can be found.
92  
93 =head2 with_plugins(NAME)
94  
95 =head2 with_plugins(NAME,NAME,...)
96  
97 This class method returns a subclass of your class with all provided plugins NAME-s implemented.
98  
99 =head1 SEE ALSO
100  
101 L<Catmandu::Bag>,
102 L<Catmandu::Plugin::Datestamps>,
103 L<Catmandu::Plugin::Versioning>
104  
105 =cut
« Build ↑ Summary blib/lib/Catmandu/Plugin/Versioning.pm »