File Coverage

blib/lib/HTML/Template/Associate.pm
Criterion Covered Total %
statement 35 39 89.7
branch 3 6 50.0
condition 1 3 33.3
subroutine 9 11 81.8
pod 4 4 100.0
total 52 63 82.5


line stmt bran cond sub pod time code
1             package HTML::Template::Associate;
2 1     1   25521 use strict;
  1         2  
  1         34  
3              
4             BEGIN {
5 1     1   4 use Exporter ();
  1         2  
  1         16  
6 1     1   4 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         5  
  1         102  
7 1     1   2 $VERSION = '2.01';
8 1         19 @ISA = qw (Exporter);
9             #Give a hoot don't pollute, do not export more than needed by default
10 1         1 @EXPORT = qw ();
11 1         2 @EXPORT_OK = qw ();
12 1         35 %EXPORT_TAGS = ();
13             }
14              
15 1     1   5 use constant ERROR_OBJECT_CREATE => 'Cannot create %s target, make sure this class exists';
  1         2  
  1         76  
16 1     1   5 use constant ERROR_SUB_INIT => 'Sub init not defined in your target class, please provide concrete implementation for it';
  1         1  
  1         52  
17 1     1   5 use constant FIELD_HASH => 'PARAMS';
  1         1  
  1         356  
