File Coverage

blib/lib/DTL/Fast/Filter/Ru/Pluralize.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition 13 13 100.0
subroutine 6 6 100.0
pod 0 2 0.0
total 59 61 96.7


line stmt bran cond sub pod time code
1             package DTL::Fast::Filter::Ru::Pluralize;
2 1     1   432 use strict; use utf8; use warnings FATAL => 'all';
  1     1   1  
  1     1   37  
  1         4  
  1         2  
  1         4  
  1         19  
  1         1  
  1         26  
3 1     1   3 use parent 'DTL::Fast::Filter::Pluralize';
  1         1  
  1         3  
4              
5             $DTL::Fast::FILTER_HANDLERS{'pluralize'} = __PACKAGE__;
6              
7             #@Override
8             sub parse_parameters
9             {
10 11     11 0 8 my $self = shift;
11 2         8 push @{$self->{'parameter'}}, DTL::Fast::Variable->new('""') # specify suffix for one, two and five
12 11 100       8 if not scalar @{$self->{'parameter'}};
  11         22  
13 11         13 $self->{'suffix'} = $self->{'parameter'}->[0];
14 11         21 return $self;
15             }
16              
17             sub pluralize
18             {
19 11     11 0 9 my $self = shift;
20 11         9 my $value = shift;
21 11         9 my $suffix = shift;
22            
23 11   100     22 my $suffix_one = shift @$suffix // '';
24 11   100     23 my $suffix_two = shift @$suffix // '';
25 11   100     21 my $suffix_five = shift @$suffix // $suffix_two;
26            
27 11         20 my $last_digit = substr int($value), -1;
28 11         9 my $amount = abs $value;
29            
30 11 100 100     64 if( $amount >= 10 and $amount <= 20 ) {
    100 100        
    100          
31 1         7 $value = $suffix_five;
32             }
33             elsif( $last_digit == 1 ) {
34 3         3 $value = $suffix_one;
35             }
36             elsif( $last_digit >= 2 and $last_digit <= 4 ) {
37 4         5 $value = $suffix_two;
38             }
39             else{
40 3         4 $value = $suffix_five;
41             }
42            
43 11         31 return $value;
44            
45             }
46              
47             1;