File Coverage

blib/lib/CGI/Application/Generator.pm
Criterion Covered Total %
statement 69 75 92.0
branch 16 20 80.0
condition n/a
subroutine 13 13 100.0
pod 10 10 100.0
total 108 118 91.5


line stmt bran cond sub pod time code
1             package CGI::Application::Generator;
2 3     3   24015 use strict;
  3         8  
  3         111  
3 3     3   17 use warnings;
  3         7  
  3         135  
4              
5             $CGI::Application::Generator::VERSION = '1.0';
6              
7 3     3   7072 use HTML::Template;
  3         68722  
  3         2656  
8              
9              
10              
11             =pod
12              
13             =head1 NAME
14              
15             CGI::Application::Generator - Dynamically build CGI::Application modules
16              
17             =head1 SYNOPSIS
18              
19             use CGI::Application::Generator;
20              
21             # Required methods
22             my $cat = CGI::Application::Generator->new();
23             $cat->package_name('My::Widget::Browser');
24             $cat->start_mode('list_widgets');
25             $cat->run_modes(qw/
26             list_widgets
27             add_widget
28             insert_widget
29             edit_widget
30             update_widget
31             delete_widget
32             /);
33              
34              
35             # Optional methods
36             $cat->base_module('My::CGI::Application');
37             $cat->use_modules(qw/My::DBICreds My::Utilities/);
38             $cat->new_dbh_method('My::DBICreds->new_dbh()');
39             $cat->tmpl_path('Path/To/My/Templates/');
40              
41              
42             # Output-related methods
43             $cat->app_module_tmpl('my_standard_cgiapp.tmpl');
44             $cat->output_app_module();
45              
46              
47             =head1 DESCRIPTION
48              
49              
50             CGI::Application::Generator provides a means by which a CGI::Application
51             module can be created from code, as opposed to being written by hand. The
52             goal of this module is two-fold:
53              
54             1. To ease the creation of new CGI::Application modules.
55             2. To allow standardization of CGI::Application coding
56             styles to be more uniformly applied.
57              
58              
59             It is also the hope of this module that Computer Assisted
60             Software Engineering (CASE) tools will eventually emerge
61             which will allow the development process for web-based applications
62             to be greatly improved. These CASE tools could more easily
63             convert visual notation (such as UML state-transition diagrams)
64             into method calls to this module, thereby creating actual code.
65              
66              
67             B
68              
69             CGI::Application::Generator is intended to create a shell of
70             an application module based on the specification you provide.
71             It will not output a completely functional application without
72             additional coding. It will, however, handle the creation of all
73             the structural parts of your application common to all
74             CGI::Application-based modules.
75              
76             CGI::Application::Generator is not a system for HTML templates.
77             If you're looking for a Perl module which will allow you
78             to separate Perl from HTML then I recommend you download and install
79             HTML::Template.
80              
81              
82             =head1 METHODS
83              
84              
85             =over 4
86              
87             =cut
88              
89              
90              
91              
92              
93             ############################
94             ##### PUBLIC METHODS #####
95             ############################
96              
97              
98             =pod
99              
100             =item new()
101              
102             my $cat = CGI::Application::Generator->new();
103              
104             Instantiate a new CGI::Application::Generator object. This object
105             stores the state of your application module while you build it.
106              
107             =cut
108              
109             sub new {
110 2     2 1 975 my $class = shift;
111              
112 2         43 my $self = {
113             __PACKAGE_NAME => '',
114             __START_MODE => '',
115             __RUN_MODES => [],
116             __BASE_MODULE => 'CGI::Application',
117             __USE_MODULES => [],
118             __NEW_DBH_METHOD => '',
119             __TMPL_PATH => '',
120             __APP_MODULE_TMPL => 'CGI/Application/Generator/app_module.tmpl',
121             __CGI_SCRIPT_TMPL => 'CGI/Application/Generator/cgi_script.tmpl',
122             };
123              
124 2         9 bless($self, $class);
125              
126 2         8 return $self
127             }
128              
129              
130              
131             =pod
132              
133             =item package_name()
134              
135             $cat->package_name('My::Web::Application');
136             my $package_name = $cat->package_name();
137              
138             Set or get the name of the package (Perl module name). This is the
139             package name which will be assigned to your CGI::Application module.
140              
141             =cut
142              
143             sub package_name {
144 4     4 1 177 my $self = shift;
145 4         11 my ($data) = @_;
146              
147 4         11 my $prop_key = '__PACKAGE_NAME';
148              
149             # If data is provided, set it!
150 4 100       20 if (defined($data)) {
151 2         19 $self->{$prop_key} = $data;
152             }
153              
154             # If we've gotten this far, return the value!
155 4         20 return $self->{$prop_key};
156             }
157              
158              
159              
160             =pod
161              
162             =item start_mode()
163              
164             $cat->start_mode('show_form');
165             my $start_mode = $cat->start_mode();
166              
167             Set or get the name of the run-mode which will be assigned
168             as "start mode" in the output CGI::Application module.
169              
170             =cut
171              
172             sub start_mode {
173 4     4 1 20 my $self = shift;
174 4         10 my ($data) = @_;
175              
176 4         9 my $prop_key = '__START_MODE';
177              
178             # If data is provided, set it!
179 4 100       16 if (defined($data)) {
180 2         7 $self->{$prop_key} = $data;
181             }
182              
183             # If we've gotten this far, return the value!
184 4         19 return $self->{$prop_key};
185             }
186              
187              
188              
189             =pod
190              
191             =item run_modes()
192              
193             $cat->run_modes(qw/show_form edit_widget delete_widget/);
194             my @run_modes = $cat->run_modes();
195              
196             Set or get the list of run-modes in your module. This method expects an array (or array-ref).
197              
198             =cut
199              
200             sub run_modes {
201 4     4 1 17 my $self = shift;
202 4         12 my (@data) = ( @_ );
203              
204 4         9 my $prop_key = '__RUN_MODES';
205              
206             # If data is provided, set it!
207 4 100       20 if (@data) {
208 2 50       10 if (ref($data[0]) eq 'ARRAY') {
209             # Copy Array via array-ref
210 0         0 $self->{$prop_key} = [ @{$data[0]} ];
  0         0  
211             } else {
212             # Copy Array
213 2         15 $self->{$prop_key} = [ @data ];
214             }
215             }
216              
217             # If we've gotten this far, return the value!
218 4         9 return @{$self->{$prop_key}};
  4         19  
219             }
220              
221              
222              
223              
224             =pod
225              
226             =item base_module()
227              
228             $cat->base_module('MyCustom::CGIAppBase');
229             my $base_module = $cat->base_module();
230              
231             Set or get the name of the module from which your module will
232             inherit. By default, this will be set to 'CGI::Application'.
233              
234             =cut
235              
236             sub base_module {
237 2     2 1 20 my $self = shift;
238 2         6 my ($data) = @_;
239              
240 2         598 my $prop_key = '__BASE_MODULE';
241              
242             # If data is provided, set it!
243 2 50       9 if (defined($data)) {
244 0         0 $self->{$prop_key} = $data;
245             }
246              
247             # If we've gotten this far, return the value!
248 2         14 return $self->{$prop_key};
249             }
250              
251              
252              
253             =pod
254              
255             =item use_modules()
256              
257             $cat->use_modules(qw/DBI Net::SMTP Data::FormValidator/);
258             my @use_modules = $cat->use_modules();
259              
260             Set or get the list of Perl modules which should be
261             included in your module.
262              
263             =cut
264              
265             sub use_modules {
266 3     3 1 10 my $self = shift;
267 3         7 my (@data) = ( @_ );
268              
269 3         7 my $prop_key = '__USE_MODULES';
270              
271             # If data is provided, set it!
272 3 100       29 if (@data) {
273 1 50       3 if (ref($data[0]) eq 'ARRAY') {
274             # Copy Array via array-ref
275 0         0 $self->{$prop_key} = [ @{$data[0]} ];
  0         0  
276             } else {
277             # Copy Array
278 1         24 $self->{$prop_key} = [ @data ];
279             }
280             }
281              
282             # If we've gotten this far, return the value!
283 3         6 return @{$self->{$prop_key}};
  3         30  
284             }
285              
286              
287              
288             =pod
289              
290             =item new_dbh_method()
291              
292             $cat->new_dbh_method('DBI->connect(DBD::mysql::MyDatabase)');
293             my $new_dbh_method = $cat->new_dbh_method();
294              
295             Set or get the code which creates a new DBI-compatible database
296             handle ($dbh). If specified, the CGI::Application module will
297             automatically call this code at run-time, in the setup() method, to
298             connect to the database and store the database handle object reference
299             in a CGI::Application param, 'DBH'. In teardown() this database
300             handle will be disconnected. Access to the $dbh will be automatically
301             set up in each run-mode method.
302              
303              
304             =cut
305              
306             sub new_dbh_method {
307 3     3 1 774 my $self = shift;
308 3         7 my ($data) = @_;
309              
310 3         6 my $prop_key = '__NEW_DBH_METHOD';
311              
312             # If data is provided, set it!
313 3 100       9 if (defined($data)) {
314 1         3 $self->{$prop_key} = $data;
315             }
316              
317             # If we've gotten this far, return the value!
318 3         30 return $self->{$prop_key};
319             }
320              
321              
322              
323             =pod
324              
325             =item tmpl_path()
326              
327             $cat->tmpl_path('Path/To/My/Templates/');
328             my $tmpl_path = $cat->tmpl_path();
329              
330             Set or get the path in which the templates
331             for your module will be stored.
332              
333             =cut
334              
335             sub tmpl_path {
336 4     4 1 16 my $self = shift;
337 4         9 my ($data) = @_;
338              
339 4         7 my $prop_key = '__TMPL_PATH';
340              
341             # If data is provided, set it!
342 4 100       13 if (defined($data)) {
343 2         8 $self->{$prop_key} = $data;
344             }
345              
346             # If we've gotten this far, return the value!
347 4         23 return $self->{$prop_key};
348             }
349              
350              
351              
352             =pod
353              
354             =item app_module_tmpl()
355              
356             $cat->app_module_tmpl('path/to/my_app_style.tmpl');
357             my $app_module_tmpl = $cat->app_module_tmpl();
358              
359             Set or get the path and filename of the HTML::Template which
360             should be used by CGI::Application::Generator to create
361             your Perl module. By default an internal template will be used.
362             You may implement your own template if you want to have
363             special output for your organization's programming style.
364              
365             =cut
366              
367             sub app_module_tmpl {
368 2     2 1 5 my $self = shift;
369 2         26 my ($data) = @_;
370              
371 2         5 my $prop_key = '__APP_MODULE_TMPL';
372              
373             # If data is provided, set it!
374 2 50       10 if (defined($data)) {
375 0         0 $self->{$prop_key} = $data;
376             }
377              
378             # If we've gotten this far, return the value!
379 2         23 return $self->{$prop_key};
380             }
381              
382              
383              
384             =pod
385              
386             =item output_app_module()
387              
388             my $module_source = $cat->output_app_module();
389              
390             The output_app_module() method returns a scalar containing the
391             source code of the module you've specified. Generally, you would
392             store this output in a file named "MyModule.pm" which would then
393             be used by your instance script.
394              
395             For example, the following code would build the shell of a basic
396             CGI application via CGI::Application::Generator:
397              
398             use CGI::Application::Generator;
399              
400             my $c = CGI::Application::Generator->new();
401             $c->package_name('WidgetBrowser');
402             $c->start_mode('show_form');
403             $c->run_modes(qw/show_form do_search view_details/);
404             $c->use_modules(qw/DBI/);
405             $c->new_dbh_method('DBI->connect("DBD:mysql:WIDGETCORP")');
406             $c->tmpl_path('WidgetBrowser/');
407              
408             open(OUTPUT, ">WidgetBrowser.pm") || die($!);
409             print OUTPUT $c->output_app_module();
410             close(OUTPUT);
411              
412             =cut
413              
414             sub output_app_module {
415 2     2 1 171 my $self = shift;
416              
417 2         11 my $t = HTML::Template->new_file(
418             $self->app_module_tmpl(),
419             global_vars => 1,
420             path => \@INC,
421             );
422              
423 6         24 $t->param(
424             package_name => $self->package_name(),
425             base_module => $self->base_module(),
426             start_mode => $self->start_mode(),
427 1         6 run_modes => [ map { {mode_name=>$_} } ($self->run_modes()) ],
428 2         7205 use_modules => [ map { {module_name=>$_} } ($self->use_modules()) ],
429             tmpl_path => $self->tmpl_path(),
430             new_dbh_method => $self->new_dbh_method(),
431             );
432              
433 2         273 return $t->output();
434             }
435              
436              
437              
438              
439              
440             #############################
441             ##### PRIVATE METHODS #####
442             #############################
443              
444              
445              
446             1;
447              
448              
449              
450              
451              
452              
453             =pod
454              
455             =back
456              
457             =head1 AUTHOR
458              
459             Jesse Erlbaum
460              
461             B
462              
463             If you have any questions, comments, bug reports or feature
464             suggestions, post them to the support mailing list!
465             To join the mailing list, simply send a blank message to
466             "cgiapp-subscribe@lists.erlbaum.net".
467              
468              
469             =head1 SEE ALSO
470              
471             L, L, L
472              
473              
474             =head1 LICENSE
475              
476             CGI::Application::Generator -
477             Dynamically build CGI::Application modules from template
478             Copyright (C) 2003 Jesse Erlbaum
479              
480             This module is free software; you can redistribute it
481             and/or modify it under the terms of either:
482              
483             a) the GNU General Public License as published by the Free
484             Software Foundation; either version 1, or (at your option)
485             any later version,
486              
487             or
488              
489             b) the "Artistic License" which comes with this module.
490              
491             This program is distributed in the hope that it will be
492             useful, but WITHOUT ANY WARRANTY; without even the implied
493             warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
494             PURPOSE. See either the GNU General Public License or the
495             Artistic License for more details.
496              
497             You should have received a copy of the Artistic License
498             with this module, in the file ARTISTIC. If not, I'll be
499             glad to provide one.
500              
501             You should have received a copy of the GNU General Public
502             License along with this program; if not, write to the Free
503             Software Foundation, Inc., 59 Temple Place, Suite 330,
504             Boston, MA 02111-1307 USA
505              
506             =cut