blib/lib/Object/Container.pm

filter: Statementstmt 93.8% 153 / 163 Branchbran 80.0% 56 / 70 Conditioncond 52.3% 22 / 42 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 90.3% 28 / 31 Podpod 81.8% 9 / 11 totaltotal 80.4% 275 / 342 SCAR 42.1
CC
55
Cov
84%
_is_class_loaded
39.0
register
11.3
get
9.0
CRAP 67.4
↑ Summary Build »
1 package Object::Container;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '0.15';
9  
10 13 __PACKAGE__->mk_accessors(qw/registered_classes autoloader_rules objects/);
11  
12 BEGIN {
13 13 ▶ our $_HAVE_EAC = 1;
Subroutine BEGIN: called CC=2 SCAR=6.9
14 13 eval { local $SIG{__DIE__}; require Exporter::AutoClean; };
15 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 12 13
16 1 $_HAVE_EAC = 0;
17 }
18 }
19  
20 do {
21 13 my @EXPORTS;
22  
23 sub import {
24 17 ▶ my ($class, $name) = @_;
Subroutine import: called CC=5 SCAR=16.1
25 17 ▶ return unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 9 8 17
26  
27 8 my $caller = caller;
28 {
29 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶ if ($name =~ /^-base$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^-base$/i) { } 4 4 8
31 4 push @{"${caller}::ISA"}, $class;
32 4 my $r = $class->can('register');
33 4 my $l = $class->can('autoloader');
34
35 my %exports = (
36 8 ▶ register => sub { $r->($caller, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 0 ▶ autoloader => sub { $l->($caller, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 preload => sub {
39 0 ▶ $caller->instance->get($_) for @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 },
41 preload_all_except => sub {
42 1 ▶ $caller->instance->load_all_except(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
43 },
44 preload_all => sub {
45 0 ▶ $caller->instance->load_all;
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 },
47 4 );
48
49 4 ▶ if ($Object::Container::_HAVE_EAC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Object::Container::_HAVE_EAC) { } 2 2 4
50 2 Exporter::AutoClean->export( $caller, %exports );
51 }
52 else {
53 2 ▶ while (my ($name, $fn) = each %exports) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %exports) 10 2 12
54 10 *{"${caller}::${name}"} = $fn;
55 }
56 2 @EXPORTS = keys %exports;
57 }
58 }
59 else {
60 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 4 *{"${caller}::${name}"} = sub {
62 11 ▶ my ($target) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
63 11 ▶ return $target ? $class->get($target) : $class;
Branch: 1 branch
BranchTrueFalseTotal
$target ? : 6 5 11
64 4 };
65 }
66 }
67 }
68  
69 sub unimport {
70 1 ▶ my $caller = caller;
Subroutine unimport: called CC=2 SCAR=6.9
71  
72 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 for my $name (@EXPORTS) {
74 5 delete ${ $caller . '::' }{ $name };
75 }
76  
77 1 1; # for EOF
78 }
79 13 };
80  
81 13 my %INSTANCES;
82 sub instance {
83 24 ▶ my $class = shift;
Subroutine instance: called CC=2 SCAR=7.1
Pod documented
84 24 ▶ return $INSTANCES{$class} ||= $class->new;
Condition66%2/3
$INSTANCES{$class} ||= $class->new
l!l&&r!l&&!r
1770
MC/DC0%0/2
$INSTANCES{$class} ||= $class->new
$INSTANCES{$class} $class->new
Truth table66%2/3
A ||= B
A= $INSTANCES{$class}B= $class->new
ABresult
1X1
011
000
85 }
86  
87 sub has_instance {
88 2 ▶ my $class = shift;
Subroutine has_instance: called CC=2 SCAR=7.6
Pod undocumented
89 2 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
90 2 return $INSTANCES{$class};
91 };
92  
93 sub new {
94 17 ▶ $_[0]->SUPER::new( +{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
95 registered_classes => +{},
96 autoloader_rules => +[],
97 objects => +{},
98 } );
99 }
100  
101 sub register {
102 22 ▶ my ($self, $args, @rest) = @_;
Subroutine register: called CC=11 SCAR=24.2
Pod documented
103 22 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 10 12 22
104  
105 22 my ($class, $initializer, $is_preload);
106 22 ▶ if (defined $args && !ref $args) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args and not ref $args) { } 19 3 22
elsif (ref $args eq 'HASH') { } 3 0 3
Condition66%2/3
defined $args and not ref $args
!ll&&!rl&&r
0319
MC/DC50%1/2
defined $args and not ref $args
defined $args not ref $args
Truth table66%2/3
A and not(B)
A= defined $argsB= not ref $args
ABresult
0X0
100
111
107 19 $class = $args;
108 19 ▶ if (@rest == 1 and ref $rest[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest == 1 and ref $rest[0] eq 'CODE') { } 6 13 19
Condition66%2/3
@rest == 1 and ref $rest[0] eq "CODE"
!ll&&!rl&&r
1306
MC/DC50%1/2
@rest == 1 and ref $rest[0] eq "CODE"
@rest == 1 ref $rest[0] eq "CODE"
Truth table66%2/3
A and B
A= @rest == 1B= ref $rest[0] eq "CODE"
ABresult
0X0
100
111
109 6 $initializer = $rest[0];
110 }
111 else {
112 $initializer = sub {
113 11 ▶ $self->ensure_class_loaded($class);
Subroutine __ANON__: called CC=1 SCAR=0.0
114 11 $class->new(@rest);
115 13 };
116 }
117 }
118 elsif (ref $args eq 'HASH') {
119 3 $class = $args->{class};
120 3 ▶ if (exists $args->{initializer} && ref $args->{initializer} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'initializer'} and ref $args->{'initializer'} eq 'CODE') { } 2 1 3
Condition66%2/3
exists $args->{"initializer"} and ref $args->{"initializer"} eq "CODE"
!ll&&!rl&&r
102
MC/DC50%1/2
exists $args->{"initializer"} and ref $args->{"initializer"} eq "CODE"
exists $args->{"initializer"} ref $args->{"initializer"} eq "CODE"
Truth table66%2/3
A and B
A= exists $args->{"initializer"}B= ref $args->{"initializer"} eq "CODE"
ABresult
0X0
100
111
121 2 $initializer = $args->{initializer};
122 }
123 else {
124 $initializer = sub {
125 1 ▶ $self->ensure_class_loaded($class);
Subroutine __ANON__: called CC=2 SCAR=7.1
126 1 ▶ $class->new(@{exists $args->{args} ? $args->{args} : []});
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'args'} ? : 1 0 1
127 1 };
128 }
129  
130 3 ▶ $is_preload = 1 if $args->{preload};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"preload"} 1 2 3
131 }
132 else {
133 0 croak "Usage: $self->register($class || { class => $class ... })";
134 }
135  
136 22 $self->registered_classes->{$class} = $initializer;
137 22 ▶ $self->get($class) if $is_preload;
Branch: 1 branch
BranchTrueFalseTotal
if $is_preload 1 21 22
138
139 22 return $initializer;
140 }
141  
142 sub unregister {
143 1 ▶ my ($self, $class) = @_;
Subroutine unregister: called CC=3 SCAR=11.7
Pod documented
144 1 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
145  
146 1 ▶ delete $self->registered_classes->{$class} and $self->remove($class);
Branch: 1 branch
BranchTrueFalseTotal
delete $self->registered_classes->{$class} and $self->remove($class) 1 0 1
147 }
148  
149 sub autoloader {
150 1 ▶ my ($self, $rule, $trigger) = @_;
Subroutine autoloader: called CC=2 SCAR=7.0
Pod undocumented
151 1 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
152  
153 1 push @{ $self->autoloader_rules }, [$rule, $trigger];
154 }
155  
156 sub get {
157 32 ▶ my ($self, $class) = @_;
Subroutine get: called CC=9 SCAR=22.0
Pod documented
158 32 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 11 21 32
159  
160 32 ▶ my $obj = $self->objects->{ $class } ||= do {
Condition100%3/3
$self->objects->{$class} ||= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
l!l&&r!l&&!r
11183
MC/DC100%2/2
$self->objects->{$class} ||= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
$self->objects->{$class} do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
Truth table100%3/3
A ||= B
A= $self->objects->{$class}B= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
ABresult
1X1
011
000
161 21 my $initializer = $self->registered_classes->{ $class };
162 21 ▶ $initializer ? $initializer->($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
$initializer ? : 18 3 21
163 };
164  
165 32 ▶ unless ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 3 29 32
166 # autoloaderer
167 3 ▶ if (my ($trigger) = grep { $class =~ /$_->[0]/ } @{ $self->autoloader_rules }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($trigger) = grep({$class =~ /$_->[0]/;} @{$self->autoloader_rules;})) 1 2 3
168 1 $trigger->[1]->($self, $class);
169 }
170  
171 3 ▶ $obj = $self->objects->{ $class } ||= do {
Condition66%2/3
$self->objects->{$class} ||= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
l!l&&r!l&&!r
012
MC/DC50%1/2
$self->objects->{$class} ||= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
$self->objects->{$class} do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
Truth table66%2/3
A ||= B
A= $self->objects->{$class}B= do { my $initializer = $self->registered_classes->{$class}; $initializer ? &$initializer($self) : () }
ABresult
1X1
011
000
172 3 my $initializer = $self->registered_classes->{ $class };
173 3 ▶ $initializer ? $initializer->($self) : ();
Branch: 1 branch
BranchTrueFalseTotal
$initializer ? : 1 2 3
174 };
175 }
176
177 32 ▶ $obj or croak qq["$class" is not registered in @{[ ref $self ]}];
Branch: 1 branch
BranchTrueFalseTotal
$obj or croak(qq["$class" is not registered in @{[ref $self];}]) 2 30 32
178 }
179  
180 sub remove {
181 2 ▶ my ($self, $class) = @_;
Subroutine remove: called CC=2 SCAR=7.1
Pod documented
182 2 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2 2
183 2 delete $self->objects->{ $class };
184 }
185  
186 sub load_all {
187 1 ▶ my ($self) = @_;
Subroutine load_all: called CC=1 SCAR=0.0
Pod documented
188 1 $self->load_all_except;
189 }
190  
191 sub load_all_except {
192 3 ▶ my ($self, @except) = @_;
Subroutine load_all_except: called CC=4 SCAR=13.9
Pod documented
193 3 ▶ $self = $self->instance unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 3 3
194  
195 3 for my $class (keys %{ $self->registered_classes }) {
196 6 ▶ next if grep { $class eq $_ } @except;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$class eq $_;} @except 2 4 6
197 4 $self->get($class);
198 }
199 }
200  
201 # taken from Mouse
202 sub _is_class_loaded {
203 12 ▶ my $class = shift;
Subroutine _is_class_loaded: called CC=17 SCAR=36.6
204  
205 12 ▶ return 0 if ref($class) || !defined($class) || !length($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class or not defined $class or not length $class 0 12 12
Condition33%2/6
ref $class or not defined $class or not length $class
l!l&&r!l&&!r
0012
ref $class or not defined $class
l!l&&r!l&&!r
0012
MC/DC0%0/3
ref $class or not defined $class or not length $class
ref $class not defined $class not length $class
Truth table25%1/4
A or not(B) or not(C)
A= ref $classB= not defined $classC= not length $class
ABCresult
1XX1
01X1
0011
0000
206  
207 # walk the symbol table tree to avoid autovififying
208 # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::
209  
210 12 my $pack = \%::;
211 12 foreach my $part (split('::', $class)) {
212 14 $part .= '::';
213 14 ▶ return 0 if !exists $pack->{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pack->{$part} 3 11 14
214  
215 11 my $entry = \$pack->{$part};
216 11 ▶ return 0 if ref($entry) ne 'GLOB';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" 0 11 11
217 11 $pack = *{$entry}{HASH};
218 }
219  
220 9 ▶ return 0 if !%{$pack};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$pack;} 0 9 9
221  
222 # check for $VERSION or @ISA
223 return 1 if exists $pack->{VERSION}
224 9 ▶ && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};} 3 6 9
Condition66%4/6
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
!ll&&!rl&&r
603
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"}
!ll&&!rl&&r
603
MC/DC33%1/3
exists $pack->{"VERSION"} and defined *{$pack->{"VERSION"};}{"SCALAR"} and defined ${$$pack{"VERSION"};}
exists $pack->{"VERSION"} defined *{$pack->{"VERSION"};}{"SCALAR"} defined ${$$pack{"VERSION"};}
Truth table50%2/4
A and B and C
A= exists $pack->{"VERSION"}B= defined *{$pack->{"VERSION"};}{"SCALAR"}C= defined ${$$pack{"VERSION"};}
ABCresult
0XX0
10X0
1100
1111
225 return 1 if exists $pack->{ISA}
226 6 ▶ && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0 6 0 6
Condition33%2/6
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
!ll&&!rl&&r
006
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"}
!ll&&!rl&&r
006
MC/DC0%0/3
exists $pack->{"ISA"} and defined *{$pack->{"ISA"};}{"ARRAY"} and @{$pack->{"ISA"};} != 0
exists $pack->{"ISA"} defined *{$pack->{"ISA"};}{"ARRAY"} @{$pack->{"ISA"};} != 0
Truth table25%1/4
A and B and C
A= exists $pack->{"ISA"}B= defined *{$pack->{"ISA"};}{"ARRAY"}C= @{$pack->{"ISA"};} != 0
ABCresult
0XX0
10X0
1100
1111
227  
228 # check for any method
229 0 foreach my $name( keys %{$pack} ) {
230 0 my $entry = \$pack->{$name};
231 0 ▶ return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry ne "GLOB" or defined *{$entry;}{"CODE"} 0 0 0
Condition0%0/3
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $entry ne "GLOB" or defined *{$entry;}{"CODE"}
ref $entry ne "GLOB" defined *{$entry;}{"CODE"}
Truth table0%0/3
A or B
A= ref $entry ne "GLOB"B= defined *{$entry;}{"CODE"}
ABresult
1X1
011
000
232 }
233  
234 # fail
235 0 return 0;
236 }
237  
238  
239 sub _try_load_one_class {
240 12 ▶ my $class = shift;
Subroutine _try_load_one_class: called CC=2 SCAR=6.9
241  
242 12 ▶ return '' if _is_class_loaded($class);
Branch: 1 branch
BranchTrueFalseTotal
if _is_class_loaded($class) 9 3 12
243 3 my $klass = $class;
244 3 $klass =~ s{::}{/}g;
245 3 $klass .= '.pm';
246  
247 3 return do {
248 3 local $@;
249 3 eval { require $klass };
250 3 $@;
251 };
252 }
253  
254 sub ensure_class_loaded {
255 12 ▶ my ($self, $class) = @_;
Subroutine ensure_class_loaded: called CC=2 SCAR=7.0
Pod documented
256 12 my $e = _try_load_one_class($class);
257 12 ▶ Carp::confess "Could not load class ($class) because : $e" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 12 12
258  
259 12 return $class;
260 }
261  
262 13 1;
263 __END__
↑ Summary Build »