blib/lib/Class/Decorator.pm

filter: Statementstmt 73.0% 73 / 100 Branchbran 36.6% 11 / 30 Conditioncond 61.5% 8 / 13 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 62.5% 105 / 168 CC 21 SCAR 16.5
CC
21
Cov
64%
VERSION
7.3
isa
7.3
can
7.3
CRAP 5.2
↑ Summary
1 package Class::Decorator;
2 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw ( $VERSION $METH $METHOD $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 $VERSION = '0.99';
7  
8 sub new
9 5 ▶{
Subroutine new: called CC=6 SCAR=19.6
Pod undocumented
10 5 my ($caller, %args) = @_;
11 5 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
12 bless {
13 10 ▶ pre => $args{pre} || sub {}, # performed before dispatched method
Subroutine __ANON__: called CC=1 SCAR=0.0
14 10 ▶ post => $args{post} || sub {}, # performed after dispatched method
Subroutine __ANON__: called CC=1 SCAR=0.0
15 obj => $args{obj} || croak("decorator must be constructed with a component to be decorated"),
16 methods => $args{methods} || {}
17 5 ▶ }, $class;
Condition70%7/10
$args{'pre'} || sub { }
l!l&&r!l&&!r
320
$args{'post'} || sub { }
l!l&&r!l&&!r
320
$args{'obj'} || croak('decorator must be constructed with a component to be decorated')
l!l
50
$args{'methods'} || {}
l!l
14
MC/DC0%0/2
$args{'pre'} || sub { }
$args{'pre'} sub { }
MC/DC0%0/2
$args{'post'} || sub { }
$args{'post'} sub { }
MC/DC0%0/1
$args{'obj'} || croak('decorator must be constructed with a component to be decorated')
$args{'obj'}
MC/DC100%1/1
$args{'methods'} || {}
$args{'methods'}
Truth table66%2/3
A || B
A= $args{'pre'}B= sub { }
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $args{'post'}B= sub { }
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'obj'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'methods'}
Aresult
11
00
18 }
19  
20 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
21  
22 sub VERSION
23 2 ▶{
Subroutine VERSION: called CC=4 SCAR=19.9
Pod undocumented
24 2 my ($self, @args) = @_;
25 2 my ($pre, $post) = ($self->{pre}, $self->{post});
26
27 2 ▶ if (exists ${$self->{methods}}{VERSION}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"} }->{"VERSION"}) 0 2 2
28 0 ▶ if (exists ${$self->{methods}->{VERSION}}{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"VERSION"} }->{"pre"}) 0 0 0
29 0 $pre = ${$self->{methods}->{VERSION}}{pre};
30 }
31 0 ▶ if (exists ${$self->{methods}->{VERSION}}{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"VERSION"} }->{"post"}) 0 0 0
32 0 $post = ${$self->{methods}->{VERSION}}{post};
33 }
34 }
35
36 2 $pre->(@args);
37 2 my $return_value = $self->{obj}->VERSION(@args);
38 2 $post->(@args);
39 2 return $return_value;
40 }
41  
42 sub isa
43 4 ▶{
Subroutine isa: called CC=4 SCAR=19.9
Pod undocumented
44 4 my ($self, @args) = @_;
45 4 my ($pre, $post) = ($self->{pre}, $self->{post});
46
47 4 ▶ if (exists ${$self->{methods}}{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"} }->{"isa"}) 0 4 4
48 0 ▶ if (exists ${$self->{methods}->{isa}}{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"isa"} }->{"pre"}) 0 0 0
49 0 $pre = ${$self->{methods}->{isa}}{pre};
50 }
51 0 ▶ if (exists ${$self->{methods}->{isa}}{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"isa"} }->{"post"}) 0 0 0
52 0 $post = ${$self->{methods}->{isa}}{post};
53 }
54 }
55
56 4 $pre->(@args);
57 4 my $return_value = $self->{obj}->isa(@args);
58 4 $post->(@args);
59 4 return $return_value;
60 }
61  
62 sub can
63 4 ▶{
Subroutine can: called CC=4 SCAR=19.9
Pod undocumented
64 4 my ($self, @args) = @_;
65 4 my ($pre, $post) = ($self->{pre}, $self->{post});
66
67 4 ▶ if (exists ${$self->{methods}}{can}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"} }->{"can"}) 0 4 4
68 0 ▶ if (exists ${$self->{methods}->{can}}{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"can"} }->{"pre"}) 0 0 0
69 0 $pre = ${$self->{methods}->{can}}{pre};
70 }
71 0 ▶ if (exists ${$self->{methods}->{can}}{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$self{"methods"}{"can"} }->{"post"}) 0 0 0
72 0 $post = ${$self->{methods}->{can}}{post};
73 }
74 }
75
76 4 $pre->(@args);
77 4 my $return_value = $self->{obj}->can(@args);
78 4 $post->(@args);
79 4 return $return_value;
80 }
81  
82 sub AUTOLOAD
83 1 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
84 1 my ($self, @args) = @_;
85  
86 # check to see whether method name is of form Foo::Bar::Baz
87 1 ▶ if ($AUTOLOAD =~ /.+::(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Decorator::AUTOLOAD =~ /.+::(.+)$/) { } 1 0 1
88 1 $METHOD = $METH = $1; # $METH for backward compaitibility (v0.01)
89 } else {
90 0 die("cannot find method name");
91 }
92  
93 1 my $dispatch = $self->{obj}->can($METHOD);
94  
95 ############################
96 # construct the subroutine #
97 ############################
98 12 ▶ my $sub = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
99 12 my ($decorator, @args) = @_;
100 12 my ($pre, $post) = ($decorator->{pre}, $decorator->{post});
101 12 ▶ if (exists ${$decorator->{methods}}{$METHOD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$decorator{"methods"} }->{$Class::Decorator::METHOD}) 1 11 12
102 1 ▶ if (exists ${$decorator->{methods}->{$METHOD}}{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$decorator{"methods"}{$Class::Decorator::METHOD} }->{"pre"}) 1 0 1
103 1 $pre = ${$decorator->{methods}->{$METHOD}}{pre};
104 }
105 1 ▶ if (exists ${$decorator->{methods}->{$METHOD}}{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $$decorator{"methods"}{$Class::Decorator::METHOD} }->{"post"}) 1 0 1
106 1 $post = ${$decorator->{methods}->{$METHOD}}{post};
107 }
108 }
109  
110 12 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 7 5 12
111 7 () = $pre->(@args);
112 7 my @return_values = $decorator->{obj}->$METHOD(@args);
113 7 () = $post->(@args);
114 7 return @return_values;
115 } else {
116 5 $pre->(@args);
117 5 my $return_value = $decorator->{obj}->$METHOD(@args);
118 5 $post->(@args);
119 5 return $return_value;
120 }
121 1 };
122
123 ###########################
124 # load the subroutine #
125 ###########################
126 {
127 1 ▶ no strict "refs"; # keep following line happy
Subroutine BEGIN: called CC=1 SCAR=0.0
128 1 *{$AUTOLOAD} = $sub;
129 }
130
131 ############################
132 # call the subroutine #
133 ############################
134 1 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 1 1
135 0 my @return_values = $sub->($self, @args);
136 0 return @return_values;
137 } else {
138 1 my $return_value = $sub->($self, @args);
139 1 return $return_value;
140 }
141 }
142  
143 1 1;
144 __END__
↑ Summary