| 1 | package Eixo::Base::Singleton; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use Eixo::Base::Clase; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 11 | ▶ | sub make_singleton{ | ||||||||
|
Subroutine
make_singleton: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 7 | 11 | my ($clase, %args) = @_; | |||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 11 | ▶ | return if(defined(&{$clase . '::SINGLETON'})); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | |||||||||||
| 15 | 8 | my $instance = $clase->new(%args); | |||||||||
| 16 | |||||||||||
| 17 | 8 | ▶ | *{$clase . '::SINGLETON'} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | return $instance; | |||||||||
| 20 | |||||||||||
| 21 | 8 | }; | |||||||||
| 22 | |||||||||||
| 23 | 8 | ▶ | *{$clase . '::AUTOLOAD'} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||
| 24 | |||||||||||
| 25 | 28 | my ($attribute) = our $AUTOLOAD =~ /\:(\w+)$/; | |||||||||
| 26 | |||||||||||
| 27 | 28 | ▶ | if(my $method = $instance->can('__' . $attribute)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | |||||||||||
| 29 | 28 | $instance->$method(@_[1..$#_]); | |||||||||
| 30 | |||||||||||
| 31 | } | ||||||||||
| 32 | else{ | ||||||||||
| 33 | 0 | die($AUTOLOAD . ' method not found'); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 8 | }; | |||||||||
| 37 | |||||||||||
| 38 | 8 | ▶ | if($instance->can('initialize')){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 8 | $instance->initialize(); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 8 | ▶ | if( ! $instance->can("DESTROY")){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 5 | ▶ | *{$clase . '::DESTROY'} = sub {} | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 5 | } | |||||||||
| 48 | |||||||||||
| 49 | 8 | $instance; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 10 | ▶ | sub new{ | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 53 | 10 | my ($class, @args) = @_; | |||||||||
| 54 | |||||||||||
| 55 | 10 | ▶ | my $self = ($class->can('SINGLETON')) ? $class->SINGLETON : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 10 | ▶ | if($self){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 2 | $self->__chainInitialize; | |||||||||
| 59 | |||||||||||
| 60 | 2 | $self->initialize(@args); | |||||||||
| 61 | } | ||||||||||
| 62 | else{ | ||||||||||
| 63 | 8 | $self = bless({}, $class); | |||||||||
| 64 | |||||||||||
| 65 | 8 | $self->__chainInitialize; | |||||||||
| 66 | |||||||||||
| 67 | 8 | ▶ | $self->__initialize if($self->can('__initialize')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 10 | $self; | |||||||||
| 73 | |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 21 | ▶ | sub __createSetterGetter{ | ||||||||
|
Subroutine
__createSetterGetter: called CC=2 SCAR=6.9
| |||||||||||
| 79 | 21 | my ($class, $attribute, $value) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 4 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | |||||||||||
| 83 | 21 | ▶ | unless(defined(&{$class . '::__' . $attribute})){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 21 | ▶ | *{$class . '::__' . $attribute} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 86 | |||||||||||
| 87 | 28 | my ($self, $value) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 28 | ▶ | if(defined($value)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 7 | $self->{$attribute} = $value; | |||||||||
| 92 | |||||||||||
| 93 | 7 | $self; | |||||||||
| 94 | } | ||||||||||
| 95 | else{ | ||||||||||
| 96 | |||||||||||
| 97 | 21 | $self->{$attribute}; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 21 | }; | |||||||||
| 101 | |||||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | 4 | 1; | |||||||||