GX/Callback/Hook.pm

filter: Statementstmt 94.2% 65 / 69 Branchbran 80.0% 16 / 20 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 84.3% 108 / 128 CC 15 SCAR 17.2
CC
15
Cov
86%
add
11.5
remove
3.2
new
3.1
CRAP 5.6
« GX/Class/Object.pm ↑ Summary GX/Exception/StackTrace/Frame.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Callback/Hook.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Callback::Hook;
7  
8 60 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 60 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # ----------------------------------------------------------------------------------------------------------------------
14 # Constants
15 # ----------------------------------------------------------------------------------------------------------------------
16  
17 60 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 60 PRIORITY_HIGHEST => 0,
19 PRIORITY_HIGH => 2,
20 PRIORITY_NORMAL => 4,
21 PRIORITY_LOW => 6,
22 PRIORITY_LOWEST => 8
23 60 };
24  
25  
26 # ----------------------------------------------------------------------------------------------------------------------
27 # Class setup
28 # ----------------------------------------------------------------------------------------------------------------------
29  
30 60 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 60 has 'name' => (
33 isa => 'String',
34 initialize => 1,
35 accessor => { type => 'get' }
36 );
37  
38 60 has 'callbacks' => (
39 isa => 'Array',
40 initialize => 1,
41 accessor => undef
42 );
43  
44 60 build;
45  
46  
47 # ----------------------------------------------------------------------------------------------------------------------
48 # Overloading
49 # ----------------------------------------------------------------------------------------------------------------------
50  
51 use overload
52 29 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
53 80 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 39 ▶ '""' => sub { $_[0]->{'name'} },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 1 ▶ '@{}' => sub { [ $_[0]->all ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 60 'fallback' => 1;
57  
58  
59 # ----------------------------------------------------------------------------------------------------------------------
60 # Constructor
61 # ----------------------------------------------------------------------------------------------------------------------
62  
63 29 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
64  
65 29 my $class = shift;
66  
67 29 ▶ return eval { $class->SUPER::new( @_ == 1 ? ( name => $_[0] ) : @_ ) } || complain $@;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2 27 29
Condition33%1/3
eval { do { $class->SUPER::new(@_ == 1 ? ('name', $_[0]) : @_) } } || &complain($@)
l!l&&r!l&&!r
2900
MC/DC0%0/2
eval { do { $class->SUPER::new(@_ == 1 ? ('name', $_[0]) : @_) } } || &complain($@)
eval { do { $class->SUPER::new(@_ == 1 ? ('name', $_[0]) : @_) } } &complain($@)
Truth table33%1/3
A || B
A= eval { do { $class->SUPER::new(@_ == 1 ? ('name', $_[0]) : @_) } }B= &complain($@)
ABresult
1X1
011
000
68  
69 }
70  
71  
72 # ----------------------------------------------------------------------------------------------------------------------
73 # Public methods
74 # ----------------------------------------------------------------------------------------------------------------------
75  
76 90 ▶sub add {
Subroutine add: called CC=11 SCAR=24.4
Pod documented
77  
78 90 my $self = shift;
79 90 my $callback = shift;
80 90 my $priority = shift;
81  
82 90 ▶ if ( ! blessed $callback || ! $callback->isa( 'GX::Callback' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($callback) and $callback->isa("GX::Callback")) 0 90 90
Condition33%1/3
builtin::blessed($callback) and $callback->isa("GX::Callback")
!ll&&!rl&&r
0090
MC/DC0%0/2
builtin::blessed($callback) and $callback->isa("GX::Callback")
builtin::blessed($callback) $callback->isa("GX::Callback")
Truth table33%1/3
A and B
A= builtin::blessed($callback)B= $callback->isa("GX::Callback")
ABresult
0X0
100
111
83 0 complain "Invalid argument";
84 }
85  
86 90 ▶ if ( defined $priority ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $priority) { } 45 45 90
87  
88 45 ▶ if ( $priority !~ /\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($priority =~ /\d+/) 0 45 45
89 0 complain "Invalid argument";
90 }
91  
92 }
93 else {
94 45 $priority = PRIORITY_NORMAL;
95 }
96  
97 90 my $callbacks = $self->{'callbacks'};
98  
99 90 my $position;
100  
101 90 ▶ if ( ! @$callbacks || $priority < $callbacks->[0][1] ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not @$callbacks or $priority < $callbacks->[0][1]) { } 28 62 90
elsif ($priority >= $callbacks->[-1][1]) { } 45 17 62
elsif (@$callbacks == 2) { } 0 17 17
Condition100%3/3
not @$callbacks or $priority < $callbacks->[0][1]
l!l&&r!l&&!r
171162
MC/DC100%2/2
not @$callbacks or $priority < $callbacks->[0][1]
not @$callbacks $priority < $callbacks->[0][1]
Truth table100%3/3
not(A) or B
A= not @$callbacksB= $priority < $callbacks->[0][1]
ABresult
1X1
011
000
102 28 $position = 0;
103 }
104 elsif ( $priority >= $callbacks->[-1][1] ) {
105 45 $position = @$callbacks;
106 }
107 elsif ( @$callbacks == 2 ) {
108 0 $position = 1;
109 }
110 else {
111  
112 17 my $min = 0;
113 17 my $max = $#$callbacks;
114  
115 17 while ( 1 ) {
116  
117 64 my $mid = int( ( $min + $max ) / 2 );
118  
119 64 ▶ if ( $priority < $callbacks->[$mid][1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($priority < $callbacks->[$mid][1]) { } 33 31 64
120 33 $max = $mid - 1;
121 }
122 else {
123 31 $min = $mid + 1;
124 }
125  
126 64 ▶ if ( $max < $min ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $min) 17 47 64
127 17 $position = $min;
128 17 last;
129 }
130  
131 }
132  
133 }
134  
135 90 splice( @$callbacks, $position, 0, [ $callback, $priority ] );
136  
137 90 return;
138  
139 }
140  
141 94 ▶sub all {
Subroutine all: called CC=1 SCAR=0.0
Pod documented
142  
143 94 return map { $_->[0] } @{$_[0]->{'callbacks'}};
144  
145 }
146  
147 5 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.7
Pod documented
148  
149 5 my $self = shift;
150 5 my $callback = shift;
151  
152 5 ▶ if ( ! blessed $callback || ! $callback->isa( 'GX::Callback' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($callback) and $callback->isa("GX::Callback")) 0 5 5
Condition33%1/3
builtin::blessed($callback) and $callback->isa("GX::Callback")
!ll&&!rl&&r
005
MC/DC0%0/2
builtin::blessed($callback) and $callback->isa("GX::Callback")
builtin::blessed($callback) $callback->isa("GX::Callback")
Truth table33%1/3
A and B
A= builtin::blessed($callback)B= $callback->isa("GX::Callback")
ABresult
0X0
100
111
153 0 complain "Invalid argument";
154 }
155  
156 5 my $callbacks = $self->{'callbacks'};
157  
158 5 my $count = @$callbacks;
159  
160 5 @$callbacks = grep { $_->[0] != $callback } @$callbacks;
161  
162 5 return $count - @$callbacks;
163  
164 }
165  
166 1 ▶sub remove_all {
Subroutine remove_all: called CC=1 SCAR=0.0
Pod documented
167  
168 1 my $self = shift;
169  
170 1 @{$self->{'callbacks'}} = ();
171  
172 1 return;
173  
174 }
175  
176  
177 60 1;
178  
179 __END__
« GX/Class/Object.pm ↑ Summary GX/Exception/StackTrace/Frame.pm »