blib/lib/MooX/Object/Pluggable.pm

filter: Statementstmt 96.3% 104 / 108 Branchbran 91.6% 33 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 88.8% 16 / 18 Podpod 60.0% 3 / 5 totaltotal 92.6% 164 / 177 CC 23 SCAR 14.8
CC
23
Cov
95%
load_plugins
8.0
__ANON__
7.0
_pluggable_object
5.0
CRAP 4.4
↑ Summary
1 package MooX::Object::Pluggable;
2 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 require Module::Pluggable::Object;
6 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.0.5'; # VERSION
9 # ABSTRACT: Moo eXtension to inject plugins to exist objects as a role
10  
11  
12 15 ▶sub _apply_roles {
Subroutine _apply_roles: called CC=2 SCAR=6.9
13 15 my ($self, @roles) = @_;
14 map {
15 15 my $role = $_;
16 20 ▶ Moo::Role->apply_roles_to_object($self, $role) unless $self->does($role)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does($role) 17 3 20
17 } @roles;
18 15 return $self;
19 }
20  
21 0 ▶sub load_plugin { load_plugins(@_) }
Subroutine load_plugin: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 14 ▶sub load_plugins {
Subroutine load_plugins: called CC=8 SCAR=20.8
Pod documented
24 14 my ($self, @plugin_options) = @_;
25 14 my $pluggable_object = $self->_pluggable_object;
26 14 my @plugins = $pluggable_object->plugins;
27 # Provide ability for roles in a real package, with syntax: '+MooX::ConfigFromFile'
28 map {
29 1 my $option = $_; $option=~s/^\+//;
30 1 $self->_apply_roles($option);
31 14 } grep { /^\+/ } @plugin_options;
32 14 ▶ return $self unless @plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless @plugins 0 14 14
33 14 for my $plugin_option (@plugin_options) {
34 16 ▶ if ($plugin_option eq '-all') {
Branch: 3 branches
BranchTrueFalseTotal
if ($plugin_option eq '-all') { } 1 15 16
elsif (ref $plugin_option eq 'ARRAY') { } 1 14 15
elsif (ref $plugin_option eq 'Regexp') { } 4 10 14
35 1 $self->_apply_roles(@plugins);
36 } elsif (ref $plugin_option eq 'ARRAY') {
37 1 $self->load_plugins(@$plugin_option);
38 } elsif (ref $plugin_option eq 'Regexp') {
39 4 my @load_plugins = grep { $plugin_option } @plugins;
40 4 ▶ return $self unless @load_plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless @load_plugins 0 4 4
41 4 $self->_apply_roles(@load_plugins);
42 } else {
43 10 my @load_plugins = map { $_.'::'.$plugin_option } @{$pluggable_object->{search_path}};
44 10 my %all_plugins = map { $_ => 1 } @plugins;
45 10 my @real_roles = grep { $all_plugins{$_} } @load_plugins;
46 10 ▶ return $self unless @real_roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @real_roles 1 9 10
47 9 $self->_apply_roles(@real_roles)
48 }
49 }
50 13 return $self;
51 }
52  
53  
54 1 ▶sub plugins {
Subroutine plugins: called CC=1 SCAR=0.0
Pod documented
55 1 my ($self) = @_;
56 1 $self->_pluggable_object->plugins;
57 }
58  
59  
60 0 ▶sub loaded_plugins {
Subroutine loaded_plugins: not called CC=1 SCAR=6.9
Pod documented
61 0 my $self = shift;
62 0 grep { $self->does($_) } $self->plugins;
63 }
64  
65  
66 4 my %pluggable_objects = (); # key: object, value: loaded plugins
67  
68 13 ▶sub BUILD { } # BUILD() will be override by consumers, so we use afterBuild
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
69  
70 13 ▶after BUILD => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
71 13 my ($self, $opts) = @_;
72 13 ▶ if (defined $opts->{pluggable_options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"pluggable_options"}) 2 11 13
73 2 my $pluggable_options = $opts->{pluggable_options};
74 2 ▶ $pluggable_options->{package} = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 0 2
75 2 $pluggable_objects{refaddr($self)} = Module::Pluggable::Object->new(%$pluggable_options);
76 }
77 13 ▶ if (defined $self->_build_load_plugins and scalar @{$self->_build_load_plugins} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_build_load_plugins and scalar @{$self->_build_load_plugins;} > 0) 4 9 13
Condition66%2/3
defined $self->_build_load_plugins and scalar @{$self->_build_load_plugins;} > 0
!ll&&!rl&&r
094
MC/DC50%1/2
defined $self->_build_load_plugins and scalar @{$self->_build_load_plugins;} > 0
defined $self->_build_load_plugins scalar @{$self->_build_load_plugins;} > 0
Truth table66%2/3
A and B
A= defined $self->_build_load_pluginsB= scalar @{$self->_build_load_plugins;} > 0
ABresult
0X0
100
111
78 4 $self->load_plugins(@{$self->_build_load_plugins});
79 }
80 13 ▶ if (defined $opts->{load_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"load_plugins"}) 4 9 13
81 $self->load_plugins(ref $opts->{load_plugins} eq 'ARRAY' ?
82 3 @{$opts->{load_plugins}} : $opts->{load_plugins}
83 4 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'load_plugins'} eq 'ARRAY' ? : 3 1 4
84 }
85 4 };
86  
87 2 ▶sub _build_pluggable_options { {} }
Subroutine _build_pluggable_options: called CC=1 SCAR=0.0
88  
89 18 ▶sub _build_load_plugins { [] }
Subroutine _build_load_plugins: called CC=1 SCAR=0.0
90  
91 27 ▶sub _pluggable_object {
Subroutine _pluggable_object: called CC=5 SCAR=16.1
92 27 my $self = shift;
93 27 my ($class, $addr);
94 27 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 22 5 27
95 22 $class = ref $self;
96 22 $addr = refaddr $self;
97 } else {
98 5 $class = $self;
99 }
100 # Find self pluggable object;
101 27 ▶ return $pluggable_objects{$addr} if defined $addr and defined $pluggable_objects{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $addr and defined $pluggable_objects{$addr} 3 24 27
Condition100%3/3
defined $addr and defined $pluggable_objects{$addr}
!ll&&!rl&&r
5193
MC/DC100%2/2
defined $addr and defined $pluggable_objects{$addr}
defined $addr defined $pluggable_objects{$addr}
Truth table100%3/3
A and B
A= defined $addrB= defined $pluggable_objects{$addr}
ABresult
0X0
100
111
102 # Find package pluggable object;
103 24 $class=~s/__WITH__.*//g; # use parent package name as class name.
104 24 ▶ return $pluggable_objects{$class} if defined $pluggable_objects{$class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pluggable_objects{$class} 19 5 24
105 # Not found, create a new one for package.
106 5 my $pluggable_options = $self->_build_pluggable_options;
107 5 $pluggable_options->{package} = $class;
108 5 $pluggable_objects{$class} = Module::Pluggable::Object->new(
109 %$pluggable_options,
110 );
111 }
112  
113  
114 8 ▶sub _inject_roles_to {
Subroutine _inject_roles_to: called CC=4 SCAR=13.9
115 8 my ($target, $import_options) = @_;
116 8 my $with = $target->can("with");
117 8 ▶ return unless $with; # Do nothing unless it's a Moo(se) object or role.
Branch: 1 branch
BranchTrueFalseTotal
unless $with 3 5 8
118  
119 5 $with->('MooX::Object::Pluggable');
120 5 my $around = $target->can("around");
121 5 for my $builder (qw/pluggable_options load_plugins/) {
122 10 my ($key) = grep /$builder/, keys %$import_options;
123 10 ▶ next unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 3 7 10
124 7 ▶ $around->("_build_$builder" => sub { $import_options->{$key} });
Subroutine __ANON__: called CC=1 SCAR=0.0
125 }
126 }
127  
128 sub import
129 7 ▶{
Subroutine import: called CC=2 SCAR=6.9
130 7 my ( undef, %import_options ) = @_;
131 7 my $target = caller;
132 # Inject roles to target namespace
133 7 &_inject_roles_to($target, \%import_options);
134  
135 # Compatible for MooX
136 7 my $around = $target->can("around");
137 7 ▶ return unless $around;
Branch: 1 branch
BranchTrueFalseTotal
unless $around 3 4 7
138 1 ▶ $around->("import" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 1 my ($orig, $self, @opts) = @_;
140 1 my %pluggable_opts = map { $opts[$_] => $opts[$_ + 1] } grep { $opts[$_] =~/^-(pluggable_options|load_plugins)$/ } 0..$#opts;
141 1 &_inject_roles_to($target, \%pluggable_opts);
142 1 my %hash = map { $_ => 1 } %pluggable_opts;
143 1 my @remains = grep { ! defined $hash{$_} } @opts;
144 1 $self->$orig(@remains);
145 4 });
146 4 return;
147 }
148  
149  
150 4 1;
151  
152 __END__
↑ Summary