Gettext/Storage.pm

filter: Statementstmt 98.7% 160 / 162 Branchbran 55.0% 33 / 60 Conditioncond 42.1% 16 / 38 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 96.3% 26 / 27 Podpod 100% 6 / 6 totaltotal 77.0% 242 / 314 CC 53 SCAR 19.3
CC
53
Cov
80%
__ANON__
14.5
__ANON__
11.0
load
7.3
CRAP 6.9
« Gettext.pm ↑ Summary Gettext/Storage/PO.pm »
1 package File::Gettext::Storage;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::DataClass::Constants qw( EXCEPTION_CLASS FALSE NUL TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::DataClass::Functions qw( is_stale merge_file_data throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::DataClass::Types qw( Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Unexpected::Functions qw( NothingUpdated Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has 'gettext' => is => 'lazy', isa => Object,
15 1 ▶ builder => sub { File::Gettext->new( builder => $_[ 0 ]->schema ) };
Subroutine _build_gettext: called CC=1 SCAR=0.0
16  
17 1 has 'schema' => is => 'ro', isa => Object, required => TRUE,
18 handles => [ qw( cache language ) ], weak_ref => TRUE;
19  
20 1 has 'storage' => is => 'ro', isa => Object, required => TRUE,
21 handles => [ qw( extn meta_pack meta_unpack
22 read_file txn_do validate_params ) ];
23  
24 # Private functions
25 5 ▶my $_get_attributes = sub {
Subroutine __ANON__: called CC=5 SCAR=18.9
26 5 my ($condition, $source) = @_;
27  
28 10 ▶ return grep { not m{ \A _ }msx
Condition44%4/9
!/ \A _ /msx && $_ ne 'id' && $_ ne 'name' && &$condition($_)
!ll&&!rl&&r
055
not / \A _ /msx and $_ ne "id" and $_ ne "name"
!ll&&!rl&&r
0010
not / \A _ /msx and $_ ne "id"
!ll&&!rl&&r
0010
MC/DC25%1/4
!/ \A _ /msx && $_ ne 'id' && $_ ne 'name' && &$condition($_)
not / \A _ /msx $_ ne "id" $_ ne "name" &$condition($_)
Truth table40%2/5
not(A) and B and C && D
A= not / \A _ /msxB= $_ ne "id"C= $_ ne "name"D= &$condition($_)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
29 and $_ ne 'id' and $_ ne 'name'
30 5 ▶ and $condition->( $_ ) } @{ $source->attributes || [] };
Condition50%1/2
$source->attributes || []
l!l
50
MC/DC0%0/1
$source->attributes || []
$source->attributes
Truth table50%1/2
A
A= $source->attributes
Aresult
11
00
31 1 };
32  
33 # Private methods
34 16 ▶my $_extn = sub {
Subroutine __ANON__: called CC=4 SCAR=14.9
35 16 ▶ my ($self, $path) = @_; $path //= NUL;
Condition50%1/2
$path //= ""
l!l
160
MC/DC0%0/1
$path //= ""
$path
Truth table50%1/2
A
A= $path
Aresult
11
00
36  
37 16 ▶ my $extn = (split m{ \. }mx, ("${path}" // NUL))[ -1 ];
Condition50%1/2
"$path" // ""
l!l
160
MC/DC0%0/1
"$path" // ""
"$path"
Truth table50%1/2
A
A= "$path"
Aresult
11
00
38  
39 16 ▶ return $extn ? ".${extn}" : $self->extn;
Branch: 1 branch
BranchTrueFalseTotal
$extn ? : 16 0 16
40 1 };
41  
42 10 ▶my $_gettext = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
43 10 ▶ my ($self, $path) = @_; $path or throw Unspecified, [ 'path name' ];
Branch: 1 branch
BranchTrueFalseTotal
$path or &throw(Unspecified(), ["path name"]) 10 0 10
44  
45 10 my $gettext = $self->gettext; my $extn = $self->$_extn( $path );
46  
47 10 $gettext->set_path( $self->language, basename( "${path}", $extn ) );
48  
49 10 return $gettext;
50 1 };
51  
52 4 ▶my $_create_or_update = sub {
Subroutine __ANON__: called CC=9 SCAR=24.0
53 4 my ($self, $path, $result, $updating) = @_;
54  
55 4 ▶ my $source = $result->can( 'result_source' )
Branch: 1 branch
BranchTrueFalseTotal
$result->can('result_source') ? : 4 0 4
56 ? $result->result_source : $result->_resultset->source;
57 4 ▶ my $condition = sub { not $source->language_dependent->{ $_[ 0 ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
58 4 my $updated = $self->storage->create_or_update
59 ( $path, $result, $updating, $condition );
60 3 my $rs = $self->$_gettext( $path )->resultset;
61 3 my $element = $source->name;
62  
63 3 ▶ $condition = sub { $source->language_dependent->{ $_[ 0 ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 3 for my $attr_name ($_get_attributes->( $condition, $source )) {
66 3 ▶ my $msgstr = $result->$attr_name() or next;
Branch: 1 branch
BranchTrueFalseTotal
my $msgstr = $result->$attr_name or next 3 0 3
67 3 my $attrs = { msgctxt => "${element}.${attr_name}",
68 msgid => $result->name,
69 msgstr => [ $msgstr ], };
70  
71 3 $attrs->{name} = $rs->storage->make_key( $attrs ); my $name;
72  
73 3 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 3 ▶ $name = $updating ? $rs->create_or_update( $attrs )
Branch: 1 branch
BranchTrueFalseTotal
$updating ? : 1 2 3
75 : $rs->create( $attrs );
76 }
77 0 ▶ catch { $_->class ne NothingUpdated and throw $_ };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_->class ne NothingUpdated() and &throw($_) 0 0 0
78  
79 3 ▶ $updated ||= $name ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
Condition33%1/3
$updated ||= $name ? 1 : 0
l!l&&r!l&&!r
300
MC/DC0%0/2
$updated ||= $name ? 1 : 0
$updated $name ? 1 : 0
Truth table33%1/3
A ||= B
A= $updatedB= $name ? 1 : 0
ABresult
1X1
011
000
80 }
81  
82 3 ▶ $updating and not $updated and throw NothingUpdated, level => 4;
Branch: 1 branch
BranchTrueFalseTotal
$updating and not $updated and &throw(NothingUpdated(), "level", 4) 0 3 3
Condition66%2/3
$updating and not $updated
!ll&&!rl&&r
210
MC/DC0%0/2
$updating and not $updated
$updating not $updated
Truth table66%2/3
A and not(B)
A= $updatingB= not $updated
ABresult
0X0
100
111
83 3 ▶ $updated and $path->touch;
Branch: 1 branch
BranchTrueFalseTotal
$updated and $path->touch 3 0 3
84 3 return $updated;
85 1 };
86  
87 6 ▶my $_get_key_and_newest = sub {
Subroutine __ANON__: called CC=13 SCAR=26.8
88 6 my ($self, $paths) = @_;
89  
90 6 my $gettext = $self->gettext; my $key; my $newest = 0; my $valid = TRUE;
91  
92 6 for my $path (grep { length } map { "${_}" } @{ $paths }) {
93 6 ▶ $key .= $key ? "~${path}" : $path;
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 0 6 6
94  
95 6 my $mtime = $self->cache->get_mtime( $path );
96  
97 6 ▶ if ($mtime) { $mtime > $newest and $newest = $mtime }
Branch: 2 branches
BranchTrueFalseTotal
if ($mtime) { } 2 4 6
$mtime > $newest and $newest = $mtime 2 0 2
98 4 else { $valid = FALSE }
99  
100 6 my $file = basename( "${path}", $self->$_extn( $path ) );
101 6 my $lang_file = $gettext->object_file( $self->language, $file );
102  
103 6 ▶ if (defined ($mtime = $self->cache->get_mtime( "${lang_file}" ))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($mtime = $self->cache->get_mtime("$lang_file"))) { } 2 4 6
104 2 ▶ if ($mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime) 2 0 2
105 2 ▶ $key .= $key ? "~${lang_file}" : "${lang_file}";
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 2 0 2
106 2 ▶ $mtime > $newest and $newest = $mtime;
Branch: 1 branch
BranchTrueFalseTotal
$mtime > $newest and $newest = $mtime 0 2 2
107 }
108 }
109 else {
110 4 ▶ if ($lang_file->exists and $lang_file->is_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang_file->exists and $lang_file->is_file) { } 4 0 4
Condition33%1/3
$lang_file->exists and $lang_file->is_file
!ll&&!rl&&r
004
MC/DC0%0/2
$lang_file->exists and $lang_file->is_file
$lang_file->exists $lang_file->is_file
Truth table33%1/3
A and B
A= $lang_file->existsB= $lang_file->is_file
ABresult
0X0
100
111
111 4 ▶ $key .= $key ? "~${lang_file}" : "${lang_file}"; $valid = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 4 0 4
112 }
113 0 else { $self->cache->set_mtime( "${lang_file}", 0 ) }
114 }
115 }
116  
117 6 ▶ return ($key, $valid ? $newest : undef);
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 2 4 6
118 1 };
119  
120 4 ▶my $_load_gettext = sub {
Subroutine __ANON__: called CC=6 SCAR=19.2
121 4 my ($self, $data, $path) = @_;
122  
123 4 ▶ my $gettext = $self->$_gettext( $path ); $gettext->path->is_file or return;
Branch: 1 branch
BranchTrueFalseTotal
$gettext->path->is_file or return 4 0 4
124  
125 4 my $gettext_data = $gettext->load->{ $gettext->source_name };
126  
127 4 for my $key (keys %{ $gettext_data }) {
128 3 my ($msgctxt, $msgid) = $gettext->storage->decompose_key( $key );
129 3 my ($element, $attr_name) = split m{ [\.] }msx, $msgctxt, 2;
130  
131 3 ▶ ($element and $attr_name and $msgid) or next;
Branch: 1 branch
BranchTrueFalseTotal
$element and $attr_name and $msgid or next 3 0 3
Condition33%2/6
$element and $attr_name and $msgid
!ll&&!rl&&r
003
$element and $attr_name
!ll&&!rl&&r
003
MC/DC0%0/3
$element and $attr_name and $msgid
$element $attr_name $msgid
Truth table25%1/4
A and B and C
A= $elementB= $attr_nameC= $msgid
ABCresult
0XX0
10X0
1100
1111
132  
133 $data->{ $element }->{ $msgid }->{ $attr_name }
134 3 = $gettext_data->{ $key }->{msgstr}->[ 0 ];
135 }
136  
137 4 return $gettext->path->stat->{mtime};
138 1 };
139  
140 # Public methods
141 2 ▶sub delete {
Subroutine delete: called CC=5 SCAR=16.8
Pod documented
142 2 my ($self, $path, $result) = @_;
143  
144 2 ▶ my $source = $result->can( 'result_source' )
Branch: 1 branch
BranchTrueFalseTotal
$result->can('result_source') ? : 2 0 2
145 ? $result->result_source : $result->_resultset->source;
146 2 ▶ my $condition = sub { $source->language_dependent->{ $_[ 0 ] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
147 2 my $deleted = $self->storage->delete( $path, $result );
148 2 my $rs = $self->$_gettext( $path )->resultset;
149 2 my $element = $source->name;
150  
151 2 for my $attr_name ($_get_attributes->( $condition, $source )) {
152 2 my $attrs = { msgctxt => "${element}.${attr_name}",
153 msgid => $result->name, };
154 2 my $name = $rs->storage->make_key( $attrs );
155  
156 2 $name = $rs->delete( { name => $name, optional => TRUE } );
157 2 ▶ $deleted ||= $name ? TRUE : FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
Condition33%1/3
$deleted ||= $name ? 1 : 0
l!l&&r!l&&!r
200
MC/DC0%0/2
$deleted ||= $name ? 1 : 0
$deleted $name ? 1 : 0
Truth table33%1/3
A ||= B
A= $deletedB= $name ? 1 : 0
ABresult
1X1
011
000
158 }
159  
160 2 return $deleted;
161 }
162  
163 1 ▶sub dump {
Subroutine dump: called CC=7 SCAR=19.5
Pod documented
164 1 my ($self, $path, $data) = @_; $self->validate_params( $path, TRUE );
165  
166 1 my $gettext = $self->$_gettext( $path );
167 1 ▶ my $gettext_data = $gettext->path->exists ? $gettext->load : {};
Branch: 1 branch
BranchTrueFalseTotal
$gettext->path->exists ? : 0 1 1
168  
169 1 for my $source (values %{ $self->schema->source_registrations }) {
170 1 my $element = $source->name; my $element_ref = $data->{ $element };
171  
172 1 for my $msgid (keys %{ $element_ref }) {
173 8 ▶ for my $attr_name (keys %{ $source->language_dependent || {} }) {
Condition50%1/2
$source->language_dependent || {}
l!l
80
MC/DC0%0/1
$source->language_dependent || {}
$source->language_dependent
Truth table50%1/2
A
A= $source->language_dependent
Aresult
11
00
174 8 ▶ my $msgstr = delete $element_ref->{ $msgid }->{ $attr_name }
Branch: 1 branch
BranchTrueFalseTotal
my $msgstr = delete $element_ref->{$msgid}{$attr_name} or next 1 7 8
175 or next;
176 1 my $attrs = { msgctxt => "${element}.${attr_name}",
177 msgid => $msgid,
178 msgstr => [ $msgstr ] };
179 1 my $key = $gettext->storage->make_key( $attrs );
180  
181 1 $gettext_data->{ $gettext->source_name }->{ $key } = $attrs;
182 }
183 }
184 }
185  
186 1 $gettext->dump( { data => $gettext_data } );
187  
188 1 return $self->storage->dump( $path, $data );
189 }
190  
191 3 ▶sub insert {
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
192 3 return $_[ 0 ]->$_create_or_update( $_[ 1 ], $_[ 2 ], FALSE );
193 }
194  
195 6 ▶sub load {
Subroutine load: called CC=7 SCAR=19.9
Pod documented
196 6 ▶ my ($self, @paths) = @_; $paths[ 0 ] or return {};
Branch: 1 branch
BranchTrueFalseTotal
$paths[0] or return {} 6 0 6
197  
198 6 my ($key, $newest) = $self->$_get_key_and_newest( \@paths );
199 6 my ($data, $meta) = $self->cache->get( $key );
200 6 my $cache_mtime = $self->meta_unpack( $meta );
201  
202 6 ▶ not is_stale $data, $cache_mtime, $newest and return $data;
Branch: 1 branch
BranchTrueFalseTotal
&is_stale($data, $cache_mtime, $newest) or return $data 4 2 6
203  
204 4 $data = {}; $newest = 0;
205  
206 4 for my $path (@paths) {
207 4 my ($red, $path_mtime) = $self->read_file( $path, FALSE );
208  
209 4 merge_file_data $data, $red;
210 4 ▶ $path_mtime > $newest and $newest = $path_mtime;
Branch: 1 branch
BranchTrueFalseTotal
$path_mtime > $newest and $newest = $path_mtime 4 0 4
211 4 $path_mtime = $self->$_load_gettext( $data, $path );
212 4 ▶ $path_mtime and $path_mtime > $newest and $newest = $path_mtime;
Branch: 1 branch
BranchTrueFalseTotal
$path_mtime and $path_mtime > $newest and $newest = $path_mtime 0 4 4
Condition33%1/3
$path_mtime and $path_mtime > $newest
!ll&&!rl&&r
040
MC/DC0%0/2
$path_mtime and $path_mtime > $newest
$path_mtime $path_mtime > $newest
Truth table33%1/3
A and B
A= $path_mtimeB= $path_mtime > $newest
ABresult
0X0
100
111
213 }
214  
215 4 $self->cache->set( $key, $data, $self->meta_pack( $newest ) );
216  
217 4 return $data;
218 }
219  
220 5 ▶sub select {
Subroutine select: called CC=2 SCAR=7.0
Pod documented
221 5 my ($self, $path, $element) = @_; $self->validate_params( $path, $element );
222  
223 5 my $data = $self->load( $path );
224  
225 5 ▶ return exists $data->{ $element } ? $data->{ $element } : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$element} ? : 5 0 5
226 }
227  
228 1 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod documented
229 1 return $_[ 0 ]->$_create_or_update( $_[ 1 ], $_[ 2 ], TRUE );
230 }
231  
232 1 1;
233  
234 __END__
« Gettext.pm ↑ Summary Gettext/Storage/PO.pm »