CGI/Portable/AppSplitScreen.pm

filter: Statementstmt 53.1% 25 / 47 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 5 / 8 Podpod 100% 3 / 3 totaltotal 47.8% 33 / 69 CC 6 SCAR 19.2
CC
6
Cov
45%
main
12.0
set_split_screen_attach_delegate_list
12.0
set_split_screen_attach_one_delegate
6.0
CRAP 6.8
« CGI/Portable/AppMultiScreen.pm ↑ Summary DemoRedirect.pm »
1 =head1 NAME
2  
3 CGI::Portable::AppSplitScreen - Delegate construction of a screen between several modules
4  
5 =cut
6  
7 ######################################################################
8  
9 package CGI::Portable::AppSplitScreen;
10 1 require 5.004;
11  
12 # Copyright (c) 1999-2004, Darren R. Duncan. All rights reserved. This module
13 # is free software; you can redistribute it and/or modify it under the same terms
14 # as Perl itself. However, I do request that this copyright information and
15 # credits remain attached to the file. If you modify this module and
16 # redistribute a changed version then please attach a note listing the
17 # modifications. This module is available "as-is" and the author can not be held
18 # accountable for any problems resulting from its use.
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 $VERSION = '0.50';
24  
25 ######################################################################
26  
27 =head1 DEPENDENCIES
28  
29 =head2 Perl Version
30  
31 5.004
32  
33 =head2 Standard Modules
34  
35 I<none>
36  
37 =head2 Nonstandard Modules
38  
39 CGI::Portable 0.50
40 CGI::Portable::AppStatic 0.50 (a superclass)
41  
42 =cut
43  
44 ######################################################################
45  
46 1 ▶use CGI::Portable 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use CGI::Portable::AppStatic 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 @ISA = qw( CGI::Portable::AppStatic );
49  
50 ######################################################################
51  
52 =head1 SYNOPSIS
53  
54 =head2 Simple program whose output is made by combining several modules:
55  
56 #!/usr/bin/perl
57 use strict;
58 use warnings;
59  
60 require CGI::Portable;
61 my $globals = CGI::Portable->new();
62  
63 use Cwd;
64 $globals->file_path_root( cwd() ); # let us default to current working dir
65 $globals->file_path_delimiter( $^O=~/Mac/i ? ":" : $^O=~/Win/i ? "\\" : "/" );
66  
67 require CGI::Portable::AdapterCGI;
68 my $io = CGI::Portable::AdapterCGI->new();
69 $io->fetch_user_input( $globals );
70  
71 $globals->default_application_title( 'Demo Application' );
72 $globals->default_maintainer_name( 'Tony Simons' );
73 $globals->default_maintainer_email_address( 'tony@aardvark.net' );
74  
75 $globals->current_user_path_level( 1 );
76 $globals->set_prefs( 'myconfig.pl' );
77 $globals->call_component( 'CGI::Portable::AppSplitScreen' );
78  
79 $io->send_user_output( $globals );
80  
81 1;
82  
83 =head2 Content of file 'myconfig.pl' telling program what to do:
84  
85 I<This is not a whole example, but you can get an idea what to substitute.
86 This example shows 3 screen regions handled by 3 module instances, each of
87 which has their own preferences and optional subdirectory for files.>
88  
89 my $rh_preferences = {
90 delegate_list => [
91 {
92 file_subdir => 'menus',
93 preferences => { key => 'value' },
94 module_name => 'other::module::menu',
95 leave_scalars => 1,
96 replace_lists => 0,
97 },
98 {
99 file_subdir => 'forms',
100 preferences => { key1 => 'value', key2 => 'value' },
101 module_name => 'other::module::form',
102 leave_scalars => 0,
103 replace_lists => 0,
104 },
105 {
106 file_subdir => undef,
107 preferences => {},
108 module_name => 'other::module::disclaim',
109 leave_scalars => 1,
110 replace_lists => 0,
111 },
112 ],
113 };
114  
115 =head1 DESCRIPTION
116  
117 This Perl 5 object class is a simple encapsulated application, or "component",
118 that runs in the CGI::Portable environment. It allows you to easily divide a
119 response screen into multiple regions and then delegate the construction of each
120 region to separate "components". You define the regions within the standard
121 "preferences", and no other input is required. Specifically, you use the
122 "delegate_list" array preference; each array element describes a region, and the
123 created regions appear consecutively in the same order as in the array. Each
124 element says what delegate module to call and what its preferences are. This
125 class is subclassed from CGI::Portable::AppStatic, which is convenient if you
126 want to do simple post-processing of your new screen using its preferences. This
127 module is designed to be easily subclassed by your own application components,
128 should you wish to extend or customize its functionality.
129  
130 =cut
131  
132 ######################################################################
133  
134 # Constant values used by this class:
135  
136 # Keys for items in site page preferences:
137 1 my $PKEY_DELEGATE_LIST = 'delegate_list';
138  
139 # Keys for elements in $PKEY_DELEGATE_LIST hash:
140 1 my $DKEY_FILE_SUBDIR = 'file_subdir';
141 1 my $DKEY_PREFERENCES = 'preferences';
142 1 my $DKEY_MODULE_NAME = 'module_name';
143 1 my $DKEY_LEAVE_SCALARS = 'leave_scalars';
144 1 my $DKEY_REPLACE_LISTS = 'replace_lists';
145  
146 ######################################################################
147  
148 =head1 SYNTAX
149  
150 This class does not export any functions or methods, so you need to call them
151 using object notation. This means using B<Class-E<gt>function()> for functions
152 and B<$object-E<gt>method()> for methods. If you are inheriting this class for
153 your own modules, then that often means something like B<$self-E<gt>method()>.
154  
155 This class does not have any of its own stored properties, so there is no risk
156 of property namespace collisions with subclasses. Instead, you must provide the
157 CGI::Portable object to each method explicitely as its first argument.
158  
159 =head1 THE ONLY PUBLIC METHOD
160  
161 =head2 main( GLOBALS )
162  
163 You invoke this method to run the application component that is encapsulated by
164 this class. The required argument GLOBALS is an CGI::Portable object that
165 you have previously configured to hold the instance settings and user input for
166 this class. When this method returns then the encapsulated application will
167 have finished and you can get its user output from the CGI::Portable object.
168  
169 =cut
170  
171 ######################################################################
172  
173 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
Pod documented
174 0 my ($class, $globals) = @_;
175 0 ▶ my $self = bless( {}, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
176  
177 0 ▶ UNIVERSAL::isa( $globals, 'CGI::Portable' ) or
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($globals, "CGI::Portable") or die "initializer is not a valid CGI::Portable object" 0 0 0
178 die "initializer is not a valid CGI::Portable object";
179  
180 0 $self->set_static_low_replace( $globals );
181  
182 0 $self->set_split_screen_attach_delegate_list( $globals );
183  
184 0 $self->set_static_high_replace( $globals );
185 0 $self->set_static_attach_unordered( $globals );
186 0 $self->set_static_attach_ordered( $globals );
187 0 $self->set_static_search_and_replace( $globals );
188 }
189  
190 ######################################################################
191  
192 =head1 PREFERENCES HANDLED BY THIS MODULE
193  
194 This module is a subclass of CGI::Portable::AppStatic and will handle all of
195 that module's preferences in addition to the new ones discussed below.
196  
197 The only preference specifically for CGI::Portable::AppSplitScreen allows you to
198 define a list of other modules which are all to be called in order to construct a
199 portion of the result screen. AppSplitScreen doesn't normally do any screen
200 construction by itself, but rather handles the logic required to call a list of
201 modules and stitch together their results. The key preference that you need to
202 set is "delegate_list". See the SYNOPSIS for the format of that preference.
203  
204 =head2 delegate_list
205  
206 This array-ref preference is a delegation list which divides the result screen
207 into a list of consecutive regions, and matches each region with an "application
208 component" module instance that will handle the content of that region. The
209 array elements are hash-refs that say what other module to call and what its
210 config preferences are. Any elements that are not hash-refs are ignored.
211  
212 Each of these hash-refs must have values for at least these two keys set:
213 "module_name" and "preferences". Each can optionally have these three keys set:
214 "file_subdir", "leave_scalars", "replace_lists". The value for "module_name" is
215 a scalar having the name of the Perl 5 module to invoke as an "application
216 component" that will make the new result screen. The value for "preferences" can
217 be either a hash-ref having literal preferences for the invoked module, or a
218 scalar having a filename of a Perl file that can be executed to return the same
219 preferences hash ref; it must be an empty hash ref if the module takes no
220 preferences. The value for "file_subdir" is a relative file system path within
221 which the invoked module should look for its files (or config file) if any; if
222 this value is not set then the current file directory is used instead. The
223 values for "leave_scalars" and "replace_lists" are used when stitching the
224 delegates' output together, in order to better control when conflicting outputs
225 have priority over each other (eg: there can be only one document title).
226  
227 Delegated modules operate within a new "context" created by the current
228 CGI::Portable object's make_new_context() method. Within that context,
229 AppSplitScreen does the following: 1. call navigate_file_path() with the
230 "file_subdir" value as its argument; 2. call set_prefs() with the "preferences"
231 value as its argument; 3. call call_component() with the "module_name" value as
232 its argument. Finally, the output held by the inner context is saved using the
233 current CGI::Portable object's take_context_output() method, which takes the
234 "leave_scalars" and "replace_lists" values as its last two optional arguments.
235  
236 =head1 PRIVATE METHODS FOR USE BY SUBCLASSES
237  
238 =head2 set_split_screen_attach_delegate_list( GLOBALS )
239  
240 This method will iterate through all the elements of the "delegate_list" array
241 preference and use each element to construct a consecutive screen region. For
242 each element, the method set_split_screen_attach_delegate_list() is called to
243 process it as a delegate. This method returns without doing anything if there
244 is no "delegate_list" preference or it isn't an array.
245  
246 =cut
247  
248 ######################################################################
249  
250 0 ▶sub set_split_screen_attach_delegate_list {
Subroutine set_split_screen_attach_delegate_list: not called CC=3 SCAR=24.8
Pod documented
251 0 my ($self, $globals) = @_;
252 0 my $delegate_list = $globals->pref( $PKEY_DELEGATE_LIST );
253 0 ▶ ref( $delegate_list ) eq 'ARRAY' or return( 0 );
Branch: 1 branch
BranchTrueFalseTotal
ref $delegate_list eq "ARRAY" or return 0 0 0 0
254 0 foreach my $delegate (@{$delegate_list}) {
255 0 $self->set_split_screen_attach_one_delegate( $globals, $delegate );
256 }
257 }
258  
259 ######################################################################
260  
261 =head2 set_split_screen_attach_one_delegate( GLOBALS, DELEGATE )
262  
263 This method will use its DELEGATE argument to construct a new screen region and
264 append it to any existing response in the GLOBALS context argument. DELEGATE is
265 a hash ref whose values describe how the new screen region is to be made. It's
266 properties are described by the delegate_list preference documentation, above, as
267 DELEGATE can be set as an element in the delegate_list array. This method
268 returns without doing anything if DELEGATE is not a valid hash ref.
269  
270 =cut
271  
272 ######################################################################
273  
274 0 ▶sub set_split_screen_attach_one_delegate {
Subroutine set_split_screen_attach_one_delegate: not called CC=2 SCAR=17.9
Pod documented
275 0 my ($self, $globals, $delegate) = @_;
276 0 ▶ ref( $delegate ) eq 'HASH' or return( 0 );
Branch: 1 branch
BranchTrueFalseTotal
ref $delegate eq "HASH" or return 0 0 0 0
277 0 my $inner_context = $globals->make_new_context();
278 0 $inner_context->navigate_file_path( $delegate->{$DKEY_FILE_SUBDIR} );
279 0 $inner_context->set_prefs( $delegate->{$DKEY_PREFERENCES} );
280 0 $inner_context->call_component( $delegate->{$DKEY_MODULE_NAME} );
281 $globals->take_context_output( $inner_context,
282 0 $delegate->{$DKEY_LEAVE_SCALARS}, $delegate->{$DKEY_REPLACE_LISTS} );
283 }
284  
285 ######################################################################
286  
287 1 1;
288 __END__
« CGI/Portable/AppMultiScreen.pm ↑ Summary DemoRedirect.pm »