File Coverage

blib/lib/DTL/Fast/Filter/Timesince.pm
Criterion Covered Total %
statement 37 37 100.0
branch 7 8 87.5
condition 5 9 55.5
subroutine 7 7 100.0
pod 0 3 0.0
total 56 64 87.5


line stmt bran cond sub pod time code
1             package DTL::Fast::Filter::Timesince;
2 3     3   1597 use strict; use utf8; use warnings FATAL => 'all';
  3     3   4  
  3     3   71  
  3         11  
  3         12  
  3         15  
  3         60  
  3         4  
  3         104  
3 3     3   11 use parent 'DTL::Fast::Filter';
  3         5  
  3         15  
4              
5             $DTL::Fast::FILTER_HANDLERS{'timesince'} = __PACKAGE__;
6              
7             our $MAXSTEPS = 2;
8              
9             our @LAPSE = (
10             ['minute', 60]
11             );
12              
13             unshift @LAPSE, ['hour', $LAPSE[0]->[1] * 60];
14             unshift @LAPSE, ['day', $LAPSE[0]->[1] * 24];
15             unshift @LAPSE, ['week', $LAPSE[0]->[1] * 7];
16             unshift @LAPSE, ['month', $LAPSE[1]->[1] * 30];
17             unshift @LAPSE, ['year', $LAPSE[2]->[1] * 365];
18              
19              
20             #@Override
21             #@todo make pre-defined formats from Django
22             sub parse_parameters
23             {
24 44     44 0 34 my $self = shift;
25 22         82 push @{$self->{'parameter'}}, DTL::Fast::Variable->new(time)
26 44 100       35 if not scalar @{$self->{'parameter'}};
  44         91  
27 44         73 $self->{'time'} = $self->{'parameter'}->[0];
28 44         86 $self->{'suffix'} = DTL::Fast::Variable->new('s'); # this is pluralize related
29            
30 44         114 return $self;
31             }
32              
33             #@Override
34             sub filter
35             {
36 44     44 0 38 my $self = shift; # self
37 44         33 shift; # filter_manager
38 44         32 my $value = shift;
39 44         40 my $context = shift;
40            
41 44         93 my $time = $self->{'time'}->render($context);
42            
43 44         137 return $self->time_diff($time - $value);
44             }
45              
46             sub time_diff
47             {
48 44     44 0 34 my $self = shift;
49 44         72 my $diff = shift;
50 44         50 my @diffs = ();
51            
52 44 50       69 if( $diff )
53             {
54 44         99 foreach my $lapse (@LAPSE)
55             {
56 236 100       336 if( $diff >= $lapse->[1] )
57             {
58 64         105 my $val = int( $diff / $lapse->[1]);
59 64         49 $diff = $diff % $lapse->[1];
60             push @diffs, sprintf( '%d %s%s'
61             , $val // 'undef'
62             , $lapse->[0] // 'undef'
63 64   50     1585 , ($DTL::Fast::FILTER_HANDLERS{'pluralize'} // require DTL::Fast::Filter::Pluralize && 'DTL::Fast::Filter::Pluralize')->pluralize($val, ['s'])
      50        
      50        
      66        
64             );
65            
66 64 100       137 if( scalar @diffs == $MAXSTEPS )
67             {
68 24         35 last;
69             }
70             }
71             }
72             }
73              
74 44         171 return join ', ', @diffs;
75             }
76              
77             1;