blib/lib/Class/Method/Delegate.pm

filter: Statementstmt 84.6% 55 / 65 Branchbran 36.1% 13 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 69.9% 79 / 113 CC 20 SCAR 33.1
CC
20
Cov
67%
delegate
44.5
__ANON__
3.1
BEGIN
1.0
CRAP 27.3
↑ Summary
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
BranchTrueFalseTotal
@_ ? : 1 0 1
@_ > 1 ? : 1 0 1
25 1 my ($methods, $object, $handlers);
26  
27 1 ▶ if (exists $options->{'methods'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'methods'}) { } 1 0 1
28 1 $methods = $options->{'methods'};
29 1 ▶ croak 'methods undefined' unless $methods;
Branch: 1 branch
BranchTrueFalseTotal
unless $methods 0 1 1
30 1 ▶ croak 'methods not arrayref' unless ref($methods) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $methods eq "ARRAY" 0 1 1
31 1 ▶ croak 'methods empty' if 0 == @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == @$methods 0 1 1
32 } else {
33 0 croak "Can't delegate without methods";
34 }
35 1 ▶ if (exists $options->{'to'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'to'}) { } 1 0 1
36 1 $object = $options->{'to'};
37 1 ▶ croak 'to undefined' unless $object;
Branch: 1 branch
BranchTrueFalseTotal
unless $object 0 1 1
38 1 ▶ croak 'to not coderef' unless ref($object) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object eq "CODE" 0 1 1
39 } else {
40 0 croak "Can't delegate without an object";
41 }
42 1 ▶ if (exists $options->{'class'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"class"}) 0 1 1
43 0 $class = $options->{'class'};
44 0 ▶ croak 'class undefined or empty' unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
45 }
46 1 ▶ if (exists $options->{'handlers'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'handlers'}) { } 0 1 1
47 0 $handlers = $options->{'handlers'};
48 0 ▶ croak 'handlers undefined' unless $handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless $handlers 0 0 0
49 0 ▶ croak 'handlers not arrayref' unless ref($handlers) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $handlers eq "ARRAY" 0 0 0
50 0 ▶ croak 'too few handlers' if @$handlers < @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if @$handlers < @$methods 0 0 0
51 0 ▶ croak 'too many handlers' if @$handlers > @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if @$handlers > @$methods 0 0 0
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
BranchTrueFalseTotal
unless builtin::blessed($delegation_object) 0 3 3
65 3 ▶ if($delegation_object->can('delegated_by')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delegation_object->can("delegated_by")) 3 0 3
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__
↑ Summary