« blib/lib/Magpie/Transformer/ServiceUnavailable.pm
↑ Summary
blib/lib/Magpie/Dispatcher/RequestMethod.pm »
| 1 | package Magpie::Transformer::TT2; | ||||||||||
| 2 | # ABSTRACT: Template Toolkit Transformer Component | ||||||||||
| 3 | 1 | $Magpie::Transformer::TT2::VERSION = '1.141660'; | |||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | extends 'Magpie::Transformer'; | |||||||||
| 6 | 1 | ▶ | use Magpie::Constants; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Template; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use MooseX::Types::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | __PACKAGE__->register_events( qw( get_tt_conf get_tt_vars get_template get_transformer transform)); | |||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | sub load_queue { return qw( get_tt_conf get_tt_vars get_template get_transformer transform ) } | ||||||||
|
Subroutine
load_queue: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 14 | |||||||||||
| 15 | has tt_conf => ( | ||||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'HashRef', | ||||||||||
| 18 | required => 1, | ||||||||||
| 19 | 2 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ); | |||||||||
| 21 | |||||||||||
| 22 | has tt_vars => ( | ||||||||||
| 23 | is => 'rw', | ||||||||||
| 24 | isa => 'HashRef', | ||||||||||
| 25 | required => 1, | ||||||||||
| 26 | 2 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ); | |||||||||
| 28 | |||||||||||
| 29 | 1 | has template_file => ( | |||||||||
| 30 | is => 'rw', | ||||||||||
| 31 | isa => 'Path::Class::File', | ||||||||||
| 32 | init_arg => 'template', | ||||||||||
| 33 | coerce => 1, | ||||||||||
| 34 | ); | ||||||||||
| 35 | |||||||||||
| 36 | 1 | has template_path => ( | |||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | isa => 'Path::Class::Dir', | ||||||||||
| 39 | coerce => 1, | ||||||||||
| 40 | required => 1, | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 1 | has transformer => ( | |||||||||
| 44 | is => 'rw', | ||||||||||
| 45 | isa => 'Template', | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | sub get_tt_conf { OK; } | ||||||||
|
Subroutine
get_tt_conf: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | 0 | ▶ | sub get_tt_vars { OK; } | ||||||||
|
Subroutine
get_tt_vars: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 50 | 0 | ▶ | sub get_template { OK; } | ||||||||
|
Subroutine
get_template: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 51 | |||||||||||
| 52 | sub get_transformer { | ||||||||||
| 53 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_transformer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 2 | my $conf = $self->tt_conf; | |||||||||
| 55 | 2 | my $template_path = $self->template_path->stringify; | |||||||||
| 56 | 2 | my $tt_obj = Template->new( | |||||||||
| 57 | { %$conf, INCLUDE_PATH => $template_path } | ||||||||||
| 58 | ); | ||||||||||
| 59 | 2 | $self->transformer($tt_obj); | |||||||||
| 60 | 2 | return OK; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 64 | sub transform { | ||||||||||
| 65 | 2 | ▶ | my ($self, $ctxt) = @_; | ||||||||
|
Subroutine
transform: called CC=2 SCAR=7.3
Pod
undocumented
| |||||||||||
| 66 | 2 | my $tt = $self->transformer; | |||||||||
| 67 | 2 | my $template = $self->template_file->stringify; | |||||||||
| 68 | 2 | my %tt_vars = %{ $self->tt_vars }; | |||||||||
| 69 | 2 | my $output; | |||||||||
| 70 | |||||||||||
| 71 | 2 | $tt_vars{magpie} = $self; | |||||||||
| 72 | |||||||||||
| 73 | try { | ||||||||||
| 74 | 2 | ▶ | $tt->process( $template, \%tt_vars, \$output ) || die $tt->error | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | catch { | ||||||||||
| 77 | 0 | ▶ | my $error = "Error processing template: $_"; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 78 | 0 | warn "$error\n"; | |||||||||
| 79 | 0 | $self->set_error({ status_code => 500, reason => $error }); | |||||||||
| 80 | |||||||||||
| 81 | 2 | }; | |||||||||
| 82 | |||||||||||
| 83 | 2 | ▶ | return OK if $self->has_error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 2 | $self->resource->data( encode('UTF-8', $output) ); | |||||||||
| 86 | |||||||||||
| 87 | 2 | return OK; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | # SEEALSO: Magpie | ||||||||||
| 91 | |||||||||||
| 92 | 1 | 1; | |||||||||
| 93 | __END__ | ||||||||||
| 94 | =pod | ||||||||||
| 95 | |||||||||||
| 96 | =head1 NAME | ||||||||||
| 97 | |||||||||||
| 98 | Magpie::Transformer::TT2 - Template Toolkit Transformer Component | ||||||||||
| 99 | |||||||||||
| 100 | =head1 VERSION | ||||||||||
| 101 | |||||||||||
| 102 | version 1.141660 | ||||||||||
| 103 | |||||||||||
| 104 | =head1 AUTHORS | ||||||||||
| 105 | |||||||||||
| 106 | =over 4 | ||||||||||
| 107 | |||||||||||
| 108 | =item * | ||||||||||
| 109 | |||||||||||
| 110 | Kip Hampton <kip.hampton@tamarou.com> | ||||||||||
| 111 | |||||||||||
| 112 | =item * | ||||||||||
| 113 | |||||||||||
| 114 | Chris Prather <chris.prather@tamarou.com> | ||||||||||
| 115 | |||||||||||
| 116 | =back | ||||||||||
| 117 | |||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 119 | |||||||||||
| 120 | This software is copyright (c) 2011 by Tamarou, LLC. | ||||||||||
| 121 | |||||||||||
| 122 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 123 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||