X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FTT%2FBase.pm;h=75338209ca044b2172028712fcc4fed5eaecd9df;hb=11ad2df91bcc0674faa8fb5b6bab52c9e4a73762;hp=e2dbc0b228193bb772547a68ec6255ea85f0223e;hpb=11a8c77ab2663ad12f254fd3e292d8d16cb08918;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/TT/Base.pm b/lib/SQL/Translator/Producer/TT/Base.pm index e2dbc0b..7533820 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.3 2004-05-14 00:46:32 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 @@ -32,10 +30,11 @@ class. use strict; use vars qw[ $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; +$VERSION = '1.59'; use Template; use Data::Dumper; +use IO::Handle; use Exporter; use base qw(Exporter); @EXPORT_OK = qw(produce); @@ -119,16 +118,33 @@ sub run { sub tt_config { () }; -sub tt_schema { shift->args("ttfile") }; +sub tt_schema { + my $me = shift; + my $class = ref $me; + + my $file = $me->args("ttfile"); + return $file if $file; + + no strict 'refs'; + my $ref = *{"$class\:\:DATA"}{IO}; + if ( $ref->opened ) { + local $/ = undef; # Slurp mode + return \<$ref>; + } + + undef; +}; sub tt_default_vars { my $me = shift; return ( translator => $me->translator, - schema => $me->translator->schema, + schema => $me->pre_process_schema($me->translator->schema), ); } +sub pre_process_schema { $_[1] } + sub tt_vars { () }; 1; @@ -139,85 +155,85 @@ sub tt_vars { () }; =head1 SYNOPSIS + # Create a producer using a template in the __DATA__ section. package SQL::Translator::Producer::Foo; use base qw/SQL::Translator::Producer::TT::Base/; - # Convert produce call into an object of our new class + # Convert produce call into a method call on our new class sub produce { return __PACKAGE__->new( translator => shift )->run; }; - # Return file name or template source - sub tt_schema { local $/ = undef; \; } - # Configure the Template object. sub tt_config { ( INTERPOLATE => 1 ); } # Extra vars to add to the template sub tt_vars { ( foo => "bar" ); } -=head1 DESCRIPTION + # Put template in DATA section (or use file with ttfile producer arg) + __DATA__ + Schema -WARNING: This is currently WORK IN PROGRESS and so subject to change, -but it does work ;-) + Database: [% schema.database %] + Foo: $foo + ... + +=head1 DESCRIPTION A base class producer designed to be sub-classed to create new TT based producers cheaply - by simply giving the template to use and sprinkling in some extra template variables and config. +You can find an introduction to this module in L. + The 1st thing the module does is convert the produce sub routine call we get -from SQL::Translator into a method call on an object, so we can sub-class it. -This is done with the following code which B appear in B sub -classes. +from SQL::Translator into a method call on an object, which we can then +sub-class. This is done with the following code which needs to appear in B +sub classes. # Convert produce call into an object method call sub produce { return __PACKAGE__->new( translator => shift )->run; }; See L below for details. -The upshot of this is we can make new template producers by writing a template, -then creating a module that starts like this; - - 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; }; - -And then over ride the hooks detailed below in L. +The upshot of this is we can make new template producers by sub classing this +base class, adding the above snippet and a template. +The module also provides a number of hooks into the templating process, +see L for details. See the L above for an example of creating a simple producer using a single template stored in the producers DATA section. =head1 SUB CLASS HOOKS -Sub-classes should override these methods to control the templating by giving +Sub-classes can override these methods to control the templating by giving the template source, adding variables and giving config to the Tempate object. =head2 tt_config sub tt_config { ( INTERPOLATE => 1 ); } -Return hash of Template config to add to the config given to the -C<< Template->new >> method. +Return hash of Template config to add to that given to the L