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   1965 use strict; use utf8; use warnings FATAL => 'all';
  3     3   6  
  3     3   72  
  3         15  
  3         6  
  3         16  
  3         80  
  3         6  
  3         108  
3 3     3   14 use parent 'DTL::Fast::Filter';
  3         5  
  3         17  
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 55 my $self = shift;
25 22         88 push @{$self->{'parameter'}}, DTL::Fast::Variable->new(time)
26 44 100       44 if not scalar @{$self->{'parameter'}};
  44         122  
27 44         91 $self->{'time'} = $self->{'parameter'}->[0];
28 44         124 $self->{'suffix'} = DTL::Fast::Variable->new('s'); # this is pluralize related
29            
30 44         139 return $self;
31             }
32              
33             #@Override
34             sub filter
35             {
36 44     44 0 48 my $self = shift; # self
37 44         45 shift; # filter_manager
38 44         79 my $value = shift;
39 44         52 my $context = shift;
40            
41 44         128 my $time = $self->{'time'}->render($context);
42            
43 44         162 return $self->time_diff($time - $value);
44             }
45              
46             sub time_diff
47             {
48 44     44 0 56 my $self = shift;
49 44         47 my $diff = shift;
50 44         64 my @diffs = ();
51            
52 44 50       86 if( $diff )
53             {
54 44         75 foreach my $lapse (@LAPSE)
55             {
56 236 100       490 if( $diff >= $lapse->[1] )
57             {
58 64         126 my $val = int( $diff / $lapse->[1]);
59 64         94 $diff = $diff % $lapse->[1];
60             push @diffs, sprintf( '%d %s%s'
61             , $val // 'undef'
62             , $lapse->[0] // 'undef'
63 64   50     1785 , ($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       211 if( scalar @diffs == $MAXSTEPS )
67             {
68 24         48 last;
69             }
70             }
71             }
72             }
73              
74 44         210 return join ', ', @diffs;
75             }
76              
77             1;