Framework/Extension.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 2 / 2 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 5 / 5 totaltotal 93.0% 80 / 86 CC 8 SCAR 8.5
CC
8
Cov
96%
heap
3.0
extend_fn
3.0
call_extend_fn
3.0
CRAP 2.3
« Framework/Core/Script.pm ↑ Summary Framework/Feature.pm »
1 package App::Framework::Extension ;
2  
3 =head1 NAME
4  
5 App::Framework::Extension - Application Extension
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework::Extension ;
10  
11  
12 =head1 DESCRIPTION
13  
14 Provides the base object from which all Extensions must be derived. Is itself derived from L<App::Framework::Core> and overrides
15 whichever methods are necessary to modify the application behaviour.
16  
17  
18 =cut
19  
20 31 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 31 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 31 our $VERSION = "1.000" ;
24  
25 #============================================================================================
26 # USES
27 #============================================================================================
28 31 ▶use App::Framework::Core ;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #============================================================================================
31 # OBJECT HIERARCHY
32 #============================================================================================
33 #our @ISA = qw(App::Framework::Core) ;
34 31 our @ISA ;
35  
36 #============================================================================================
37 # GLOBALS
38 #============================================================================================
39  
40 =head2 FIELDS
41  
42 The following fields should be defined either in the call to 'new()', as part of a 'set()' call, or called by their accessor method
43 (which is the same name as the field):
44  
45  
46 =over 4
47  
48  
49 =back
50  
51 =cut
52  
53  
54 31 my %FIELDS = (
55 'extension_heap' => {}, # Extension-specific heap
56 );
57  
58 #============================================================================================
59  
60 =head2 CONSTRUCTOR
61  
62 =over 4
63  
64 =cut
65  
66 #============================================================================================
67  
68 =item B< new([%args]) >
69  
70 Create a new Extension.
71  
72 The %args are specified as they would be in the B<set> method.
73  
74 =cut
75  
76 sub new
77 31 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod documented
78 31 my ($obj, %args) = @_ ;
79  
80 31 ▶ my $class = ref($obj) || $obj ;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
0310
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
81  
82 #print "App::Framework::Extension->new() class=$class\n" ;
83  
84 ## Inherit from specified list
85 31 my $this = App::Framework::Core->inherit($class, %args) ;
86  
87 31 $this->_dbg_prt(["Extension - $class ISA=@ISA\n"]) ;
88  
89 # Create object
90 # my $this = $class->SUPER::new(%args) ;
91  
92 #$this->debug(1) ;
93 #print "App::Framework::Extension->new() - END\n" ;
94
95 31 return($this) ;
96 }
97  
98  
99  
100 #============================================================================================
101  
102 =back
103  
104 =head2 CLASS METHODS
105  
106 =over 4
107  
108 =cut
109  
110 #============================================================================================
111  
112 #-----------------------------------------------------------------------------
113  
114 =item B< init_class([%args]) >
115  
116 Initialises the object class variables.
117  
118 =cut
119  
120 sub init_class
121 31 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
122 31 my $class = shift ;
123 31 my (%args) = @_ ;
124  
125 # Add extra fields
126 31 $class->add_fields(\%FIELDS, \%args) ;
127  
128 # init class
129 31 $class->SUPER::init_class(%args) ;
130  
131 }
132  
133  
134 #============================================================================================
135  
136 =back
137  
138 =head2 OBJECT METHODS
139  
140 =over 4
141  
142 =cut
143  
144 #============================================================================================
145  
146 #----------------------------------------------------------------------------
147  
148 =item B<heap([$level])>
149  
150 Returns HEAP space for the calling module
151
152 =cut
153  
154  
155 sub heap
156 434 ▶{
Subroutine heap: called CC=3 SCAR=11.0
Pod documented
157 434 my $this = shift ;
158 434 my ($level) = @_ ;
159  
160 ## Get calling package
161 434 ▶ $level ||= 0 ;
Condition50%1/2
$level ||= 0
l!l
4340
MC/DC0%0/1
$level ||= 0
$level
Truth table50%1/2
A
A= $level
Aresult
11
00
162 434 my $pkg = (caller($level))[0] ;
163  
164 #print "##!!## heap($pkg)\n" ;
165 #$this->dump_callstack() ;
166  
167 # Get total heap space
168 434 my $heap = $this->extension_heap() ;
169  
170 # Return this package's area
171 434 ▶ $heap->{$pkg} ||= {} ;
Condition100%2/2
$heap->{$pkg} ||= {}
l!l
4286
MC/DC100%1/1
$heap->{$pkg} ||= {}
$heap->{$pkg}
Truth table100%2/2
A
A= $heap->{$pkg}
Aresult
11
00
172 434 $this->_dbg_prt(["#!# this=$this pkg=$pkg Heap [$heap->{$pkg}] Total heap [$heap]=", $heap]) ; ;
173  
174 434 return $heap->{$pkg} ;
175 }
176  
177  
178 # TODO: Specify fn(s) as method name strings that get called on this
179  
180 #----------------------------------------------------------------------------
181  
182 =item B<extend_fn(%spec)>
183  
184 Hi-jack the specified application function. %spec is a HASH of:
185  
186 key = function name
187 value = CODE ref to subroutine
188
189 =cut
190  
191  
192 sub extend_fn
193 6 ▶{
Subroutine extend_fn: called CC=3 SCAR=11.0
Pod documented
194 6 my $this = shift ;
195 6 my (%spec) = @_ ;
196  
197 #$this->debug(2);
198 6 my $pkg = (caller(0))[0] ;
199 6 $this->_dbg_prt(["#!# extend_fn() pkg=$pkg (this=$this)", \%spec]) ; ;
200
201 6 my $heap = $this->heap(1) ;
202 6 $this->_dbg_prt(["#!# heap [$heap]", $heap]) ; ;
203 6 foreach my $fn (keys %spec)
204 {
205 # save original
206 18 $heap->{'extend_fn'}{$fn} = $this->$fn ;
207 {
208 18 ▶my $saved = $heap->{'extend_fn'}{$fn} || "" ;
Condition100%2/2
$heap->{'extend_fn'}{$fn} || ''
l!l
711
MC/DC100%1/1
$heap->{'extend_fn'}{$fn} || ''
$heap->{'extend_fn'}{$fn}
Truth table100%2/2
A
A= $heap->{'extend_fn'}{$fn}
Aresult
11
00
209 18 $this->_dbg_prt(["#!# + pkg=$pkg Extend $fn - saved ($saved), new $fn=($spec{$fn})\n"]) ;
210 }
211
212 # update function
213 18 $this->$fn($spec{$fn}) ;
214
215 }
216 6 $this->_dbg_prt(["#!# extend_fn() - END", "HEAP=", $heap]) ; ;
217  
218 }
219  
220 #----------------------------------------------------------------------------
221  
222 =item B<call_extend_fn($fn, @args)>
223  
224 Calls the function with specified args. If not extended by the extension then just calls the
225 default function.
226  
227 NOTE: Application function is always called with:
228  
229 fn($app, \%options, @args)
230
231 =cut
232  
233  
234 sub call_extend_fn
235 428 ▶{
Subroutine call_extend_fn: called CC=3 SCAR=11.0
Pod documented
236 428 my $this = shift ;
237 428 my ($fn, @args) = @_ ;
238  
239 428 my $heap = $this->heap(1) ;
240 428 my $call = $heap->{'extend_fn'}{$fn} ;
241 #$this->debug(2);
242 428 my $pkg = (caller(0))[0] ;
243 428 ▶my $dbg_call = $call||'' ;
Condition100%2/2
$call || ''
l!l
41216
MC/DC100%1/1
$call || ''
$call
Truth table100%2/2
A
A= $call
Aresult
11
00
244 428 $this->_dbg_prt(["#!# pkg=$pkg call_extend_fn($fn) call=$dbg_call HEAP [$heap]=", $heap]) ; ;
245  
246 # do call if specified
247 428 ▶ if ($call)
Branch: 1 branch
BranchTrueFalseTotal
if ($call) 412 16 428
248 {
249 # get options
250 412 my %options = $this->options() ;
251  
252 412 $this->_dbg_prt(["#!# + pkg=$pkg calling $fn call=$call\n"]) ;
253
254 # do call
255 412 &$call($this, \%options, @args) ;
256
257 }
258
259 }
260  
261 # ============================================================================================
262 # END OF PACKAGE
263  
264 =back
265  
266 =head1 DIAGNOSTICS
267  
268 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
269  
270 =head1 AUTHOR
271  
272 Steve Price C<< <sdprice at cpan.org> >>
273  
274 =head1 BUGS
275  
276 None that I know of!
277  
278 =cut
279  
280 31 1;
281  
282 __END__
« Framework/Core/Script.pm ↑ Summary Framework/Feature.pm »