| 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
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 15 | 7 | ▶ | my @plugins = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 7 | @plugins = split /,/, join ',', @plugins; | |||||||||||||||||||||||||||
| 17 | 7 | ▶ | @plugins || return $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$pkg =~ s/^\+// or $pkg =~ /^$ns/
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/
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||