18              
19             ########################################### main pod documentation begin ##
20              
21             =head1 NAME
22              
23             HTML::Template::Associate - Associate relevant packages with HTML::Template
24              
25             =head1 SYNOPSIS
26              
27             #Example usage with FormValidator as the target
28            
29             use CGI qw/:standard/;
30             use Data::FormValidator;
31             use HTML::Template;
32             use HTML::Template::Associate;
33            
34             my $cgi = CGI->new;
35             #for testing purposes we can add some input to our cgi object
36             $cgi->param( 'fullname', 'John Doe' );
37             $cgi->param( 'phone', 6041112222 );
38             $cgi->param( 'email', 'invalid@email' );
39            
40             my $input_profile = {
41             optional => [ qw( company fax country ) ],
42             required => [ qw( fullname phone email address city state zipcode ) ],
43             constraints => {
44             email => 'email',
45             fax => 'american_phone',
46             phone => 'american_phone',
47             zipcode => '/^\s*\d{5}(?:[-]\d{4})?\s*$/',
48             state => "state",
49             },
50             defaults => { country => "Canada" },
51             msgs => {
52             prefix=> 'error_',
53             missing => 'Not Here!',
54             invalid => 'Problematic!',
55             invalid_seperator => '
',
56             format => 'ERROR: %s',
57             any_errors => 'some_errors',
58             }
59             };
60            
61             my $validator = Data::FormValidator->new;
62             my $results = $validator->check ( scalar $cgi->Vars, $input_profile );
63            
64             my $associate = HTML::Template::Associate->new( {
65             target => 'FormValidator',
66             results => $results,
67             extra_arguments => [ $validator ] #not needed but just illustrated
68             } );
69            
70             my $template = HTML::Template->new(
71             filename => 'test.tmpl',
72             associate => [ $cgi, $associate ]
73             );
74            
75             print $template->output;
76            
77             #and in our test.tmpl file we could have
78            
79             Valid Fields:
80            
81             Field Name:
82             Field Value:
83            
84            
85             Missing Fields:
86            
87             Field Name:
88             Field Value:
89            
90            
91            
92             Phone: you supplied is invalid.
93            
94            
95            
96             City name is missing, please fix this.
97            
98            
99            
101            
102             I think is very big country.
103            
104            
105            
106             Message Fields:
107            
108            
109             Field Name:
110             Field Value:
111            
112            
113            
114             Our default error message set in the profiling code is:
115            
116            
117              
118             #Example usage with DBI as the target
119             use DBI;
120             use HTML::Template;
121             use HTML::Template::Associate;
122            
123             #initiliaze your $dbh ...
124            
125             my $results_foo = $dbh->selectall_hashref (
126             'SELECT foo FROM bar WHERE baz = ?',
127             'foo_id',
128             {},
129             $baz
130             );
131            
132             my $results_bar = $dbh->selectall_hashref (
133             'SELECT foo, bar FROM bar WHERE baz = ?',
134             [ 'foo_id', 'bar_id' ] ,
135             {},
136             $baz
137             );
138            
139             my $results_moo = $dbh->selectrow_hashref ( 'SELECT x, y FROM z LIMIT 1' );
140            
141             my @results_array = $dbh->selectrow_array ( 'SELECT x FROM z' );
142            
143             my $associate = HTML::Template::Associate->new( {
144             target => 'DBI',
145             create => [ {
146             results => $results_foo,
147             name => 'my_loop',
148             type => 'selectall_hashref'
149             }, {
150             results => $results_bar,
151             name => 'my_other_loop',
152             type => 'selectall_hashref'
153             }, {
154             results => $results_moo,
155             type => 'selectrow_hashref',
156             name => 'my_params'
157             }, {
158             results => \@results_array,
159             type => 'selectrow_array',
160             name => 'my_array_params'
161             }
162             ]
163             } );
164            
165             my $template = HTML::Template->new (
166             filename => 'test.tmpl',
167             associate => [ $associate ],
168             die_on_bad_params => 0
169             );
170            
171             print $template->output();
172            
173             #sample.tmpl
174            
175            
176             Foo is:
177            
178            
179            
180             Foo is:
181             Bar is:
182            
183            
184             x is:
185             y is:
186            
187             x via $dbh->selectrow_array is:
188              
189             =head1 DESCRIPTION
190              
191             HTML::Template::Associate bridges gap between HTML::Template and
192             other modules that can be used in conjunction with it to do something
193             useful together, like for example Data::FormValidator that can verify
194             form inputs.
195            
196             The idea is that every associate object can map required data structure
197             onto the one which corresponds to the one found in HTML::Template.
198             The factory will then instantiate the target class and user can then make
199             it available to HTML::Template via associate argument during object
200             construction. The data structures then become automatically visible to
201             your templates.
202            
203             This module is abstract class it provides no mapping functionality
204             whatsoever, but rather defines common interface to all associate
205             objects underneath it and acts as a object production factory.
206             You should however use this module whenever you wish to access a
207             concrete associate class that provides functionality you desire.
208              
209             =head1 USAGE
210              
211             #where $results = Data::FormValidator::Results; for example
212             my $associate = HTML::Template::Associate->new( {
213             target => 'FormValidator',
214             results => $results
215             } );
216              
217             Target is always last portion of your full class name, so if
218             you had HTML::Template::Associate::XYZ the target would be XYZ
219              
220             =head1 BUGS
221              
222             Maybe. If you see any make sure you let me know.
223              
224             =head1 SUPPORT
225              
226              
227             =head1 AUTHOR
228              
229             Alex Pavlovic
230             alex.pavlovic@taskforce-1.com
231             http://www.taskforce-1.com
232              
233             =head1 COPYRIGHT
234              
235             This program is free software; you can redistribute
236             it and/or modify it under the same terms as Perl itself.
237            
238             The full text of the license can be found in the
239             LICENSE file included with this module.
240              
241             =head1 SEE ALSO
242              
243             HTML::Template::Associate::FormValidator HTML::Template::Associate::DBI perl(1).
244              
245             =cut
246              
247             ############################################# main pod documentation end ##
248              
249              
250             ################################################ subroutine header begin ##
251              
252             =head2 new
253              
254             Usage : my $associate = HTML::Template::Associate->new ( $target_arguments );
255             Purpose : Constructs new Associate object
256             Returns : Associate instance
257             Argument : Refer to the target
258             Throws : Error in case target does not exist
259             Comments : None
260              
261             =cut
262              
263             ################################################## subroutine header end ##
264              
265             sub new {
266 1     1 1 18067 shift;
267 1         3 my $params = shift;
268 1         5 my $target = __PACKAGE__ . '::' . $params->{target};
269 1         62 eval "require $target";
270 1 50       7 error( sprintf ( ERROR_OBJECT_CREATE, $target ) . " [$@]" ) if ( $@ );
271 1   33     10 my $self = bless ({}, ref ($target) || $target);
272 1         5 $self->init ( $params );
273 1         4 return ($self);
274             }
275              
276             ################################################ subroutine header begin ##
277              
278             =head2 param
279              
280             Usage : my $MyParam = $associate->param('MyParam');
281             Purpose : Retrieves param in a form suitable for access by HTML::Template
282             Returns : Single param or arrays suitable for loops
283             Argument : Parameter name and optional value if setting it
284             Throws : Error in case subroutine was not implemented in concrete class
285             Comments : This subroutine should be redefined in concrete class
286              
287             =cut
288              
289             ################################################## subroutine header end ##
290              
291             sub param {
292 16     16 1 31 my ( $self, $field, $value ) = @_;
293 16 50       83 $self->{&FIELD_HASH}->{$field} = $value if $value;
294 16 50       34 return keys %{ $self->{&FIELD_HASH} } unless $field;
  0         0  
295 16         56 return $self->{&FIELD_HASH}->{$field};
296             }
297              
298             ################################################ subroutine header begin ##
299              
300             =head2 init
301              
302             Usage : $self->init ( $params );
303             Purpose : Provides basic initiliazation for the target class
304             Returns : true or false depending on whether initilization was succesful
305             Argument : hash of parameters passed to factory during object construction
306             Throws : Error in case subroutine was not implemented in concrete class
307             Comments : This subroutine should be redefined in concrete class
308              
309             =cut
310              
311             ################################################## subroutine header end ##
312              
313              
314 0     0 1   sub init { warn ERROR_SUB_INIT };
315              
316              
317             ################################################ subroutine header begin ##
318              
319             =head2 error
320              
321             Purpose : Used internally to die on errors
322              
323             =cut
324              
325             ################################################## subroutine header end ##
326              
327 0     0 1   sub error { shift; die @_ }
  0            
328              
329             1;
330              
331             __END__