File Coverage

blib/lib/Template/Plugin/ByDate.pm
Criterion Covered Total %
statement 15 34 44.1
branch 0 18 0.0
condition 0 5 0.0
subroutine 5 8 62.5
pod 2 2 100.0
total 22 67 32.8


line stmt bran cond sub pod time code
1             package Template::Plugin::ByDate;
2              
3 1     1   25212 use warnings;
  1         2  
  1         37  
4 1     1   5 use strict;
  1         3  
  1         214  
5              
6             =head1 NAME
7              
8             Template::Plugin::ByDate - Keeps/removes included text based on whether the current date is within range.
9              
10             =head1 VERSION
11              
12             Version 0.01
13              
14             =cut
15              
16             our $VERSION = '0.04';
17              
18             =head1 SYNOPSIS
19              
20             [% USE ByDate %]
21            
22             [% FILTER ByDate
23             starting = '2006-05-02'
24             until = '2006-08-22' %]
25             This text only shows up from May 2, 2006 through August 22, 2006.
26             [% END %]
27              
28             =head1 FUNCTIONS
29              
30             =head2 init
31              
32             =cut
33              
34 1     1   6 use base 'Template::Plugin::Filter';
  1         7  
  1         1244  
35              
36             sub init
37             {
38 0     0 1   my $self = shift;
39 0           $self->{ _DYNAMIC }++;
40 0   0       $self->install_filter($self->{_ARGS}->[0] || 'ByDate');
41 0           $self;
42             }
43              
44             =head2 filter
45              
46             We accept one optional argument, the word "not". If specified, it will
47             reverse the meaning of the filter: rather than keeping the text if the current
48             date is between starting and until, ignore it. e.g.,
49              
50             [% FILTER ByDate
51             starting = '2006-05-02'
52             until = '2006-08-22' %]
53             This only shows up inside the date range
54             [% END %]
55              
56             while
57              
58             [% FILTER ByDate
59             'not' starting = '2006-05-02'
60             until = '2006-08-22' %]
61             This only shows up outside the date range
62             [% END %]
63              
64             The starting and until dates are actually parsed by L. If you
65             do not specify a time, the starting time is 00:00:00, while the until time is
66             23:59:59. This is done by checking the until stamp for a colon - if there is
67             no colon, we add " 23:59:59" to the string before passing it into Date::Parse.
68             If that doesn't work for you, please let me know what string you're using.
69              
70             =cut
71              
72 1     1   9101 use Date::Parse;
  1         12256  
  1         157  
73 1     1   945 use List::MoreUtils qw/any/;
  1         984  
  1         336  
74              
75             sub filter
76             {
77 0     0 1   my ($self, $text, $args, $conf) = @_;
78              
79             # cargo-cult code: this is what Template::Plugin says to do.
80 0           $args = $self->merge_args($args);
81 0           $conf = $self->merge_config($conf);
82              
83             # if "not" is specified B,
84             # then we will reverse the I expression
85 0 0   0     my $not = (any { lc eq 'not' } @$args) ? 1 : 0;
  0            
86              
87             # if until is provided, but there is no colon, treat this as the
88             # end of day rather than beginning of day. This may reduce some
89             # ability to do what you want, but for the vast majority of the cases
90             # will make your templates easier to read, IMO.
91 0           my $until_str = $conf->{'until'};
92 0 0 0       if (defined $until_str and $until_str !~ /:/)
93             {
94 0           $until_str .= ' 23:59:59';
95             }
96              
97             # convert input to timestamps.
98 0 0         my $starting = exists $conf->{starting} ? str2time($conf->{starting}) : 0;
99 0 0         my $until = defined $until_str ? str2time($until_str) : undef;
100              
101             # undocumented: don't use it. This is here solely for testing purposes.
102 0 0         my $now = exists $conf->{now} ? str2time($conf->{now}) : time;
103              
104             # are we within the range? There probably is a simpler way to express
105             # this, but this works.
106 0 0         my $display = $now >= $starting ? 1 : 0;
107 0 0         if (defined $until)
108             {
109 0 0         $display = 0 unless $until >= $now;
110             }
111              
112             # negate the display if the 'not' argument was given, and return either
113             # the text or nothing depending on that.
114 0 0         $display ^ $not ? $text : '';
115             }
116              
117             =head1 AUTHOR
118              
119             Darin McBride, C<< >>
120              
121             =head1 BUGS
122              
123             Please report any bugs or feature requests to
124             C, or through the web interface at
125             L.
126             I will be notified, and then you'll automatically be notified of progress on
127             your bug as I make changes.
128              
129             =head1 SUPPORT
130              
131             You can find documentation for this module with the perldoc command.
132              
133             perldoc Template::Plugin::ByDate
134              
135             You can also look for information at:
136              
137             =over 4
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L
142              
143             =item * CPAN Ratings
144              
145             L
146              
147             =item * RT: CPAN's request tracker
148              
149             L
150              
151             =item * Search CPAN
152              
153             L
154              
155             =back
156              
157             =head1 ACKNOWLEDGEMENTS
158              
159             =head1 COPYRIGHT & LICENSE
160              
161             Copyright 2006, 2008 Darin McBride, all rights reserved.
162              
163             This program is free software; you can redistribute it and/or modify it
164             under the same terms as Perl itself.
165              
166             =cut
167              
168             1; # End of Template::Plugin::ByDate