File Coverage

blib/lib/Template/AsGraph/Context.pm
Criterion Covered Total %
statement 9 19 47.3
branch 0 4 0.0
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 28 46.4


line stmt bran cond sub pod time code
1             #============================================================= -*-Perl-*-
2             #
3             # Template::AsGraph::Context
4             #
5             # DESCRIPTION
6             # Wrapper module for original Template::Context, populating a tree
7             # structure for each processed template.
8             #
9             # AUTHOR
10             # Breno G. de Oliveira
11             #
12             # COPYRIGHT
13             # Copyright (C) 2009 Breno G. de Oliveira. All Rights Reserved.
14             #
15             # This module is free software; you can redistribute it and/or
16             # modify it under the same terms as Perl itself.
17             #
18             #============================================================================
19              
20             package Template::AsGraph::Context;
21             # maybe this should be refactored into a separate
22             # module such as TemplateX::Context::Routes
23              
24 1     1   1315 use strict;
  1         3  
  1         451  
25 1     1   8 use warnings;
  1         3  
  1         46  
26 1     1   7 use base 'Template::Context';
  1         2  
  1         1455  
27              
28             our $VERSION = '0.01';
29              
30             #TODO: make tree-building code suck less
31             # preferably getting rid of this:
32             our $_tree = {};
33              
34              
35             #------------------------------------------------------------------------
36             # process($template, \%params) [% PROCESS template var=val ... %]
37             # process($template, \%params, $local) [% INCLUDE template var=val ... %]
38             #
39             # This is a wrapper for Template::Context's process() method. It creates
40             # a tree of visited templates as it goes deeper into them.
41             #
42             # The tree hash can be accessed after process() is called via
43             # $template->context->tree
44             #------------------------------------------------------------------------
45              
46             sub process {
47 0     0 1   my ($self, $template, $params, $localize) = @_;
48            
49             # find out the name of the template
50 0           my $name = ref($template) eq 'ARRAY'
51 0 0         ? join (' + ', @{$template}) : ref($template)
    0          
52             ? $template->name : $template
53             ;
54              
55             # initialize tree node
56 0           $_tree->{$name} = {};
57              
58             # prepare environment and call parent's
59             # original process() method. This will recursively
60             # come back here with a localized version of the
61             # tree so we can place our node with the command
62             # above.
63 0           my $output;
64             {
65 0           local $_tree = $_tree->{$name};
  0            
66 0           $output = $self->SUPER::process($template, $params, $localize);
67             }
68            
69             # this will make our parent automatically create
70             # the $context->tree() method with our entire
71             # template tree.
72 0           $self->{ TREE } = $_tree;
73            
74             # finally, return the output just like
75             # the original process() does.
76 0           return $output;
77             }
78              
79             42;