Singleton/Object.pm

filter: Statementstmt 48.5% 34 / 70 Branchbran 13.3% 4 / 30 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 62.5% 5 / 8 Podpod 100% 4 / 4 totaltotal 35.9% 50 / 139 CC 25 SCAR 37.8
CC
25
Cov
35%
find
110.0
call
20.0
_parse_plugins
18.6
CRAP 44.0
↑ Summary Singleton.pm »
1 package Module::Pluggable::Singleton::Object;
2 {
3 2 $Module::Pluggable::Singleton::Object::VERSION = '0.06';
4 }
5 {
6 2 $Module::Pluggable::Singleton::Object::DIST = 'Module-Pluggable-Singleton';
7 }
8  
9 2 ▶use parent 'Module::Pluggable::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dump q/pp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Module::Pluggable::Singleton::Object
16  
17 =head1 VERSION
18  
19 version 0.06
20  
21 =head1 METHODS
22  
23 =head2 new
24  
25 =cut
26
27 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
28 1 my $class = shift;
29 1 my %opts = @_;
30
31 1 $opts{require} = 1;
32 1 my $obj = Module::Pluggable::Object->new(%opts);
33  
34 1 my $self = bless $obj, $class;
35 1 ▶ $self->_parse_plugins($opts{package} || $class);
Condition33%1/3
$opts{'package'} || $class
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'package'} || $class
$opts{'package'} $class
Truth table33%1/3
A || B
A= $opts{'package'}B= $class
ABresult
1X1
011
000
36  
37 1 return $self;
38 }
39  
40  
41 1 ▶sub _parse_plugins {
Subroutine _parse_plugins: called CC=10 SCAR=29.2
42 1 my($self,$caller) = @_;
43  
44 1 ▶ if (!$self->{search_path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"search_path"}) 1 0 1
45 1 $self->{search_path} = "${caller}::Plugin";
46 }
47 1 ▶ if ($self->{search_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"search_path"}) 1 0 1
48 1 ▶ if (ref($self->{search_path}) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"search_path"} eq "") 1 0 1
49 1 $self->{search_path} = [$self->{search_path}];
50 }
51 }
52  
53 warn "SEARCH_PATH: ". pp($self->{search_path})
54 1 ▶ if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
55  
56  
57 1 ▶ my $namespace = "${caller}::". ucfirst($self->{sub_name} || 'plugins');
Condition50%1/2
$self->{'sub_name'} || 'plugins'
l!l
01
MC/DC0%0/1
$self->{'sub_name'} || 'plugins'
$self->{'sub_name'}
Truth table50%1/2
A
A= $self->{'sub_name'}
Aresult
11
00
58 1 ▶ my $sub_name = $self->{sub_name} || 'plugins';
Condition50%1/2
$self->{'sub_name'} || 'plugins'
l!l
01
MC/DC0%0/1
$self->{'sub_name'} || 'plugins'
$self->{'sub_name'}
Truth table50%1/2
A
A= $self->{'sub_name'}
Aresult
11
00
59 1 my $plugin_for = { }; # maps shortname to module name
60 1 my $instance_of = { }; # instances
61  
62  
63 1 foreach my $plugin ($self->plugins) {
64 0 my $shortname = $plugin;
65  
66 0 foreach my $path (@{$self->{search_path}}) {
67 0 $shortname =~ s/^${path}:://;
68 }
69  
70 # FIXME:
71 # if (not $plugin->isa($base_class)) {
72 # confess __PACKAGE__ .": plugin '$shortname' needs to implement "
73 # ."'". $base_class ."'";
74 # }
75  
76 0 ▶ if (exists $plugin_for->{$shortname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $plugin_for->{$shortname}) 0 0 0
77 0 confess "$caller: Plugin already exists for '$shortname'";
78 }
79  
80 0 $plugin_for->{$shortname} = $plugin;
81 }
82  
83 1 $self->{plugin_for} = $plugin_for;
84 }
85  
86 =head2 find
87  
88 =cut
89  
90 0 ▶sub find {
Subroutine find: not called CC=10 SCAR=47.0
Pod documented
91 0 my($self,$shortname) = @_;
92  
93 0 ▶ if (!defined $shortname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shortname) 0 0 0
94 0 die "Not provided name of plugin";
95 0 return;
96 }
97  
98 0 ▶ my $name = $self->{plugin_for}->{$shortname} || undef;
Condition0%0/2
$self->{'plugin_for'}{$shortname} || undef
l!l
00
MC/DC0%0/1
$self->{'plugin_for'}{$shortname} || undef
$self->{'plugin_for'}{$shortname}
Truth table0%0/2
A
A= $self->{'plugin_for'}{$shortname}
Aresult
11
00
99 0 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
100 0 die "Not possible to load module '$shortname'";
101 }
102  
103  
104 # use an existing instance or create a new one.. and keep ref to it
105 0 ▶ my $instance_of = $self->{instance_of} // { };
Condition0%0/2
$self->{"instance_of"} // {}
l!l
00
MC/DC0%0/1
$self->{"instance_of"} // {}
$self->{"instance_of"}
Truth table0%0/2
A
A= $self->{"instance_of"}
Aresult
11
00
106 0 ▶ my $instance = $instance_of->{$shortname}
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
Condition0%0/3
$instance_of->{$shortname} || (defined $name ? $name->new : undef)
l!l&&r!l&&!r
000
MC/DC0%0/2
$instance_of->{$shortname} || (defined $name ? $name->new : undef)
$instance_of->{$shortname} (defined $name ? $name->new : undef)
Truth table0%0/3
A || B
A= $instance_of->{$shortname}B= (defined $name ? $name->new : undef)
ABresult
1X1
011
000
107 || (defined $name ? $name->new() : undef);
108  
109 0 ▶ if ($instance && not defined $instance_of->{$shortname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance and not defined $instance_of->{$shortname}) 0 0 0
Condition0%0/3
$instance and not defined $instance_of->{$shortname}
!ll&&!rl&&r
000
MC/DC0%0/2
$instance and not defined $instance_of->{$shortname}
$instance not defined $instance_of->{$shortname}
Truth table0%0/3
A and not(B)
A= $instanceB= not defined $instance_of->{$shortname}
ABresult
0X0
100
111
110 0 $instance_of->{$shortname} = $instance;
111 }
112  
113 # if we didn't already the hash ref created
114 0 ▶ $self->{instance_of} = $instance_of if (!defined $self->{instance_of});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"instance_of"} 0 0 0
115  
116 0 return $instance;
117 }
118  
119 =head2 plugin
120  
121 =cut
122  
123 0 ▶sub plugin {
Subroutine plugin: not called CC=3 SCAR=24.8
Pod documented
124 0 my($self,$shortname) = @_;
125 0 my $plugin_for = $self->{plugin_for};
126  
127 0 ▶ return keys %{$plugin_for} if (!defined $shortname);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shortname 0 0 0
128  
129 return defined $plugin_for->{$shortname}
130 0 ▶ ? $plugin_for->{$shortname} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $plugin_for->{$shortname} ? : 0 0 0
131  
132 }
133  
134 =head2 call
135  
136 =cut
137  
138 0 ▶sub call {
Subroutine call: not called CC=4 SCAR=30.0
Pod documented
139 0 my($self,$shortname,$method) = @_;
140  
141 0 ▶ if (!defined $shortname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shortname) 0 0 0
142 0 die "$caller: Plugin name not provided";
143 0 return;
144 }
145  
146 0 ▶ if (!defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 0 0 0
147 0 die "Method name not provided";
148 0 return;
149 }
150  
151  
152 0 my $instance = $self->find($shortname);
153 0 ▶ if (!$instance->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance->can($method)) 0 0 0
154 0 die "Cannot call '$method' on '$shortname' plugin";
155 }
156  
157 0 return $instance->$method(@_);
158  
159 }
160  
161 2 1;
↑ Summary Singleton.pm »