| 1 | package EO::delegate; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 4 | ▶ | use EO; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 4 | ▶ | use EO::Class; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 4 | ▶ | use EO::Method; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 4 | our $VERSION = 0.96; | |||||||||||
| 11 | 4 | our $AUTOLOAD; | |||||||||||
| 12 | |||||||||||||
| 13 | 4 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||
| 14 | 4 | my $this = shift; | |||||||||||
| 15 | 4 | my $what = shift; | |||||||||||
| 16 | 4 | my $caller = caller(); | |||||||||||
| 17 | |||||||||||||
| 18 | 4 | my $class = EO::Class->new_with_classname( $caller ); | |||||||||||
| 19 | |||||||||||||
| 20 | ## | ||||||||||||
| 21 | ## create a delegate_error method in the class, this will getset the method | ||||||||||||
| 22 | ## to be called when we need to throw/record an exception | ||||||||||||
| 23 | ## | ||||||||||||
| 24 | 4 | my $error_method = EO::Method->new(); | |||||||||||
| 25 | 4 | $error_method->name('delegate_error'); | |||||||||||
| 26 | $error_method->reference( | ||||||||||||
| 27 | 2 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 28 | 2 | my $self = shift; | |||||||||||
| 29 | 2 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 1 | $self->{ _delegate_error } = shift; | |||||||||||
| 31 | 1 | return $self; | |||||||||||
| 32 | } | ||||||||||||
| 33 | 1 | ▶ | $self->{ _delegate_error } ||= 'throw'; | ||||||||||
|
Condition50%1/2
$self->{"_delegate_error"} ||= "throw"
MC/DC0%0/1
$self->{"_delegate_error"} ||= "throw"
$self->{"_delegate_error"}
Truth table50%1/2
A
A= $self->{"_delegate_error"}
| |||||||||||||
| 34 | 1 | return $self->{ _delegate_error }; | |||||||||||
| 35 | } | ||||||||||||
| 36 | 4 | ); | |||||||||||
| 37 | |||||||||||||
| 38 | ## | ||||||||||||
| 39 | ## create a delegate method in the class, this will getset the delegate | ||||||||||||
| 40 | ## that is to be used | ||||||||||||
| 41 | ## | ||||||||||||
| 42 | 4 | my $delegate_method = EO::Method->new(); | |||||||||||
| 43 | 4 | $delegate_method->name( 'delegate' ); | |||||||||||
| 44 | $delegate_method->reference( | ||||||||||||
| 45 | 28 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 46 | 28 | my $self = shift; | |||||||||||
| 47 | 28 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 8 | $self->{ _delegate_to } = shift; | |||||||||||
| 49 | 8 | return $self; | |||||||||||
| 50 | } | ||||||||||||
| 51 | 20 | return $self->{ _delegate_to }; | |||||||||||
| 52 | } | ||||||||||||
| 53 | 4 | ); | |||||||||||
| 54 | |||||||||||||
| 55 | 4 | my $resolver_method = EO::Method->new(); | |||||||||||
| 56 | 4 | $resolver_method->name( 'AUTOLOAD' ); | |||||||||||
| 57 | $resolver_method->reference( | ||||||||||||
| 58 | 4 | ▶ | sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 59 | 4 | my $self = shift; | |||||||||||
| 60 | 4 | my $meth = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1); | |||||||||||
| 61 | 4 | ▶ | my $delegate = $self->delegate || return undef; | ||||||||||
|
Condition50%1/2
$self->delegate || (return undef)
MC/DC0%0/1
$self->delegate || (return undef)
$self->delegate
Truth table50%1/2
A
A= $self->delegate
| |||||||||||||
| 62 | 4 | ▶ | if (my $sub = $delegate->can($meth)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 3 | $sub->( $self->delegate, @_ ); | |||||||||||
| 64 | } else { | ||||||||||||
| 65 | 1 | my $class = ref($self->delegate); | |||||||||||
| 66 | 1 | my $from_class = ref($self); | |||||||||||
| 67 | 1 | my $text = "Can't locate object method \"$meth\" via package" | |||||||||||
| 68 | . "$class delegated from $from_class"; | ||||||||||||
| 69 | 1 | my $on_error = $self->delegate_error(); | |||||||||||
| 70 | 1 | local($Error::Depth) = $Error::Depth + 1; | |||||||||||
| 71 | 1 | EO::Error::Method::NotFound->$on_error( | |||||||||||
| 72 | text => $text, | ||||||||||||
| 73 | file => __FILE__, | ||||||||||||
| 74 | line => __LINE__ | ||||||||||||
| 75 | ); | ||||||||||||
| 76 | 0 | return undef; | |||||||||||
| 77 | } | ||||||||||||
| 78 | } | ||||||||||||
| 79 | 4 | ); | |||||||||||
| 80 | |||||||||||||
| 81 | 4 | $class->add_method( $error_method ); | |||||||||||
| 82 | 4 | $class->add_method( $delegate_method ); | |||||||||||
| 83 | 4 | $class->add_method( $resolver_method ); | |||||||||||
| 84 | |||||||||||||
| 85 | 4 | ▶ | if (!$caller->can('DESTROY')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 3 | ▶ | $class->add_method( EO::Method->new()->name( 'DESTROY' )->reference( sub {} ) ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||
| 87 | } | ||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 4 | 1; | |||||||||||
| 91 | |||||||||||||
| 92 | __END__ | ||||||||||||