File Coverage

blib/lib/HTML/Tmojo/TemplateLoader.pm
Criterion Covered Total %
statement 27 34 79.4
branch 4 8 50.0
condition n/a
subroutine 4 5 80.0
pod 0 4 0.0
total 35 51 68.6


line stmt bran cond sub pod time code
1             ###########################################################################
2             # Copyright 2004 Lab-01 LLC
3             #
4             # Licensed under the Apache License, Version 2.0 (the "License");
5             # you may not use this file except in compliance with the License.
6             # You may obtain a copy of the License at
7             #
8             # http://www.apache.org/licenses/LICENSE-2.0
9             #
10             # Unless required by applicable law or agreed to in writing, software
11             # distributed under the License is distributed on an "AS IS" BASIS,
12             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13             # See the License for the specific language governing permissions and
14             # limitations under the License.
15             #
16             # Tmojo(tm) is a trademark of Lab-01 LLC.
17             ###########################################################################
18              
19             package HTML::Tmojo::TemplateLoader;
20              
21 5     5   27 use strict;
  5         8  
  5         2298  
22              
23             sub new {
24 5     5 0 100 my ($class, $template_dir, $tmojo_lite) = @_;
25            
26 5         64 $template_dir =~ s/\/$//;
27            
28 5         78 my $self = {
29             template_dir => $template_dir,
30             tmojo_lite => $tmojo_lite,
31             };
32            
33 5         90 return bless $self, $class;
34             }
35              
36             sub load_template {
37 9     9 0 29 my ($self, $normalized_template_id, $cache_time_stamp) = @_;
38            
39 9         35 my $file_name = "$self->{template_dir}$normalized_template_id";
40            
41 9 50       234 unless (-r $file_name) {
42 0         0 die "couldn't find template '$normalized_template_id' ($file_name)";
43             }
44            
45 9 50       158 if (-d $file_name) {
46 0         0 die "template '$normalized_template_id' ($file_name) is a directory";
47             }
48            
49 9         189 my $source_time_stamp = (stat($file_name))[9];
50            
51 9 100       47 if ($source_time_stamp > $cache_time_stamp) {
52             # LOAD AND RETURN THE FILE
53 8         605 open my ($fh), $file_name;
54 8         130 local $/ = "\n"; # THIS CAN GET EXTRA SCREWED UP IN MOD_PERL
55 8         475 my @template_lines = <$fh>;
56 8         127 close $fh;
57            
58 8         119 return \@template_lines, $self->{tmojo_lite};
59             }
60             else {
61 1         4 return 0;
62             }
63             }
64              
65             sub template_exists {
66 0     0 0 0 my ($self, $normalized_template_id) = @_;
67            
68 0         0 my $file_name = "$self->{template_dir}$normalized_template_id";
69            
70 0 0       0 if (-r $file_name) {
71 0         0 return 1;
72             }
73             else {
74 0         0 return 0;
75             }
76             }
77              
78             sub template_package_name {
79 9     9 0 28 my ($self, $normalized_template_id) = @_;
80            
81 9         69 my $package = "$self->{template_dir}$normalized_template_id";
82            
83 9         139 for ($package) {
84 9         40 tr/:/_/;
85 9         92 s{/}{::}g;
86 9         431 s/[^\d\w:]/_/g;
87             }
88            
89 9         52 $package = "HTML::Tmojo::TemplateLoader::$package";
90            
91 9         41 return $package;
92             }
93              
94             1;