| 1 | package MooX::PluginKit::Factory; | ||||||||||||||||||||
| 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::Factory - Dynamically apply plugins to classes at runtime. | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | use MooX::PluginKit::Factory; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | my $kit = MooX::PluginKit::Factory->new( | ||||||||||||||||||||
| 15 | plugins => [...], | ||||||||||||||||||||
| 16 | namespace => ..., | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | my $object = $kit->class_new('Some::Class', %args); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | A PluginKit factory takes a list of plugins and then provides methods | ||||||||||||||||||||
| 24 | for applying those plugins to classes and building objects from those classes. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | Unless you are a power user you are better off using | ||||||||||||||||||||
| 27 | L<MooX::PluginKit::Consumer>. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 3 | ▶ | use MooX::PluginKit::Core; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 3 | ▶ | use Types::Standard -types; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 3 | ▶ | use Types::Common::String -types; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 3 | ▶ | use Module::Runtime qw( require_module ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 3 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =head1 ARGUMENTS | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head2 plugins | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | An array ref of plugin names (relative or absolute). | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | has plugins => ( | ||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||
| 49 | isa => ArrayRef[ NonEmptySimpleStr ], | ||||||||||||||||||||
| 50 | 0 | ▶ | default => sub{ [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 51 | 3 | ); | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head2 namespace | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | The namespace to resolve relative plugin names to. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 3 | has namespace => ( | |||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||
| 61 | isa => NonEmptySimpleStr, | ||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =head2 resolved_plugins | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | L</plugins> with all relative plugin names resolved. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 3 | has resolved_plugins => ( | |||||||||||||||||||
| 73 | is => 'lazy', | ||||||||||||||||||||
| 74 | init_arg => undef, | ||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||
| 76 | 28 | ▶ | sub _build_resolved_plugins { | ||||||||||||||||||
|
Subroutine
_build_resolved_plugins: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 28 | my ($self) = @_; | |||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | return [ | ||||||||||||||||||||
| 80 | 12 | map { resolve_plugin( $_, $self->namespace() ) } | |||||||||||||||||||
| 81 | 28 | @{ $self->plugins() } | |||||||||||||||||||
| 82 | ]; | ||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head1 METHODS | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head2 build_class | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | my $new_class = $kit->build_class( $class ); | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Creates a new class with all applicable L</plugins> applied to it | ||||||||||||||||||||
| 92 | and returns the new class name. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 29 | ▶ | sub build_class { | ||||||||||||||||||
|
Subroutine
build_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | 29 | my ($self, $base_class) = @_; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | return build_class_with_plugins( | ||||||||||||||||||||
| 100 | $base_class, | ||||||||||||||||||||
| 101 | 29 | @{ $self->resolved_plugins() }, | |||||||||||||||||||
| 102 | ); | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 class_new | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | my $object = $kit->class_new( $class, %args ); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | Calls L</build_class> and then creates an object of that class. | ||||||||||||||||||||
| 110 | If the class to be built is a plugin consumer then | ||||||||||||||||||||
| 111 | L<MooX::PluginKit::ConsumerRole/plugin_factory> will be defaulted | ||||||||||||||||||||
| 112 | to this factory. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 11 | ▶ | sub class_new { | ||||||||||||||||||
|
Subroutine
class_new: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 117 | 11 | my $self = shift; | |||||||||||||||||||
| 118 | 11 | my $base_class = shift; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 11 | my $class = $self->build_class( $base_class ); | |||||||||||||||||||
| 121 | 11 | ▶ | require_module $class if !$class->can('new'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 11 | my $args = $class->BUILDARGS( @_ ); | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 11 | ▶ | if (is_consumer $class) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 0 | ▶ | $args->{plugin_factory} ||= $self; | ||||||||||||||||||
|
Condition0%0/3
$args->{"plugin_factory"} ||= $self
MC/DC0%0/2
$args->{"plugin_factory"} ||= $self
$args->{"plugin_factory"}
$self
Truth table0%0/3
A ||= B
A= $args->{"plugin_factory"}B= $self
| |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 11 | return $class->new( $args ); | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 3 | 1; | |||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||