blib/lib/SQL/Translator/Producer/TT/Base.pm

filter: Statementstmt 87.8% 65 / 74 Branchbran 50.0% 8 / 16 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.0% 17 / 20 Podpod 90.9% 10 / 11 totaltotal 78.0% 103 / 132 CC 12 SCAR 11.5
CC
12
Cov
78%
args
8.7
run
4.1
new
3.2
CRAP 3.2
« blib/lib/SQL/Translator/Producer/Latex.pm ↑ Summary blib/lib/SQL/Translator/Schema/Role/Compare.pm »
1 package SQL::Translator::Producer::TT::Base;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SQL::Translator::Producer::TT::Base - TT (Template Toolkit) based Producer base
8 class.
9  
10 =cut
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our @EXPORT_OK;
16 1 our $VERSION = '1.66';
17  
18 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 @EXPORT_OK = qw(produce);
24  
25 1 ▶use SQL::Translator::Utils 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Hack to convert the produce call into an object. ALL sub-classes need todo
28 # this so that the correct class gets created.
29 0 ▶sub produce {
Subroutine produce: not called CC=1 SCAR=6.9
Pod undocumented
30 0 return __PACKAGE__->new(translator => shift)->run;
31 }
32  
33 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod documented
34 1 my $proto = shift;
35 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
36 1 my %args = @_;
37  
38 1 my $me = bless {}, $class;
39 1 ▶ $me->{translator} = delete $args{translator} || die "Need a translator.";
Condition50%1/2
delete $args{'translator'} || die('Need a translator.')
l!l
10
MC/DC0%0/1
delete $args{'translator'} || die('Need a translator.')
delete $args{'translator'}
Truth table50%1/2
A
A= delete $args{'translator'}
Aresult
11
00
40  
41 1 return $me;
42 }
43  
44 2 ▶sub translator { shift->{translator}; }
Subroutine translator: called CC=1 SCAR=0.0
Pod documented
45 1 ▶sub schema { shift->{translator}->schema(@_); }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
46  
47 # Util args access method.
48 # No args - Return hashref (the actual hash in Translator) or hash of args.
49 # 1 arg - Return that named args value.
50 # Args - List of names. Return values of the given arg names in list context
51 # or return as hashref in scalar context. Any names given that don't
52 # exist in the args are returned as undef.
53 2 ▶sub args {
Subroutine args: called CC=5 SCAR=21.6
Pod documented
54 2 my $me = shift;
55  
56 # No args
57 2 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 1 2
58 return wantarray
59 0 ? %{ $me->{translator}->producer_args }
60 1 ▶ : $me->{translator}->producer_args;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
61 }
62  
63 # 1 arg. Return the value whatever the context.
64 1 ▶ return $me->{translator}->producer_args->{ $_[0] } if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 0 1
65  
66 # More args so return values list or hash ref
67 0 my %args = %{ $me->{translator}->producer_args };
68 0 ▶ return wantarray ? @args{@_} : { map { ($_ => $args{$_}) } @_ };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
69 }
70  
71 # Run the produce and return the result.
72 1 ▶sub run {
Subroutine run: called CC=4 SCAR=14.1
Pod documented
73 1 my $me = shift;
74 1 my $scma = $me->schema;
75 1 my %args = %{ $me->args };
76 1 ▶ my $tmpl = $me->tt_schema or die "No template!";
Branch: 1 branch
BranchTrueFalseTotal
my $tmpl = $me->tt_schema or die "No template!" 1 0 1
77  
78 1 debug "Processing template $tmpl\n";
79 1 my $out;
80 1 ▶ my $tt = Template->new(
Condition50%1/2
'Template'->new('ABSOLUTE', 1, 'RELATIVE', 1, $me->tt_config, %args) || die('Failed to initialize Template object: ' . 'Template'->error)
l!l
10
MC/DC0%0/1
'Template'->new('ABSOLUTE', 1, 'RELATIVE', 1, $me->tt_config, %args) || die('Failed to initialize Template object: ' . 'Template'->error)
'Template'->new('ABSOLUTE', 1, 'RELATIVE', 1, $me->tt_config, %args)
Truth table50%1/2
A
A= 'Template'->new('ABSOLUTE', 1, 'RELATIVE', 1, $me->tt_config, %args)
Aresult
11
00
81  
82 #DEBUG => $me->translator->debug,
83 ABSOLUTE => 1, # Set so we can use from the command line sensibly
84 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
85 $me->tt_config, # Hook for sub-classes to add config
86 %args, # Allow any TT opts to be passed in the producer_args
87 ) || die "Failed to initialize Template object: " . Template->error;
88  
89 1 ▶ $tt->process(
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($tmpl, {$me->tt_default_vars, $me->tt_vars}, \$out) or die "Error processing template '${tmpl}': " . $tt->error 1 0 1
90 $tmpl,
91 {
92 $me->tt_default_vars,
93 $me->tt_vars, # Sub-class hook for adding vars
94 },
95 \$out
96 ) or die "Error processing template '$tmpl': " . $tt->error;
97  
98 1 return $out;
99 }
100  
101 # Sub class hooks
102 #-----------------------------------------------------------------------------
103  
104 0 ▶sub tt_config { () }
Subroutine tt_config: not called CC=1 SCAR=6.9
Pod documented
105  
106 1 ▶sub tt_schema {
Subroutine tt_schema: called CC=3 SCAR=11.1
Pod documented
107 1 my $me = shift;
108 1 my $class = ref $me;
109  
110 1 my $file = $me->args("ttfile");
111 1 ▶ return $file if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 1 1
112  
113 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 my $ref = *{"$class\:\:DATA"}{IO};
115 1 ▶ if ($ref->opened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->opened) 1 0 1
116 1 local $/ = undef; # Slurp mode
117 1 return \<$ref>;
118 }
119  
120 0 undef;
121 }
122  
123 1 ▶sub tt_default_vars {
Subroutine tt_default_vars: called CC=1 SCAR=0.0
Pod documented
124 1 my $me = shift;
125 return (
126 1 translator => $me->translator,
127 schema => $me->pre_process_schema($me->translator->schema),
128 );
129 }
130  
131 1 ▶sub pre_process_schema { $_[1] }
Subroutine pre_process_schema: called CC=1 SCAR=0.0
Pod documented
132  
133 0 ▶sub tt_vars { () }
Subroutine tt_vars: not called CC=1 SCAR=6.9
Pod documented
134  
135 1 1;
136  
137 =pod
138  
139 =head1 SYNOPSIS
140  
141 # Create a producer using a template in the __DATA__ section.
142 package SQL::Translator::Producer::Foo;
143  
144 use base qw/SQL::Translator::Producer::TT::Base/;
145  
146 # Convert produce call into a method call on our new class
147 sub produce { return __PACKAGE__->new( translator => shift )->run; };
148  
149 # Configure the Template object.
150 sub tt_config { ( INTERPOLATE => 1 ); }
151  
152 # Extra vars to add to the template
153 sub tt_vars { ( foo => "bar" ); }
154  
155 # Put template in DATA section (or use file with ttfile producer arg)
156 __DATA__
157 Schema
158  
159 Database: [% schema.database %]
160 Foo: $foo
161 ...
162  
163 =head1 DESCRIPTION
164  
165 A base class producer designed to be sub-classed to create new TT based
166 producers cheaply - by simply giving the template to use and sprinkling in some
167 extra template variables and config.
168  
169 You can find an introduction to this module in L<SQL::Translator::Manual>.
170  
171 The 1st thing the module does is convert the produce sub routine call we get
172 from SQL::Translator into a method call on an object, which we can then
173 sub-class. This is done with the following code which needs to appear in B<all>
174 sub classes.
175  
176 # Convert produce call into an object method call
177 sub produce { return __PACKAGE__->new( translator => shift )->run; };
178  
179 See L</PRODUCER OBJECT> below for details.
180  
181 The upshot of this is we can make new template producers by sub classing this
182 base class, adding the above snippet and a template.
183 The module also provides a number of hooks into the templating process,
184 see L</SUB CLASS HOOKS> for details.
185  
186 See the L</SYNOPSIS> above for an example of creating a simple producer using
187 a single template stored in the producers DATA section.
188  
189 =head1 SUB CLASS HOOKS
190  
191 Sub-classes can override these methods to control the templating by giving
192 the template source, adding variables and giving config to the Tempate object.
193  
194 =head2 tt_config
195  
196 sub tt_config { ( INTERPOLATE => 1 ); }
197  
198 Return hash of Template config to add to that given to the L<Template> C<new>
199 method.
200  
201 =head2 tt_schema
202  
203 sub tt_schema { "foo.tt"; }
204 sub tt_schema { local $/ = undef; \<DATA>; }
205  
206 The template to use, return a file name or a scalar ref of TT
207 source, or an L<IO::Handle>. See L<Template> for details, as the return from
208 this is passed on to it's C<produce> method.
209  
210 The default implementation uses the producer arg C<ttfile> as a filename to read
211 the template from. If the arg isn't there it will look for a C<__DATA__> section
212 in the class, reading it as template source if found. Returns undef if both
213 these fail, causing the produce call to fail with a 'no template!' error.
214  
215 =head2 tt_vars
216  
217 sub tt_vars { ( foo => "bar" ); }
218  
219 Return hash of template vars to use in the template. Nothing added here
220 by default, but see L</tt_default_vars> for the variables you get for free.
221  
222 =head2 tt_default_vars
223  
224 Return a hash-ref of the default vars given to the template.
225 You wouldn't normally over-ride this, just inherit the default implementation,
226 to get the C<translator> & C<schema> variables, then over-ride L</tt_vars> to add
227 your own.
228  
229 The current default variables are:
230  
231 =over 4
232  
233 =item schema
234  
235 The schema to template.
236  
237 =item translator
238  
239 The L<SQL::Translator> object.
240  
241 =back
242  
243 =head2 pre_process_schema
244  
245 WARNING: This method is Experimental so may change!
246  
247 Called with the L<SQL::Translator::Schema> object and should return one (it
248 doesn't have to be the same one) that will become the C<schema> variable used
249 in the template.
250  
251 Gets called from tt_default_vars.
252  
253 =head1 PRODUCER OBJECT
254  
255 The rest of the methods in the class set up a sub-classable producer object.
256 You normally just inherit them.
257  
258 =head2 new
259  
260 my $tt_producer = TT::Base->new( translator => $translator );
261  
262 Construct a new TT Producer object. Takes a single, named arg of the
263 L<SQL::Translator> object running the translation. Dies if this is not given.
264  
265 =head2 translator
266  
267 Return the L<SQL::Translator> object.
268  
269 =head2 schema
270  
271 Return the L<SQL::Translator::Schema> we are translating. This is equivalent
272 to C<< $tt_producer->translator->schema >>.
273  
274 =head2 run
275  
276 Called to actually produce the output, calling the sub class hooks. Returns the
277 produced text.
278  
279 =head2 args
280  
281 Util wrapper method around C<< TT::Base->translator->producer_args >> for
282 (mostly) readonly access to the producer args. How it works depends on the
283 number of arguments you give it and the context.
284  
285 No args - Return hashref (the actual hash in Translator) or hash of args.
286 1 arg - Return value of the arg with the passed name.
287 2+ args - List of names. In list context returns values of the given arg
288 names, returns as a hashref in scalar context. Any names given
289 that don't exist in the args are returned as undef.
290  
291 This is still a bit messy but is a handy way to access the producer args when
292 you use your own to drive the templating.
293  
294 =head1 SEE ALSO
295  
296 L<perl>,
297 L<SQL::Translator>,
298 L<Template>.
299  
300 =head1 TODO
301  
302 - Add support for a sqlf template repository, set as an INCLUDE_PATH,
303 so that sub-classes can easily include file based templates using relative
304 paths.
305  
306 - Pass in template vars from the producer args and command line.
307  
308 - Merge in L<TT::Table|SQL::Translator::Producer::TT::Table>.
309  
310 - Hooks to pre-process the schema and post-process the output.
311  
312 =head1 AUTHOR
313  
314 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
315  
316 =cut
« blib/lib/SQL/Translator/Producer/Latex.pm ↑ Summary blib/lib/SQL/Translator/Schema/Role/Compare.pm »