Fluent/ResourceGroup.pm

filter: Statementstmt 85.2% 116 / 136 Branchbran 58.3% 42 / 72 Conditioncond 51.3% 19 / 37 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 100% 15 / 15 Podpod 83.3% 5 / 6 totaltotal 69.3% 201 / 290 CC 57 SCAR 26.0
CC
57
Cov
72%
_find_resource
28.8
slurp_directory
16.2
slurp_file
12.2
CRAP 13.4
↑ Summary Fluent/Elements/Base.pm »
1 package Translate::Fluent::ResourceGroup;
2  
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 has sets => (
6 is => 'rw',
7 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
8 6 );
9  
10 has fallback_order => (
11 is => 'ro',
12 3 ▶ default => sub { ['language' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 6 );
14  
15 has default_language => (
16 is => 'ro',
17 3 ▶ default => sub { 'en' },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 6 );
19  
20 6 ▶use Translate::Fluent::ResourceGroup::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Translate::Fluent::ResourceSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶sub __build_self {
Subroutine __build_self: called CC=3 SCAR=12.3
24 2 my ($class, $context) = @_;
25
26 2 my %params = ();
27 2 ▶ if ($context->{default_language}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"default_language"}) 0 2 2
28 0 $params{default_language} = delete $context->{default_language};
29 }
30 2 ▶ if ($context->{fallback_order}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"fallback_order"}) 0 2 2
31 0 $params{fallback_order} = delete $context->{fallback_order};
32 }
33  
34 2 return $class->new( %params );
35 }
36  
37 14 ▶sub slurp_file {
Subroutine slurp_file: called CC=10 SCAR=25.1
Pod undocumented
38 14 my ($self, $fname, $context) = @_;
39  
40 14 ▶ $self = $self->__build_self( $context )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 14 14
41 unless ref $self;
42
43 14 local $/ = undef;
44 14 ▶ open my $fh, $fname or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $fname or do { warn "Error opening file '${fname}'"; next } 14 0 14
45 0 warn "Error opening file '$fname'";
46 0 next;
47 };
48  
49 14 my $fluent = <$fh>;
50 14 ▶ next unless $fluent; # is the file empty?
Branch: 1 branch
BranchTrueFalseTotal
unless $fluent 0 14 14
51  
52 14 ▶ if (my ($ctx) = $fluent =~ m{ \A\#\s*context:\s*([^\n]+)\n }x) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ctx) = $fluent =~ / \A\#\s*context:\s*([^\n]+)\n /x) 12 2 14
53 12 my %fcontext;
54
55 12 my @fcontext = map { split /\s*[:=]\s*/, $_, 2 } split /\s*[:=]\s/, $ctx;
56  
57 12 ▶ if ( !( scalar @fcontext % 2) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @fcontext % 2) { } 12 0 12
58 12 %fcontext = @fcontext;
59  
60 $context->{ $_ } //= $fcontext{ $_ }
61 12 ▶ for keys %fcontext;
Condition33%1/3
$context->{$_} //= $fcontext{$_}
l!l&&r!l&&!r
0120
MC/DC0%0/2
$context->{$_} //= $fcontext{$_}
$context->{$_} $fcontext{$_}
Truth table33%1/3
A //= B
A= $context->{$_}B= $fcontext{$_}
ABresult
1X1
011
000
62 } else {
63 0 print STDERR "invalid context: '$ctx'\n";
64 0 warn "Invalid context in fluent file '$fname' - ignoring";
65 }
66 }
67  
68 14 my $resset = Translate::Fluent::Parser::parse_string( $fluent );
69 14 ▶ if ($resset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resset) 12 2 14
70 12 $self->add_resource_set( $resset, $context );
71 }
72  
73 14 ▶ return $self if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 14 14
74  
75 14 return;
76 }
77  
78 2 ▶sub slurp_directory {
Subroutine slurp_directory: called CC=11 SCAR=27.9
Pod documented
79 2 my ($self, $directory, $context) = @_;
80  
81 2 ▶ $self = $self->__build_self( $context )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 0 2
82 unless ref $self;
83
84 2 for my $k (qw(default_language fallback_order)) {
85 die "'$k' is invalid when adding resources to a resource group"
86 4 ▶ if $context->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $context->{$k} 0 4 4
87 }
88  
89 2 ▶ my $recursive = delete $context->{recursive} // 0;
Condition50%1/2
delete $context->{"recursive"} // 0
l!l
02
MC/DC0%0/1
delete $context->{"recursive"} // 0
delete $context->{"recursive"}
Truth table50%1/2
A
A= delete $context->{"recursive"}
Aresult
11
00
90  
91 2 ▶ $directory .='/' unless substr($directory, -1) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($directory, -1) eq "/" 2 0 2
92  
93 2 ▶ opendir my $dh, $directory or die "Can't open $directory: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $directory or die "Can't open ${directory}: $!" 2 0 2
94 2 my @all = readdir( $dh );
95 2 my @files = grep { m{ \.flt\z }xi } @all;
96 2 closedir( $dh );
97  
98 # first we slurp the subdirectories, so that the files
99 # on the top directories override the files in the sub directories
100 # I could go both ways, but I think it is more intuitive this way
101 # TODO: add directory priority to the docs of slurp_directory
102 2 ▶ if ($recursive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive) 0 2 2
103 0 ▶ my @dirs = grep { !m{\A\.} and -d "$directory$_" } @all;
Condition0%0/3
!/\A\./ && -d "$directory$_"
!ll&&!rl&&r
000
MC/DC0%0/2
!/\A\./ && -d "$directory$_"
!/\A\./ -d "$directory$_"
Truth table0%0/3
not(A) && B
A= !/\A\./B= -d "$directory$_"
ABresult
0X0
100
111
104  
105 0 for my $dir ( @dirs ) {
106 0 $self->slurp_directory( "$directory$dir", $context );
107 }
108 }
109  
110 2 for my $fname ( @files ) {
111 14 my %context = %{$context};
112
113 14 my $fname = "$directory$fname";
114 14 $self->slurp_file( $fname, \%context );
115 }
116  
117 2 return $self;
118 }
119  
120 #TODO: add override priority to add_resource_set docs.
121 13 ▶sub add_resource_set {
Subroutine add_resource_set: called CC=6 SCAR=19.2
Pod documented
122 13 my ($self, $resource_set, $context ) = @_;
123  
124 13 my @kv = ();
125 13 for my $fbo (@{ $self->fallback_order }) {
126 13 ▶ my $fbok = $context->{ $fbo }
Branch: 1 branch
BranchTrueFalseTotal
$fbo eq 'language' ? : 3 0 3
Condition66%2/3
$context->{$fbo} || ($fbo eq 'language' ? 'dev' : 'default')
l!l&&r!l&&!r
1030
MC/DC0%0/2
$context->{$fbo} || ($fbo eq 'language' ? 'dev' : 'default')
$context->{$fbo} ($fbo eq 'language' ? 'dev' : 'default')
Truth table66%2/3
A || B
A= $context->{$fbo}B= ($fbo eq 'language' ? 'dev' : 'default')
ABresult
1X1
011
000
127 || ($fbo eq 'language' ? 'dev' : 'default');
128
129 13 push @kv, $fbok;
130 }
131  
132 13 my $key = join '>', @kv;
133  
134 13 my $reset = $self->sets->{ $key };
135 13 ▶ if ( $reset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reset) { } 0 13 13
136 0 for my $k (keys %{ $resource_set->resources }) {
137 0 $reset->add_resource( $resource_set->resources->{ $k } );
138 }
139  
140 } else {
141 13 $self->sets->{ $key } = $resource_set;
142  
143 }
144  
145 13 return;
146 }
147  
148 15 ▶sub translate {
Subroutine translate: called CC=8 SCAR=22.0
Pod documented
149 15 my ($self, $res_id, $variables, $context) = @_;
150  
151 $context = $variables->{__context}
152 15 ▶ if !$context and $variables->{__context};
Branch: 1 branch
BranchTrueFalseTotal
if not $context and $variables->{"__context"} 0 15 15
Condition66%2/3
not $context and $variables->{"__context"}
!ll&&!rl&&r
870
MC/DC0%0/2
not $context and $variables->{"__context"}
not $context $variables->{"__context"}
Truth table66%2/3
not(A) and B
A= not $contextB= $variables->{"__context"}
ABresult
0X0
100
111
153  
154 15 my $_ctx;
155 15 ▶ if (ref $context eq 'Translate::Fluent::ResourceGroup::Context') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $context eq "Translate::Fluent::ResourceGroup::Context") 0 15 15
156 0 $_ctx = $context;
157 0 $context = $_ctx->context;
158 }
159  
160 15 my $res = $self->_find_resource( $res_id, $context );
161  
162 15 ▶ return unless $res and $res->isa("Translate::Fluent::Elements::Message");
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->isa("Translate::Fluent::Elements::Message") 2 13 15
Condition100%3/3
$res and $res->isa("Translate::Fluent::Elements::Message")
!ll&&!rl&&r
1113
MC/DC100%2/2
$res and $res->isa("Translate::Fluent::Elements::Message")
$res $res->isa("Translate::Fluent::Elements::Message")
Truth table100%3/3
A and B
A= $resB= $res->isa("Translate::Fluent::Elements::Message")
ABresult
0X0
100
111
163  
164 13 ▶ $_ctx ||= Translate::Fluent::ResourceGroup::Context->new(
Condition33%1/3
$_ctx ||= "Translate::Fluent::ResourceGroup::Context"->new("context", $context, "resgroup", $self)
l!l&&r!l&&!r
0130
MC/DC0%0/2
$_ctx ||= "Translate::Fluent::ResourceGroup::Context"->new("context", $context, "resgroup", $self)
$_ctx "Translate::Fluent::ResourceGroup::Context"->new("context", $context, "resgroup", $self)
Truth table33%1/3
A ||= B
A= $_ctxB= "Translate::Fluent::ResourceGroup::Context"->new("context", $context, "resgroup", $self)
ABresult
1X1
011
000
165 context => $context,
166 resgroup => $self,
167 );
168
169 13 ▶ return $res->translate({ %{$variables//{}}, __resourceset => $_ctx });
Condition100%2/2
$variables // {}
l!l
112
MC/DC100%1/1
$variables // {}
$variables
Truth table100%2/2
A
A= $variables
Aresult
11
00
170 }
171  
172 12 ▶sub get_term {
Subroutine get_term: called CC=2 SCAR=6.9
Pod documented
173 12 my ($self, $term_id, $context) = @_;
174  
175 12 my $term = $self->_find_resource( $term_id, $context );
176  
177 12 ▶ return unless $term->isa("Translate::Fluent::Elements::Term");
Branch: 1 branch
BranchTrueFalseTotal
unless $term->isa("Translate::Fluent::Elements::Term") 1 11 12
178  
179 11 return $term;
180 }
181  
182 3 ▶sub get_message {
Subroutine get_message: called CC=2 SCAR=7.0
Pod documented
183 3 my ($self, $message_id, $context) = @_;
184  
185 3 my $res = $self->_find_resource( $message_id, $context );
186  
187 3 ▶ return unless $res->isa("Translate::Fluent::Elements::Message");
Branch: 1 branch
BranchTrueFalseTotal
unless $res->isa("Translate::Fluent::Elements::Message") 0 3 3
188  
189 3 return $res;
190 }
191  
192 30 ▶sub _find_resource {
Subroutine _find_resource: called CC=16 SCAR=33.6
193 30 my ($self, $res_id, $context) = @_;
194  
195 30 ▶ my $lang = $context->{language} || $self->default_language;
Condition66%2/3
$context->{'language'} || $self->default_language
l!l&&r!l&&!r
13170
MC/DC0%0/2
$context->{'language'} || $self->default_language
$context->{'language'} $self->default_language
Truth table66%2/3
A || B
A= $context->{'language'}B= $self->default_language
ABresult
1X1
011
000
196 30 my %ctx = ();
197 30 my @fborder = @{ $self->fallback_order };
198 30 for my $fb ( @fborder ) {
199 30 ▶ $ctx{ $fb } = $context->{ $fb }
Branch: 1 branch
BranchTrueFalseTotal
$fb eq 'language' ? : 17 0 17
Condition66%2/3
$context->{$fb} || ($fb eq 'language' ? $lang : 'default')
l!l&&r!l&&!r
13170
MC/DC0%0/2
$context->{$fb} || ($fb eq 'language' ? $lang : 'default')
$context->{$fb} ($fb eq 'language' ? $lang : 'default')
Truth table66%2/3
A || B
A= $context->{$fb}B= ($fb eq 'language' ? $lang : 'default')
ABresult
1X1
011
000
200 || (($fb eq 'language') ? $lang : 'default');
201 }
202 0 my %fbnext = map { $fborder[$_-1] => $fborder[$_] } 1..$#fborder;
203 30 my $fbnext = ($fborder[0]);
204  
205 30 my $res;
206  
207 RESSET:
208 30 ▶ while (!$res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 70 0 70
209 70 my $key = join '>', map { $ctx{$_} } @fborder;
210 # use Data::Dumper;
211 # print STDERR "checking '$key' => ", Dumper( \%ctx );
212
213 70 ▶ if ( my $rset = $self->sets->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rset = $self->sets->{$key}) 31 39 70
214 31 ▶ last RESSET if $res = $rset->resources->{ $res_id };
Branch: 1 branch
BranchTrueFalseTotal
if $res = $rset->resources->{$res_id} 29 2 31
215 }
216  
217 41 ▶ my $fbnext_default = $fbnext eq 'language' ? 'dev' : 'default';
Branch: 1 branch
BranchTrueFalseTotal
$fbnext eq 'language' ? : 41 0 41
218 41 ▶ if ($ctx{ $fbnext } eq $fbnext_default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx{$fbnext} eq $fbnext_default) { } 1 40 41
219 do {
220 last RESSET
221 1 ▶ unless $fbnext{ $fbnext }; #no where else to look for
Branch: 1 branch
BranchTrueFalseTotal
unless $fbnext{$fbnext} 1 0 1
222  
223 0 ▶ $ctx{ $fbnext } = $context->{ $fbnext }
Branch: 1 branch
BranchTrueFalseTotal
$context->{$fbnext} || $fbnext eq 'language' ? : 0 0 0
Condition0%0/3
$context->{$fbnext} or $fbnext eq "language"
l!l&&r!l&&!r
000
MC/DC0%0/2
$context->{$fbnext} or $fbnext eq "language"
$context->{$fbnext} $fbnext eq "language"
Truth table0%0/3
A or B
A= $context->{$fbnext}B= $fbnext eq "language"
ABresult
1X1
011
000
224 || $fbnext eq 'language' ? $lang : 'default';
225  
226 0 $fbnext = $fbnext{ $fbnext };
227  
228 1 ▶ } until $ctx{ $fbnext } ne $fbnext_default;
Branch: 1 branch
BranchTrueFalseTotal
$ctx{$fbnext} ne $fbnext_default or do { do { last RESSET unless $fbnext{$fbnext}; $ctx{$fbnext} = $context->{$fbnext} || $fbnext eq "language" ? $lang : "default"; $fbnext = $fbnext{$fbnext} } } 0 0 0
229  
230 $ctx{ $fbnext } = $fbnext eq 'language'
231 0 ▶ ? ( $self->__fallback_languages( $ctx{language} ) )[1]
Branch: 1 branch
BranchTrueFalseTotal
$fbnext eq 'language' ? : 0 0 0
232 : 'default';
233 0 $fbnext = $fborder[0];
234  
235 } else {
236 $ctx{ $fbnext } = $fbnext eq 'language'
237 40 ▶ ? ( $self->__fallback_languages( $ctx{language} ) )[1]
Branch: 1 branch
BranchTrueFalseTotal
$fbnext eq 'language' ? : 40 0 40
238 : 'default';
239 }
240 };
241  
242 30 return $res;
243 }
244  
245 40 ▶sub __fallback_languages {
Subroutine __fallback_languages: called CC=7 SCAR=20.1
246 40 my ($self, $lang, $default_lang) = @_;
247  
248 40 ▶ $default_lang ||= $self->default_language;
Condition33%1/3
$default_lang ||= $self->default_language
l!l&&r!l&&!r
0400
MC/DC0%0/2
$default_lang ||= $self->default_language
$default_lang $self->default_language
Truth table33%1/3
A ||= B
A= $default_langB= $self->default_language
ABresult
1X1
011
000
249  
250 40 ▶ my @langs = ($lang) if $lang;
Branch: 1 branch
BranchTrueFalseTotal
if $lang 40 0 40
251  
252 40 ▶ while ($lang and $lang=~m{\-}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $lang =~ /\-/) 4 40 44
Condition66%2/3
$lang and $lang =~ /\-/
!ll&&!rl&&r
0404
MC/DC50%1/2
$lang and $lang =~ /\-/
$lang $lang =~ /\-/
Truth table66%2/3
A and B
A= $langB= $lang =~ /\-/
ABresult
0X0
100
111
253 4 $lang =~ s{-\w+\z}{};
254 4 push @langs, $lang;
255 }
256 40 ▶ unless ($lang eq $default_lang) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang eq $default_lang) 13 27 40
257 13 push @langs, $default_lang;
258 }
259  
260 40 ▶ push @langs, 'dev' unless $default_lang eq 'dev';
Branch: 1 branch
BranchTrueFalseTotal
unless $default_lang eq "dev" 40 0 40
261  
262 40 return @langs;
263 }
264  
265 6 1;
266  
267 __END__
↑ Summary Fluent/Elements/Base.pm »