lib/Web/ComposableRequest/Role/L10N.pm

filter: Statementstmt 95.6% 110 / 115 Branchbran 70.0% 21 / 30 Conditioncond 67.6% 23 / 34 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 22 / 22 Podpod 100% 2 / 2 totaltotal 83.4% 187 / 224 CC 31 SCAR 18.0
CC
31
Cov
86%
__ANON__
11.4
__ANON__
8.2
_localise_args
8.1
CRAP 6.1
« lib/Web/ComposableRequest/Base.pm ↑ Summary lib/Web/ComposableRequest/Util.pm »
1 package Web::ComposableRequest::Role::L10N;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Web::ComposableRequest::Constants qw( NUL TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Web::ComposableRequest::Util qw( extract_lang is_member
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 add_config_role );
8 1 ▶use Unexpected::Functions qw( inflate_placeholders );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Unexpected::Types qw( ArrayRef CodeRef NonEmptySimpleStr
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 Undef );
11 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 requires qw( query_params _config _env );
14  
15 1 add_config_role __PACKAGE__.'::Config';
16  
17 # Attribute constructors
18 4 ▶my $_build_locale = sub {
Subroutine __ANON__: called CC=11 SCAR=24.3
19 4 my $self = shift;
20 4 my $conf = $self->_config;
21 4 my $locale = $self->query_params->('locale', { optional => TRUE });
22  
23 4 ▶ return $locale if $locale and is_member $locale, $conf->locales;
Branch: 1 branch
BranchTrueFalseTotal
if $locale and &is_member($locale, $conf->locales) 2 2 4
Condition100%3/3
$locale and &is_member($locale, $conf->locales)
!ll&&!rl&&r
112
MC/DC100%2/2
$locale and &is_member($locale, $conf->locales)
$locale &is_member($locale, $conf->locales)
Truth table100%3/3
A and B
A= $localeB= &is_member($locale, $conf->locales)
ABresult
0X0
100
111
24  
25 2 my $lang;
26  
27 2 ▶ if ($locale and $lang = extract_lang($locale)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale and $lang = &extract_lang($locale)) 1 1 2
Condition66%2/3
$locale and $lang = &extract_lang($locale)
!ll&&!rl&&r
101
MC/DC50%1/2
$locale and $lang = &extract_lang($locale)
$locale $lang = &extract_lang($locale)
Truth table66%2/3
A and B
A= $localeB= $lang = &extract_lang($locale)
ABresult
0X0
100
111
28 1 ▶ return $lang if $lang ne $locale and is_member $lang, $conf->locales;
Branch: 1 branch
BranchTrueFalseTotal
if $lang ne $locale and &is_member($lang, $conf->locales) 0 1 1
Condition33%1/3
$lang ne $locale and &is_member($lang, $conf->locales)
!ll&&!rl&&r
100
MC/DC0%0/2
$lang ne $locale and &is_member($lang, $conf->locales)
$lang ne $locale &is_member($lang, $conf->locales)
Truth table33%1/3
A and B
A= $lang ne $localeB= &is_member($lang, $conf->locales)
ABresult
0X0
100
111
29 }
30  
31 2 for my $locale (@{$self->locales}) {
32 3 ▶ return $locale if is_member $locale, $conf->locales;
Branch: 1 branch
BranchTrueFalseTotal
if &is_member($locale, $conf->locales) 1 2 3
33 }
34  
35 1 for my $lang (map { extract_lang $_ } @{$self->locales}) {
36 1 ▶ return $lang if is_member $lang, $conf->locales;
Branch: 1 branch
BranchTrueFalseTotal
if &is_member($lang, $conf->locales) 0 1 1
37 }
38  
39 1 return $conf->locale;
40 1 };
41  
42 2 ▶my $_build_locales = sub {
Subroutine _build_locales: called CC=3 SCAR=11.0
43 2 my $self = shift;
44 2 ▶ my $lang = $self->_env->{ 'HTTP_ACCEPT_LANGUAGE' } // NUL;
Condition50%1/2
$self->_env->{"HTTP_ACCEPT_LANGUAGE"} // ""
l!l
20
MC/DC0%0/1
$self->_env->{"HTTP_ACCEPT_LANGUAGE"} // ""
$self->_env->{"HTTP_ACCEPT_LANGUAGE"}
Truth table50%1/2
A
A= $self->_env->{"HTTP_ACCEPT_LANGUAGE"}
Aresult
11
00
45  
46 4 return [ map { s{ _ \z }{}mx; $_ }
47 4 ▶ map { join '_', $_->[ 0 ], uc( $_->[ 1 ] // NUL ) }
Condition100%2/2
$_->[1] // ""
l!l
13
MC/DC100%1/1
$_->[1] // ""
$_->[1]
Truth table100%2/2
A
A= $_->[1]
Aresult
11
00
48 4 map { [ split m{ - }mx, $_ ] }
49 2 map { ( split m{ ; }mx, $_ )[ 0 ] }
50 split m{ , }mx, lc $lang ];
51 1 };
52  
53 4 ▶my $_build_localiser = sub {
Subroutine _build_localiser: called CC=1 SCAR=0.0
54 10 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
55 10 my ($key, $args) = @_;
56  
57 10 ▶ defined $key or return; $key = "${key}"; chomp $key; $args //= {};
Branch: 1 branch
BranchTrueFalseTotal
defined $key or return 10 0 10
Condition50%1/2
$args //= {}
l!l
100
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
58  
59 10 my $text = $key;
60  
61 10 ▶ if (defined $args->{params} and ref $args->{params} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"params"} and ref $args->{"params"} eq "ARRAY") 9 1 10
Condition66%2/3
defined $args->{"params"} and ref $args->{"params"} eq "ARRAY"
!ll&&!rl&&r
109
MC/DC50%1/2
defined $args->{"params"} and ref $args->{"params"} eq "ARRAY"
defined $args->{"params"} ref $args->{"params"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $args->{"params"}B= ref $args->{"params"} eq "ARRAY"
ABresult
0X0
100
111
62 9 ▶ return $text if 0 > index $text, '[_';
Branch: 1 branch
BranchTrueFalseTotal
if index($text, "[_") == -1 1 8 9
63  
64 # Expand positional parameters of the form [_<n>]
65 return inflate_placeholders
66 [ '[?]', '[]', $args->{no_quote_bind_values} ], $text,
67 8 @{ $args->{params} };
68 }
69  
70 1 ▶ return $text if 0 > index $text, '{';
Branch: 1 branch
BranchTrueFalseTotal
if index($text, "{") == -1 0 1 1
71  
72 # Expand named parameters of the form {param_name}
73 1 my %args = %{ $args };
74 1 my $re = join '|', map { quotemeta $_ } keys %args;
75  
76 1 ▶ $text =~ s{ \{($re)\} }{ defined $args{$1} ? $args{$1} : "{${1}?}" }egmx;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$1} ? : 1 0 1
77  
78 1 return $text;
79 4 };
80 1 };
81  
82 # Public attributes
83 has 'domain' => is => 'lazy', isa => NonEmptySimpleStr | Undef,
84 1 ▶ builder => sub {};
Subroutine _build_domain: called CC=1 SCAR=0.0
85  
86 1 has 'domain_prefix' => is => 'lazy', isa => NonEmptySimpleStr | Undef;
87  
88 has 'language' => is => 'lazy', isa => NonEmptySimpleStr,
89 1 ▶ builder => sub { extract_lang $_[ 0 ]->locale };
Subroutine _build_language: called CC=1 SCAR=0.0
90  
91 1 has 'locale' => is => 'lazy', isa => NonEmptySimpleStr,
92 builder => $_build_locale;
93  
94 1 has 'locales' => is => 'lazy', isa => ArrayRef[NonEmptySimpleStr],
95 builder => $_build_locales;
96  
97 1 has 'localiser' => is => 'lazy', isa => CodeRef,
98 builder => $_build_localiser;
99  
100 1 my $_domains;
101  
102 # Public methods
103 9 ▶sub loc {
Subroutine loc: called CC=2 SCAR=7.4
Pod documented
104 9 my ($self, $key, @args) = @_;
105  
106 9 my $args = $self->_localise_args(@args);
107  
108 9 ▶ $args->{locale} //= $self->locale;
Condition33%1/3
$args->{"locale"} //= $self->locale
l!l&&r!l&&!r
090
MC/DC0%0/2
$args->{"locale"} //= $self->locale
$args->{"locale"} $self->locale
Truth table33%1/3
A //= B
A= $args->{"locale"}B= $self->locale
ABresult
1X1
011
000
109  
110 9 return $self->localiser->($key, $args);
111 }
112  
113 1 ▶sub loc_default {
Subroutine loc_default: called CC=1 SCAR=0.0
Pod documented
114 1 my ($self, $key, @args) = @_;
115  
116 1 my $args = $self->_localise_args(@args);
117  
118 1 $args->{locale} = $self->_config->locale;
119  
120 1 return $self->localiser->($key, $args);
121 }
122  
123 # Private methods
124 1 ▶sub _get_domains {
Subroutine _get_domains: called CC=4 SCAR=20.1
125 1 my $self = shift;
126 1 ▶ my $domains = [ @{$self->_config->l10n_attributes->{domains} // []} ];
Condition50%1/2
$self->_config->l10n_attributes->{"domains"} // []
l!l
10
MC/DC0%0/1
$self->_config->l10n_attributes->{"domains"} // []
$self->_config->l10n_attributes->{"domains"}
Truth table50%1/2
A
A= $self->_config->l10n_attributes->{"domains"}
Aresult
11
00
127 1 ▶ my $domain = $self->domain or return $domains;
Branch: 1 branch
BranchTrueFalseTotal
my $domain = $self->domain or return $domains 0 1 1
128 0 my $prefix = $self->domain_prefix;
129  
130 0 ▶ $domain = "${prefix}-${domain}" if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
131 0 push @{$domains}, $domain;
132  
133 0 return $domains;
134 }
135  
136 10 ▶sub _localise_args {
Subroutine _localise_args: called CC=8 SCAR=21.0
137 10 my $self = shift;
138 10 ▶ my $args = ($_[0] && ref $_[0] eq 'HASH' ) ? { %{ $_[0] } }
Branch: 2 branches
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'HASH' ? : 1 9 10
$_[0] && ref $_[0] eq 'ARRAY' ? : 3 6 9
Condition100%6/6
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
361
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
333
MC/DC100%2/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
139 : { params => ($_[0] && ref $_[0] eq 'ARRAY') ? $_[0] : [@_] };
140  
141 $args->{domains} = $_domains //= $self->_get_domains
142 10 ▶ unless exists $args->{domains};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"domains"} 10 0 10
Condition66%2/3
$_domains //= $self->_get_domains
l!l&&r!l&&!r
910
MC/DC0%0/2
$_domains //= $self->_get_domains
$_domains $self->_get_domains
Truth table66%2/3
A //= B
A= $_domainsB= $self->_get_domains
ABresult
1X1
011
000
143  
144 10 ▶ $args->{no_quote_bind_values} //= !$self->_config->quote_bind_values;
Condition50%1/2
$args->{"no_quote_bind_values"} //= !$self->_config->quote_bind_values
l!l
010
MC/DC0%0/1
$args->{"no_quote_bind_values"} //= !$self->_config->quote_bind_values
$args->{"no_quote_bind_values"}
Truth table50%1/2
A
A= $args->{"no_quote_bind_values"}
Aresult
11
00
145  
146 10 return $args;
147 }
148  
149 package Web::ComposableRequest::Role::L10N::Config;
150  
151 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 1 ▶use Web::ComposableRequest::Constants qw( LANG TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
154 1 ▶use Unexpected::Types qw( ArrayRef Bool HashRef
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 NonEmptySimpleStr );
156 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 # Public attributes
159 has 'l10n_attributes' => is => 'ro', isa => HashRef,
160 2 ▶ builder => sub { { domains => [ 'messages' ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
161  
162 1 has 'locale' => is => 'ro', isa => NonEmptySimpleStr,
163 default => LANG;
164  
165 has 'locales' => is => 'ro', isa => ArrayRef[NonEmptySimpleStr],
166 2 ▶ builder => sub { [ LANG ] };
Subroutine _build_locales: called CC=1 SCAR=0.0
167  
168 1 has 'quote_bind_values' => is => 'ro', isa => Bool, default => TRUE;
169  
170 1 1;
171  
172 __END__
« lib/Web/ComposableRequest/Base.pm ↑ Summary lib/Web/ComposableRequest/Util.pm »