File Coverage

blib/lib/Excel/Template/Plus/TT.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1              
2             package Excel::Template::Plus::TT;
3 1     1   2884 use Moose;
  0            
  0            
4             use Moose::Util::TypeConstraints;
5              
6             use Template ();
7             use IO::String ();
8              
9             use Excel::Template;
10              
11             our $VERSION = '0.05';
12             our $AUTHORITY = 'cpan:STEVAN';
13              
14             with 'MooseX::Param';
15              
16             subtype 'IO::Handle'
17             => as 'Object'
18             => where { $_->isa('IO::Handle') };
19              
20             has 'template' => (
21             is => 'ro',
22             # can either be a:
23             # - filename
24             # - scalar ref to string
25             # - open filehandle
26             # - an IO::Handle instance
27             # (basically anything TT takes)
28             isa => 'Str | ScalarRef | FileHandle | IO::Handle',
29             required => 1,
30             );
31              
32             has 'config' => (
33             is => 'ro',
34             isa => 'HashRef',
35             default => sub {{}},
36             );
37              
38             has '_template_object' => (
39             is => "rw",
40             isa => "Template",
41             lazy => 1,
42             default => sub {
43             my $self = shift;
44             my $class = $self->template_class;
45             Class::MOP::load_class($class);
46             ($class->isa('Template'))
47             || confess "The template_class must be Template or a subclass of it";
48             $class->new( $self->config )
49             }
50             );
51              
52             has 'template_class' => (
53             is => "rw",
54             isa => "Str",
55             default => "Template",
56             );
57              
58             ## private attributes
59              
60             has '_excel_template' => (
61             is => 'ro',
62             isa => 'Excel::Template',
63             handles => [qw[
64             output
65             write_file
66             ]],
67             lazy => 1,
68             default => sub {
69             my $self = shift;
70             $self->_prepare_excel_template;
71             }
72             );
73              
74             sub _prepare_excel_template {
75             my $self = shift;
76              
77             my $buf;
78             my $fh = IO::String->new(\$buf);
79              
80             my $tt = $self->_template_object;
81             $tt->process(
82             $self->template,
83             $self->params,
84             $fh,
85             );
86              
87             $fh->pos(0);
88            
89             confess "Template creation failed because : " . $tt->error()
90             if $tt->error();
91              
92             confess "Template failed to produce any output"
93             unless length($buf);
94              
95             my $excel_template = eval { Excel::Template->new(file => $fh) };
96             if ($@) {
97             warn $buf;
98             confess $@;
99             }
100            
101             return $excel_template;
102             }
103              
104             no Moose; no Moose::Util::TypeConstraints; 1;
105              
106             __END__
107              
108             =pod
109              
110             =head1 NAME
111              
112             Excel::Template::Plus::TT - Extension of Excel::Template to use TT
113              
114             =head1 SYNOPSIS
115              
116             use Excel::Template::Plus::TT;
117            
118             # this is most commonly used through
119             # the Excel::Template::Plus factory
120            
121             my $template = Excel::Template::Plus::TT->new(
122             template => 'greeting.tmpl',
123             config => { INCLUDE => [ '/templates' ] },
124             params => { greeting => 'Hello' }
125             );
126            
127             $template->param(location => 'World');
128            
129             $template->write_file('greeting.xls');
130              
131             =head1 DESCRIPTION
132              
133             This is an engine for Excel::Template::Plus which replaces the
134             standard Excel::Template template features with TT. See the
135             L<Excel::Template::Plus> docs for more information.
136              
137             =head1 METHODS
138              
139             =head2 Accessors
140              
141             =over 4
142              
143             =item B<config>
144              
145             =item B<template>
146              
147             =item B<template_class>
148              
149             =item B<params>
150              
151             =back
152              
153             =head2 Excel::Template compat methods
154              
155             =over 4
156              
157             =item B<params ($name | $name => $value)>
158              
159             This provides access to getting and setting the parameters, it behaves
160             exactly like the standard CGI.pm-style param method.
161              
162             =item B<output>
163              
164             Returns the generated excel file.
165              
166             =item B<write_file ($filename)>
167              
168             Writes the generated excel file to C<$filename>.
169              
170             =back
171              
172             =head2 Housekeeping
173              
174             =over 4
175              
176             =item B<DEMOLISH>
177              
178             This will cleanup any temp files generated in the process.
179              
180             =item B<meta>
181              
182             Returns the metaclass.
183              
184             =back
185              
186             =head1 BUGS
187              
188             All complex software has bugs lurking in it, and this module is no
189             exception. If you find a bug please either email me, or add the bug
190             to cpan-RT.
191              
192             =head1 ACKNOWLEDGEMENTS
193              
194             =over 4
195              
196             =item This module was inspired by Excel::Template::TT.
197              
198             =back
199              
200             =head1 AUTHOR
201              
202             Stevan Little E<lt>stevan@iinteractive.comE<gt>
203              
204             =head1 COPYRIGHT AND LICENSE
205              
206             Copyright 2007-2010 by Infinity Interactive, Inc.
207              
208             L<http://www.iinteractive.com>
209              
210             This library is free software; you can redistribute it and/or modify
211             it under the same terms as Perl itself.
212              
213             =cut