| 1 | package Locale::Utils::PluralForms; ## no critic (TidyCode) | ||||||||||||
| 2 | |||||||||||||
| 3 | 6 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 6 | ▶ | use MooseX::StrictConstructor; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 6 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 6 | ▶ | use English qw(-no_match_vars $EVAL_ERROR); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 6 | ▶ | use HTML::Entities qw(decode_entities); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 6 | require LWP::UserAgent; | |||||||||||
| 11 | 6 | require Safe; | |||||||||||
| 12 | |||||||||||||
| 13 | 6 | our $VERSION = '0.003'; | |||||||||||
| 14 | |||||||||||||
| 15 | 6 | has language => ( | |||||||||||
| 16 | is => 'rw', | ||||||||||||
| 17 | isa => 'Str', | ||||||||||||
| 18 | trigger => \&_language, | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 6 | has _all_plural_forms_url => ( | |||||||||||
| 22 | is => 'rw', | ||||||||||||
| 23 | isa => 'Str', | ||||||||||||
| 24 | default => 'http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html', | ||||||||||||
| 25 | ); | ||||||||||||
| 26 | |||||||||||||
| 27 | 6 | has _all_plural_forms_html => ( | |||||||||||
| 28 | is => 'rw', | ||||||||||||
| 29 | isa => 'Str', | ||||||||||||
| 30 | default => \&_get_all_plural_forms_html, | ||||||||||||
| 31 | lazy => 1, | ||||||||||||
| 32 | clearer => 'clear_all_plural_forms_html', | ||||||||||||
| 33 | ); | ||||||||||||
| 34 | |||||||||||||
| 35 | 6 | has all_plural_forms => ( | |||||||||||
| 36 | is => 'rw', | ||||||||||||
| 37 | isa => 'HashRef', | ||||||||||||
| 38 | default => \&_get_all_plural_forms, | ||||||||||||
| 39 | lazy => 1, | ||||||||||||
| 40 | ); | ||||||||||||
| 41 | |||||||||||||
| 42 | 6 | has plural_forms => ( | |||||||||||
| 43 | is => 'rw', | ||||||||||||
| 44 | isa => 'Str', | ||||||||||||
| 45 | default => 'nplurals=1; plural=0', | ||||||||||||
| 46 | lazy => 1, | ||||||||||||
| 47 | trigger => \&_calculate_plural_forms, | ||||||||||||
| 48 | ); | ||||||||||||
| 49 | |||||||||||||
| 50 | 6 | has nplurals => ( | |||||||||||
| 51 | is => 'rw', | ||||||||||||
| 52 | isa => 'Int', | ||||||||||||
| 53 | default => 1, | ||||||||||||
| 54 | lazy => 1, | ||||||||||||
| 55 | init_arg => undef, | ||||||||||||
| 56 | writer => '_nplurals', | ||||||||||||
| 57 | ); | ||||||||||||
| 58 | |||||||||||||
| 59 | has plural_code => ( | ||||||||||||
| 60 | is => 'rw', | ||||||||||||
| 61 | isa => 'CodeRef', | ||||||||||||
| 62 | 1 | ▶ | default => sub { return sub { return 0 } }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 6 | lazy => 1, | |||||||||||
| 64 | init_arg => undef, | ||||||||||||
| 65 | writer => '_plural_code', | ||||||||||||
| 66 | ); | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | sub _get_all_plural_forms_html { | ||||||||||
|
Subroutine
_get_all_plural_forms_html: not called CC=2 SCAR=17.9
| |||||||||||||
| 69 | 0 | my $self = shift; | |||||||||||
| 70 | |||||||||||||
| 71 | 0 | my $url = $self->_all_plural_forms_url; | |||||||||||
| 72 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||
| 73 | 0 | $ua->env_proxy; | |||||||||||
| 74 | 0 | my $response = $ua->get($url); | |||||||||||
| 75 | 0 | ▶ | $response->is_success | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | or confess "$url $response->status_line"; | ||||||||||||
| 77 | |||||||||||||
| 78 | 0 | return $response->decoded_content; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | sub _get_all_plural_forms { | ||||||||||
|
Subroutine
_get_all_plural_forms: called CC=2 SCAR=6.9
| |||||||||||||
| 82 | 1 | my $self = shift; | |||||||||||
| 83 | |||||||||||||
| 84 | ## no critic (ComplexRegexes) | ||||||||||||
| 85 | 1 | my @match = $self->_all_plural_forms_html =~ m{ | |||||||||||
| 86 | <tr \s+ class="row- (?: even | odd ) "> | ||||||||||||
| 87 | \s* | ||||||||||||
| 88 | <td> ( [^<>]+ ) </td> | ||||||||||||
| 89 | \s* | ||||||||||||
| 90 | <td> ( [^<>]+ ) .*? </td> | ||||||||||||
| 91 | \s* | ||||||||||||
| 92 | <td> ( nplurals [^<>]+? ) [;]? </td> | ||||||||||||
| 93 | \s* | ||||||||||||
| 94 | </tr> | ||||||||||||
| 95 | }xmsg; | ||||||||||||
| 96 | ## use critic(ComplexRegexes) | ||||||||||||
| 97 | 1 | $self->clear_all_plural_forms_html; | |||||||||||
| 98 | 1 | my %all_plural_forms; | |||||||||||
| 99 | 1 | ▶ | while ( my ($iso, $english_name, $plural_forms) = splice @match, 0, 3 ) { ## no critic (MagicNumbers) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 3 | $english_name =~ s{ \s+ \z }{}xms; | |||||||||||
| 101 | 3 | $all_plural_forms{ decode_entities($iso) } = { | |||||||||||
| 102 | english_name => decode_entities($english_name), | ||||||||||||
| 103 | plural_forms => decode_entities($plural_forms), | ||||||||||||
| 104 | }; | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 1 | return \%all_plural_forms; | |||||||||||
| 108 | } | ||||||||||||
| 109 | |||||||||||||
| 110 | 4 | ▶ | sub _language { | ||||||||||
|
Subroutine
_language: called CC=3 SCAR=11.0
| |||||||||||||
| 111 | 4 | my ($self, $language) = @_; | |||||||||||
| 112 | |||||||||||||
| 113 | 4 | my $all_plural_forms = $self->all_plural_forms; | |||||||||||
| 114 | 4 | ▶ | if ( exists $all_plural_forms->{$language} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | return $self->plural_forms( | ||||||||||||
| 116 | $all_plural_forms->{$language}->{plural_forms} | ||||||||||||
| 117 | 2 | ); | |||||||||||
| 118 | } | ||||||||||||
| 119 | 2 | $language =~ s{_ .* \z}{}xms; | |||||||||||
| 120 | 2 | ▶ | if ( exists $all_plural_forms->{$language} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | return $self->plural_forms( | ||||||||||||
| 122 | $all_plural_forms->{$language}->{plural_forms} | ||||||||||||
| 123 | 1 | ); | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 1 | return confess | |||||||||||
| 127 | "Missing plural forms for language $language in all_plural_forms"; | ||||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 6 | ▶ | sub _calculate_plural_forms { | ||||||||||
|
Subroutine
_calculate_plural_forms: called CC=3 SCAR=11.1
| |||||||||||||
| 131 | 6 | my $self = shift; | |||||||||||
| 132 | |||||||||||||
| 133 | 6 | my $plural_forms = $self->plural_forms; | |||||||||||
| 134 | 6 | $plural_forms =~ s{\b ( nplurals | plural | n ) \b}{\$$1}xmsg; | |||||||||||
| 135 | 6 | my $safe = Safe->new; | |||||||||||
| 136 | 6 | my $nplurals_code = <<"EOC"; | |||||||||||
| 137 | my \$n = 0; | ||||||||||||
| 138 | my (\$nplurals, \$plural); | ||||||||||||
| 139 | $plural_forms; | ||||||||||||
| 140 | \$nplurals; | ||||||||||||
| 141 | EOC | ||||||||||||
| 142 | 6 | ▶ | $self->_nplurals( | ||||||||||
|
Condition50%1/2
$safe->reval($nplurals_code) || confess("Code of Plural-Forms $plural_forms is not safe, $Locale::Utils::PluralForms::EVAL_ERROR")
MC/DC0%0/1
$safe->reval($nplurals_code) || confess("Code of Plural-Forms $plural_forms is not safe, $Locale::Utils::PluralForms::EVAL_ERROR")
$safe->reval($nplurals_code)
Truth table50%1/2
A
A= $safe->reval($nplurals_code)
| |||||||||||||
| 143 | $safe->reval($nplurals_code) | ||||||||||||
| 144 | or confess | ||||||||||||
| 145 | "Code of Plural-Forms $plural_forms is not safe, $EVAL_ERROR" | ||||||||||||
| 146 | ); | ||||||||||||
| 147 | 6 | my $plural_code = <<"EOC"; | |||||||||||
| 148 | sub { | ||||||||||||
| 149 | my \$n = shift; | ||||||||||||
| 150 | my (\$nplurals, \$plural); | ||||||||||||
| 151 | $plural_forms; | ||||||||||||
| 152 | return 0 + (\$plural || 0); | ||||||||||||
| 153 | } | ||||||||||||
| 154 | EOC | ||||||||||||
| 155 | 6 | ▶ | $self->_plural_code( | ||||||||||
|
Condition50%1/2
$safe->reval($plural_code) || confess("Code $plural_forms is not safe, $Locale::Utils::PluralForms::EVAL_ERROR")
MC/DC0%0/1
$safe->reval($plural_code) || confess("Code $plural_forms is not safe, $Locale::Utils::PluralForms::EVAL_ERROR")
$safe->reval($plural_code)
Truth table50%1/2
A
A= $safe->reval($plural_code)
| |||||||||||||
| 156 | $safe->reval($plural_code) | ||||||||||||
| 157 | or confess "Code $plural_forms is not safe, $EVAL_ERROR" | ||||||||||||
| 158 | ); | ||||||||||||
| 159 | |||||||||||||
| 160 | 6 | return $self; | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 164 | |||||||||||||
| 165 | 6 | 1; | |||||||||||
| 166 | |||||||||||||
| 167 | __END__ | ||||||||||||