Component/Component/Autocall/SingletonMethod.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 86 CC 6 SCAR 28.9
CC
6
Cov
0%
register_method
42.0
remove_method
2.0
DESTROY
2.0
CRAP 18.0
1 package Class::Component::Component::Autocall::SingletonMethod;
2  
3 use strict;
4 use warnings;
5  
6 use Carp::Clan qw/Class::Component/;
7  
8 my $instance_counter = 0;
9 my $alloc_map = {};
10 sub register_method {
11 my($self, @methods) = @_;
12  
13 $self->NEXT( register_method => @methods );
14  
15 my %add_methods;
16 while (my($method, $plugin) = splice @methods, 0, 2) {
17 $add_methods{$method} = $plugin
18 }
19 return unless %add_methods;
20  
21 my $singleton_class;
22 my $pkg = ref($self);
23 unless ($pkg =~ /::_Singletons::\d+$/) {
24 $singleton_class = "$pkg\::_Singletons::";
25 my $count;
26 for my $c (0..$instance_counter) {
27 no strict 'refs';
28 next if $alloc_map->{"$singleton_class$c"};
29 $count = $c;
30 last;
31 }
32 $count = ++$instance_counter unless defined $count;
33 $singleton_class .= $count;
34 $alloc_map->{$singleton_class} = 1;
35
36 { no strict 'refs'; @{"$singleton_class\::ISA"} = $pkg; }
37 bless $self, $singleton_class if ref($self);
38 Class::Component::Implement->component_isa_list->{$singleton_class} = Class::Component::Implement->component_isa_list->{$pkg};
39 } else {
40 $singleton_class = $pkg;
41 }
42  
43 for my $method (keys %add_methods) {
44 no strict 'refs';
45 *{"$singleton_class\::$method"} = sub { shift->call($method, @_) };
46 }
47 }
48  
49 sub remove_method {
50 my($self, @methods) = @_;
51 $self->NEXT( remove_method => @methods );
52 while (my($method, $plugin) = splice @methods, 0, 2) {
53 no strict 'refs';
54 delete ${ref($self) . "::"}{$method};
55 }
56 }
57  
58 sub DESTROY {
59 my $self = shift;
60 $self->remove_method(%{ $self->class_component_methods });
61 $self->class_component_clear_isa_list;
62 delete $alloc_map->{ref $self};
63 }
64  
65 1;