File Coverage

blib/lib/Locale/Babelfish/Phrase/PluralForms.pm
Criterion Covered Total %
statement 142 181 78.4
branch 30 58 51.7
condition 43 140 30.7
subroutine 11 11 100.0
pod 1 1 100.0
total 227 391 58.0


line stmt bran cond sub pod time code
1             package Locale::Babelfish::Phrase::PluralForms;
2              
3             # ABSTRACT: Babelfish AST pluralization node.
4              
5 4     4   27 use utf8;
  4         8  
  4         32  
6 4     4   118 use strict;
  4         22  
  4         70  
7 4     4   18 use warnings;
  4         9  
  4         176  
8 4     4   27 use feature 'state';
  4         7  
  4         451  
9              
10 4     4   26 use Carp (); # used inside compiled sub.
  4         21  
  4         75  
11 4     4   21 use Scalar::Util (); # used inside compiled sub.
  4         8  
  4         72  
12              
13 4     4   1311 use Locale::Babelfish::Phrase::Compiler ();
  4         12  
  4         99  
14              
15 4     4   23 use parent qw( Locale::Babelfish::Phrase::Node );
  4         7  
  4         23  
16              
17             our $VERSION = '2.10'; # VERSION
18              
19             __PACKAGE__->mk_accessors( qw( forms name compiled locale ) );
20              
21             our @sub_data = ();
22              
23              
24             sub to_perl_sub {
25 15     15 1 42 my ( $self ) = @_;
26              
27 15 50       269 unless ( $self->compiled ) {
28 15         140 state $compiler = Locale::Babelfish::Phrase::Compiler->new;
29 15         282 my $regular_forms = $self->forms->{regular};
30 15         130 for ( my $i = scalar(@$regular_forms) - 1; $i >= 0; $i--) {
31 37         609 $regular_forms->[ $i ] = $compiler->compile( $regular_forms->[ $i ] );
32             }
33 15         555 my $strict_forms = $self->forms->{strict};
34 15         106 for my $key ( keys %$strict_forms ) {
35 1         6 $strict_forms->{ $key } = $compiler->compile( $strict_forms->{ $key } );
36             }
37 15         279 $self->compiled( 1 );
38             }
39              
40 15         349 my $rule = Locale::Babelfish::Phrase::Pluralizer::find_rule( $self->locale );
41              
42             push @sub_data, [
43             $rule,
44             $self->{forms}->{strict},
45             $self->{forms}->{regular},
46 15         61 ];
47              
48 15         52 return $self->_to_perl_sub($self->{name}, scalar(@sub_data) - 1);
49 3     3   7 }
  2         5  
  1         2  
  1         2  
  1         3  
  1         8  
  1         3  
  1         3  
  2         4  
  1         3  
  1         6  
  1         2  
  1         4  
  2         5  
50              
51             sub _to_perl_sub {
52 3     15   6 my ( $self, $name, $index ) = @_;
  3         9  
  2         4  
  2         8  
  1         2  
  1         3  
  1         3  
  1         3  
  1         3  
  1         5  
  1         6  
  1         3  
  1         2  
  1         12  
  1         2  
  1         3  
  2         5  
  2         7  
  1         2  
  1         3  
  1         4  
  1         4  
  1         2  
  1         3  
  1         2  
  1         3  
  2         4  
  2         7  
  15         42  
53 15         50 $name = $self->to_perl_escaped_str($name);
54 3         6  
  2         4  
  1         2  
  1         2  
  1         2  
  1         3  
  1         2  
  1         2  
  2         3  
  1         2  
  1         1  
  1         2  
  1         2  
  2         3  
55 3 50       12 my $text = "#line 49 \"". __FILE__. "\"
  2 100       19  
  1 50       4  
  1 50       6  
  1 50       13  
  1 50       6  
  1 50       3  
  1 50       4  
  2 50       9  
  1 50       5  
  1 50       9  
  1 50       5  
  1 50       9  
  2 50       9  
  15         95  
56 0   0     0 sub { my ( \$params ) = \@_;
  1   50     4  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
  0   0     0  
57 0         0 my ( \$value, \$rule, \$strict_forms, \$regular_forms ) = (
  1         261  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
58 0         0 \$params->{ $name },
  1         975  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
59             \@{ \$Locale::Babelfish::Phrase::PluralForms::sub_data[$index] },
60             );
61 3   33     22 my \$r;
  1   33     16  
  1   33     6  
  1   33     7  
  1   33     10  
  1   33     7  
  1   33     6  
  1   33     8  
  2   33     25  
  1   33     7  
  1   33     10  
  1   33     8  
  1   33     15  
  2   33     17  
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
      33        
62             unless ( Scalar::Util::looks_like_number(\$value) ) {
63 3 50 50     31 \$value //= 'undef';
  2 50 50     30  
  1 50 50     17  
  1 50 50     17  
  1 50 50     31  
  1 50 50     40  
  1 50 50     16  
  1 50 50     19  
  2 50 50     36  
  1 50 50     38  
  1 50 50     32  
  1 50 50     33  
  1 50 50     15  
  2 50 50     35  
64             Carp::cluck( \"$name parameter is not numeric: \$value\" );
65             \$r = \$regular_forms->[ -1 ];
66             }
67             else {
68             \$r = \$strict_forms->{\$value} // \$regular_forms->[ \$rule->(\$value) ] // \$regular_forms->[ -1 ];
69             }
70             return ref(\$r) ? \$r->(\$params) : ( \$r // '' );
71             }";
72              
73 15         3745 return eval $text;
74             }
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Locale::Babelfish::Phrase::PluralForms - Babelfish AST pluralization node.
87              
88             =head1 VERSION
89              
90             version 2.10
91              
92             =head1 METHODS
93              
94             =head2 to_perl_sub
95              
96             $node->to_perl_sub
97              
98             Return sub that represents current node execution.
99              
100             =head1 AUTHORS
101              
102             =over 4
103              
104             =item *
105              
106             Akzhan Abdulin <akzhan@cpan.org>
107              
108             =item *
109              
110             Igor Mironov <grif@cpan.org>
111              
112             =item *
113              
114             Victor Efimov <efimov@reg.ru>
115              
116             =item *
117              
118             REG.RU LLC
119              
120             =item *
121              
122             Kirill Sysoev <k.sysoev@me.com>
123              
124             =item *
125              
126             Alexandr Tkach <tkach@reg.ru>
127              
128             =back
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is Copyright (c) 2014 by REG.RU LLC.
133              
134             This is free software, licensed under:
135              
136             The MIT (X11) License
137              
138             =cut