1 package SQL::Translator::Producer::TT::Base;
3 # -------------------------------------------------------------------
4 # $Id: Base.pm,v 1.1 2004-04-14 19:19:44 grommit Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
27 SQL::Translator::Producer::TT::Base - TT based Producer base class.
31 package SQL::Translator::Producer::Foo;
32 use base qw/SQL::Translator::Producer::TT::Base/;
34 # Convert produce call into an object of our new class
35 sub produce { return __PACKAGE__->new( translator => shift )->run; };
37 # Return file name or template source
38 sub tt_schema { local $/ = undef; return \<DATA>; }
40 # Extra vars to add to the template
41 sub tt_vars { ( foo => "bar" ); }
45 A base class producer designed to be sub-classed to create new TT base
46 producers cheaply by simply giving the template to use and sprinkling in some
47 extra template variables.
49 See the synopsis above for an example of creating a simple producer using
50 a single template stored in the producers DATA section.
52 WARNING: This is currently WORK IN PROGRESS and so subject to change,
57 # -------------------------------------------------------------------
61 use vars qw[ $VERSION @EXPORT_OK ];
62 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
67 use base qw(Exporter);
68 @EXPORT_OK = qw(produce);
70 use SQL::Translator::Utils 'debug';
72 # Hack to convert the produce call into an object. ALL sub-classes need todo
73 # this so that the correct class gets created.
75 return __PACKAGE__->new( translator => shift )->run;
80 my $class = ref $proto || $proto;
83 my $me = bless {}, $class;
84 $me->{translator} = delete $args{translator} || die "Need a translator.";
89 sub translator { shift->{translator}; }
90 sub schema { shift->{translator}->schema(@_); }
92 # Until args access method.
93 # No args - Return hashref (the actual hash in Translator) or hash of args.
94 # 1 arg - Return that named args value.
95 # Args - List of names. Return values of the given arg names in list context
96 # or return as hashref in scalar context. Any names given that don't
97 # exists in the args return undef.
104 ? %{ $me->{translator}->producer_args }
105 : $me->{translator}->producer_args
109 # 1 arg. Return the value whatever the context.
110 return $me->{translator}->producer_args->{$_[0]} if @_ == 1;
112 # More args so return values list or hash ref
113 my %args = %{ $me->{translator}->producer_args };
114 return wantarray ? @args{@_} : { map { ($_=>$args{$_}) } @_ };
117 # Run the produce and return the result.
120 my $scma = $me->schema;
121 my %args = %{$me->args};
122 my $tmpl = $me->tt_schema or die "No template!";
124 debug "Processing template $tmpl\n";
126 my $tt = Template->new(
127 #DEBUG => $me->translator->debug,
128 ABSOLUTE => 1, # Set so we can use from the command line sensibly
129 RELATIVE => 1, # Maybe the cmd line code should set it! Security!
130 %args, # Allow any TT opts to be passed in the producer_args
131 ) || die "Failed to initialize Template object: ".Template->error;
133 $tt->process( $tmpl, { $me->tt_default_vars, $me->tt_vars, }, \$out )
134 or die "Error processing template '$tmpl': ".$tt->error;
139 # Returns template file to use, or a scalar ref of tt source, or io handle.
141 sub tt_schema { shift->args("ttfile") };
143 # Returns hash-ref of the defaults vars given to the template.
144 # You wouldn't normally over-ride but here just in case.
145 sub tt_default_vars {
148 translator => $me->translator,
149 schema => $me->translator->schema,
153 # Return hash of template vars to add to the default set.
157 # -------------------------------------------------------------------
163 Mark Addison E<lt>grommit@users.sourceforge.netE<gt>.
167 Lots! But the next things include;
169 - Hook to allow sub-class to set the options given to the C<Template> instance.
171 - Add support for a sqlf template repository somewhere, set as an INCLUDE_PATH,
172 so that sub-classes can easily include file based templates.
174 - Merge in TT::Table.