| 1 | package Class::Method::Delegate; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.010000; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | our $VERSION = '1.03'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | my $class = shift; | |||||||||||||
| 13 | 1 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 2 | my $caller = caller; | |||||||||||||
| 16 | # Wants delegate | ||||||||||||||
| 17 | 2 | ▶ | *{"${caller}::delegate"} = sub { delegate($caller, @_) }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 2 | strict->import; | |||||||||||||
| 20 | } | ||||||||||||||
| 21 | |||||||||||||||
| 22 | 1 | ▶ | sub delegate { | ||||||||||||
|
Subroutine
delegate: called CC=18 SCAR=38.0
Pod
undocumented
| |||||||||||||||
| 23 | 1 | my $class = shift; | |||||||||||||
| 24 | 0 | ▶ | my $options = @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 25 | 1 | my ($methods, $object, $handlers); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | if (exists $options->{'methods'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 1 | $methods = $options->{'methods'}; | |||||||||||||
| 29 | 1 | ▶ | croak 'methods undefined' unless $methods; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 1 | ▶ | croak 'methods not arrayref' unless ref($methods) eq 'ARRAY'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 1 | ▶ | croak 'methods empty' if 0 == @$methods; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } else { | ||||||||||||||
| 33 | 0 | croak "Can't delegate without methods"; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | 1 | ▶ | if (exists $options->{'to'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 1 | $object = $options->{'to'}; | |||||||||||||
| 37 | 1 | ▶ | croak 'to undefined' unless $object; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 1 | ▶ | croak 'to not coderef' unless ref($object) eq 'CODE'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | } else { | ||||||||||||||
| 40 | 0 | croak "Can't delegate without an object"; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | 1 | ▶ | if (exists $options->{'class'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 0 | $class = $options->{'class'}; | |||||||||||||
| 44 | 0 | ▶ | croak 'class undefined or empty' unless $class; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | } | ||||||||||||||
| 46 | 1 | ▶ | if (exists $options->{'handlers'}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 0 | $handlers = $options->{'handlers'}; | |||||||||||||
| 48 | 0 | ▶ | croak 'handlers undefined' unless $handlers; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | ▶ | croak 'handlers not arrayref' unless ref($handlers) eq 'ARRAY'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 50 | 0 | ▶ | croak 'too few handlers' if @$handlers < @$methods; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | ▶ | croak 'too many handlers' if @$handlers > @$methods; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | } else { | ||||||||||||||
| 53 | 1 | $handlers = $methods; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 1 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | #Inject a method | ||||||||||||||
| 58 | 1 | my $i = 0; | |||||||||||||
| 59 | 1 | for my $method (@$methods) { | |||||||||||||
| 60 | 3 | my $handler = $handlers->[$i++]; | |||||||||||||
| 61 | 3 | ▶ | *{"${class}::$method"} = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||
| 62 | 3 | my $self = shift; | |||||||||||||
| 63 | 3 | my $delegation_object = &$object($self); | |||||||||||||
| 64 | 3 | ▶ | croak "You are trying to delegate to something that is not an object" unless blessed( $delegation_object ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 65 | 3 | ▶ | if($delegation_object->can('delegated_by')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 3 | $delegation_object->delegated_by($self); | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | 3 | $delegation_object->$handler(@_); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 3 | } | |||||||||||||
| 71 | 1 | strict->import; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 1 | 1; | |||||||||||||
| 75 | __END__ | ||||||||||||||