X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FTT%2FBase.pm;h=ff87737ef6c6a954af8bfaaa746b1d798a841969;hb=ba506e52c480afe33dfec6b38a12759fad1e7fa2;hp=19aa80214cd14f891a07761730e50c6ba0757cf3;hpb=f5f03b784100c928aa1a1706deafbc27f2ec1730;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/TT/Base.pm b/lib/SQL/Translator/Producer/TT/Base.pm index 19aa802..ff87737 100644 --- a/lib/SQL/Translator/Producer/TT/Base.pm +++ b/lib/SQL/Translator/Producer/TT/Base.pm @@ -1,9 +1,7 @@ package SQL::Translator::Producer::TT::Base; # ------------------------------------------------------------------- -# $Id: Base.pm,v 1.1 2004-04-14 19:19:44 grommit Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors +# Copyright (C) 2002-2009 SQLFairy Authors # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -20,49 +18,23 @@ package SQL::Translator::Producer::TT::Base; # 02111-1307 USA # ------------------------------------------------------------------- -=pod +=pod =head1 NAME -SQL::Translator::Producer::TT::Base - TT based Producer base class. - -=head1 SYNOPSIS - - package SQL::Translator::Producer::Foo; - use base qw/SQL::Translator::Producer::TT::Base/; - - # Convert produce call into an object of our new class - sub produce { return __PACKAGE__->new( translator => shift )->run; }; - - # Return file name or template source - sub tt_schema { local $/ = undef; return \; } - - # Extra vars to add to the template - sub tt_vars { ( foo => "bar" ); } - -=head1 DESCRIPTION - -A base class producer designed to be sub-classed to create new TT base -producers cheaply by simply giving the template to use and sprinkling in some -extra template variables. - -See the synopsis above for an example of creating a simple producer using -a single template stored in the producers DATA section. - -WARNING: This is currently WORK IN PROGRESS and so subject to change, -but it does work ;-) +SQL::Translator::Producer::TT::Base - TT (Template Toolkit) based Producer base +class. =cut -# ------------------------------------------------------------------- - use strict; use vars qw[ $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; +$VERSION = '1.60'; use Template; use Data::Dumper; +use IO::Handle; use Exporter; use base qw(Exporter); @EXPORT_OK = qw(produce); @@ -89,18 +61,18 @@ sub new { sub translator { shift->{translator}; } sub schema { shift->{translator}->schema(@_); } -# Until args access method. +# Util args access method. # No args - Return hashref (the actual hash in Translator) or hash of args. # 1 arg - Return that named args value. # Args - List of names. Return values of the given arg names in list context # or return as hashref in scalar context. Any names given that don't -# exists in the args return undef. +# exist in the args are returned as undef. sub args { my $me = shift; # No args unless (@_) { - return wantarray + return wantarray ? %{ $me->{translator}->producer_args } : $me->{translator}->producer_args ; @@ -123,58 +95,239 @@ sub run { debug "Processing template $tmpl\n"; my $out; - my $tt = Template->new( + my $tt = Template->new( #DEBUG => $me->translator->debug, - ABSOLUTE => 1, # Set so we can use from the command line sensibly - RELATIVE => 1, # Maybe the cmd line code should set it! Security! - %args, # Allow any TT opts to be passed in the producer_args + ABSOLUTE => 1, # Set so we can use from the command line sensibly + RELATIVE => 1, # Maybe the cmd line code should set it! Security! + $me->tt_config, # Hook for sub-classes to add config + %args, # Allow any TT opts to be passed in the producer_args ) || die "Failed to initialize Template object: ".Template->error; - $tt->process( $tmpl, { $me->tt_default_vars, $me->tt_vars, }, \$out ) + $tt->process( $tmpl, { + $me->tt_default_vars, + $me->tt_vars, # Sub-class hook for adding vars + }, \$out ) or die "Error processing template '$tmpl': ".$tt->error; return $out; } -# Returns template file to use, or a scalar ref of tt source, or io handle. -# See L