blib/lib/Punk/Plugin/I18n.pm

filter: Statementstmt 100% 53 / 53 Branchbran 77.7% 14 / 18 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 88.6% 78 / 88 CC 13 SCAR 22.0
CC
13
Cov
91%
register
13.4
BEGIN
1.0
BEGIN
1.0
CRAP 9.0
« blib/lib/Punk/Generate.pm ↑ Summary blib/lib/Punk/Upload.pm »
1 package Punk::Plugin::I18n;
2  
3 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = '0.69';
8  
9 12 ▶use Punk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use File::Raw ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # Loaded for its side effect: it registers the 'json' plugin that
13 # File::Raw::slurp uses below. Nothing is imported.
14 12 ▶use File::Raw::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 265 ▶sub register {
Subroutine register: called CC=13 SCAR=25.9
Pod documented
17 265 my ($class, $app, $opts) = @_;
18 265 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
2650
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
19 265 ▶ ref $opts eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq "HASH" or die "Punk::Plugin::I18n: options must be a hashref\n" 265 0 265
20 or die "Punk::Plugin::I18n: options must be a hashref\n";
21  
22 265 my $dir = $opts->{dir};
23 265 ▶ defined $dir && length $dir
Branch: 1 branch
BranchTrueFalseTotal
defined $dir and length $dir or die "Punk::Plugin::I18n: `dir` is required - it is where the catalogues live (plugin 'I18n' => { dir => 'i18n', default => 'en' })\n" 265 0 265
Condition33%1/3
defined $dir and length $dir
!ll&&!rl&&r
00265
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table33%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
24 or die "Punk::Plugin::I18n: `dir` is required - it is where the "
25 . "catalogues live (plugin 'I18n' => { dir => 'i18n', "
26 . "default => 'en' })\n";
27 265 ▶ -d $dir
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or die "Punk::Plugin::I18n: the catalogue directory '${dir}' does not " . 'exist - a missing directory is a deploy that shipped without ' . "its translations, and it should fail here rather than serve " . "every page in the default language\n" 154 111 265
28 or die "Punk::Plugin::I18n: the catalogue directory '$dir' does not "
29 . "exist - a missing directory is a deploy that shipped without "
30 . "its translations, and it should fail here rather than serve "
31 . "every page in the default language\n";
32  
33 154 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Punk::Plugin::I18n: cannot read '${dir}': $!\n" 154 0 154
34 or die "Punk::Plugin::I18n: cannot read '$dir': $!\n";
35 154 my @files = sort grep { /\.json\z/ } readdir $dh;
36 154 closedir $dh;
37  
38 @files
39 154 ▶ or die "Punk::Plugin::I18n: no *.json catalogues in '$dir' - a plugin "
Branch: 1 branch
BranchTrueFalseTotal
@files or die "Punk::Plugin::I18n: no *.json catalogues in '${dir}' - a plugin " . 'with nothing to translate is a typo in `dir`, not a working ' . "default\n" 153 1 154
40 . "with nothing to translate is a typo in `dir`, not a working "
41 . "default\n";
42  
43 153 my %cats;
44 153 for my $f (@files) {
45 197 (my $tag = $f) =~ s/\.json\z//;
46 197 $tag =~ tr/A-Z/a-z/;
47 197 my $path = File::Spec->catfile($dir, $f);
48  
49 197 my $data = eval { File::Raw::slurp($path, plugin => 'json') };
50 197 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 196 197
51 1 my $why = $@;
52 1 $why =~ s/\s+\z//;
53 1 ▶ die -r $path
Branch: 1 branch
BranchTrueFalseTotal
-r $path ? : 1 0 1
54 ? "Punk::Plugin::I18n: '$path' will not parse: $why\n"
55 : "Punk::Plugin::I18n: cannot read '$path': $!\n";
56 }
57 196 ▶ ref $data eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or die "Punk::Plugin::I18n: '${path}' is not a JSON object - a " . "catalogue is a map of key to translation\n" 195 1 196
58 or die "Punk::Plugin::I18n: '$path' is not a JSON object - a "
59 . "catalogue is a map of key to translation\n";
60 195 $cats{$tag} = $data;
61 }
62  
63 151 my $ok = eval { $class->_build($app, \%cats, $opts); 1 };
64 151 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 33 118 151
65  
66 118 my $why = $@;
67 118 $why =~ s/\s+\z//;
68 118 die "$why (catalogues from '$dir')\n";
69 }
70  
71 12 1;
72  
73 __END__
« blib/lib/Punk/Generate.pm ↑ Summary blib/lib/Punk/Upload.pm »