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