lib/Templer/Plugin/Redis.pm

filter: Statementstmt 83.3% 25 / 30 Branchbran 33.3% 2 / 6 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 64.1% 34 / 53 CC 7 SCAR 18.6
CC
7
Cov
67%
expand_variables
11.8
new
3.0
BEGIN
1.0
CRAP 6.5
« blib/lib/Templer/Timer.pm ↑ Summary lib/Templer/Plugin/SiteMap.pm »
1  
2 =head1 NAME
3  
4 Templer::Plugin::Redis - A plugin to retrieve values from Redis
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 The following is a good example use of this plugin
11  
12 title: About my site
13 count: redis_get( "total_count" )
14 ----
15 <p>There are <!-- tmpl_var name='count' --> ponies.</p>
16  
17 =cut
18  
19 =head1 DESCRIPTION
20  
21 This plugin allows template variables to be values retrieved from
22 a redis store.
23  
24 It is assumed that redis will be running on the localhost, if it is
25 not you may set the environmental variable C<REDIS_SERVER> to point
26 to your IP:port pair.
27  
28 =cut
29  
30 =head1 LICENSE
31  
32 This module is free software; you can redistribute it and/or modify it
33 under the terms of either:
34  
35 a) the GNU General Public License as published by the Free Software
36 Foundation; either version 2, or (at your option) any later version,
37 or
38  
39 b) the Perl "Artistic License".
40  
41 =cut
42  
43 =head1 AUTHOR
44  
45 Steve Kemp <steve@steve.org.uk>
46  
47 =cut
48  
49 =head1 COPYRIGHT AND LICENSE
50  
51 Copyright (C) 2015 Steve Kemp <steve@steve.org.uk>.
52  
53 This library is free software. You can modify and or distribute it under
54 the same terms as Perl itself.
55  
56 =cut
57  
58 =head1 METHODS
59  
60 =cut
61  
62  
63 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66  
67 package Templer::Plugin::Redis;
68  
69  
70 =head2
71  
72 Constructor. No arguments are required/supported.
73  
74 =cut
75  
76 sub new
77 11 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
78 11 my ( $proto, %supplied ) = (@_);
79 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
80  
81 11 my $self = {};
82  
83  
84 11 bless( $self, $class );
85  
86 11 my $module = "use Redis;";
87  
88 ## no critic (Eval)
89 11 ▶ eval($module);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 ## use critic
91  
92 11 ▶ if ( !$@ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 11 0 11
93 {
94  
95 #
96 # OK the module was loaded, but Redis might not be
97 # running locally, or accessible remotely so in those
98 # cases we'll be disabled.
99 #
100 # NOTE: Redis will use $ENV{'REDIS_SERVER'} if that is
101 # set, otherwise defaulting to 127.0.0.1:6379.
102 #
103 11 eval {$self->{ 'redis' } = new Redis()};
104 }
105  
106 11 return $self;
107 }
108  
109  
110 =head2 expand_variables
111  
112 This is the method which is called by the L<Templer::Plugin::Factory>
113 to expand the variables contained in a L<Templer::Site::Page> object.
114  
115 Variables are written in the file in the form "key: value", and are
116 internally stored within the Page object as a hash.
117  
118 This method iterates over each key & value and updates any that
119 seem to refer to redis-fetches.
120  
121 =cut
122  
123 sub expand_variables
124 9 ▶{
Subroutine expand_variables: called CC=5 SCAR=24.7
Pod documented
125 9 my ( $self, $site, $page, $data ) = (@_);
126  
127 #
128 # Get the page-variables in the template.
129 #
130 9 my %hash = %$data;
131  
132 #
133 # Look for a value of "redis_get" in each key.
134 #
135 9 foreach my $key ( keys %hash )
136 {
137 66 ▶ if ( $hash{ $key } =~ /^redis_get\((.*)\)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{$key} =~ /^redis_get\((.*)\)/) 0 66 66
138 {
139  
140 #
141 # The lookup value.
142 #
143 0 my $rkey = $1;
144  
145 #
146 # Strip leading/trailing whitespace and quotes
147 #
148 0 $rkey =~ s/^\s+|\s+$//g;
149 0 $rkey =~ s/^["']|['"]$//g;
150  
151 #
152 # If we have redis, and it is alive/connected, then use it.
153 #
154 0 ▶ if ( $self->{ 'redis' } &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"redis"} and $self->{"redis"}->ping) 0 0 0
Condition0%0/3
$self->{"redis"} and $self->{"redis"}->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"redis"} and $self->{"redis"}->ping
$self->{"redis"} $self->{"redis"}->ping
Truth table0%0/3
A and B
A= $self->{"redis"}B= $self->{"redis"}->ping
ABresult
0X0
100
111
155 $self->{ 'redis' }->ping() )
156 {
157 0 $hash{ $key } = $self->{ 'redis' }->get($rkey);
158 }
159 }
160  
161 }
162  
163 #
164 # Return.
165 #
166 9 return ( \%hash );
167 }
168  
169  
170 #
171 # Register the plugin.
172 #
173 11 Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::Redis");
« blib/lib/Templer/Timer.pm ↑ Summary lib/Templer/Plugin/SiteMap.pm »