File Coverage

blib/lib/DTL/Fast/Filter/Stringformat.pm
Criterion Covered Total %
statement 25 25 100.0
branch 3 6 50.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 35 40 87.5


line stmt bran cond sub pod time code
1             package DTL::Fast::Filter::Stringformat;
2 2     2   1184 use strict; use utf8; use warnings FATAL => 'all';
  2     2   4  
  2     2   46  
  2         10  
  2         4  
  2         12  
  2         47  
  2         5  
  2         69  
3 2     2   10 use parent 'DTL::Fast::Filter';
  2         4  
  2         13  
4              
5             $DTL::Fast::FILTER_HANDLERS{'stringformat'} = __PACKAGE__;
6              
7 2     2   124 use DTL::Fast::Variable;
  2         4  
  2         375  
8              
9             #@Override
10             sub parse_parameters
11             {
12 3     3 0 3 my $self = shift;
13             die $self->get_parse_error("no format string specified")
14 3 50       4 if not scalar @{$self->{'parameter'}};
  3         10  
15 3         6 $self->{'format'} = $self->{'parameter'}->[0];
16 3         11 return $self;
17             }
18              
19             #@Override
20             sub filter
21             {
22 3     3 0 6 my($self, $filter_manager, $value, $context ) = @_;
23            
24 3         10 my $format = $self->{'format'}->render($context);
25              
26 3 50       7 die $self->get_render_error($context, 'unable to format string with undef value')
27             if not defined $value;
28              
29 3 50       7 die $self->get_render_error($context, 'unable to format string with undef format')
30             if not defined $format;
31              
32 3         38 return sprintf '%'.$format, $value;
33             }
34              
35             1;