blib/lib/PICA/Path.pm

filter: Statementstmt 100% 142 / 142 Branchbran 93.6% 88 / 94 Conditioncond 76.4% 26 / 34 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 100% 17 / 17 Podpod 75.0% 9 / 12 totaltotal 91.5% 292 / 319 CC 63 SCAR 22.9
CC
63
Cov
95%
new
16.0
match_record
14.0
match_subfields
13.0
CRAP 9.9
« blib/lib/PICA/Writer/Base.pm ↑ Summary blib/lib/PICA/Schema/Builder.pm »
1 package PICA::Path;
2 9 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 9 our $VERSION = '1.18';
6  
7 9 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 131 ▶ my ($class, $path) = @_;
Subroutine new: called CC=16 SCAR=27.7
Pod documented
14  
15 131 ▶ confess "invalid pica path" if $path !~ /
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m( ([012.][0-9.][0-9.][A-Z\@.]) # tag (\[([0-9.]{2,3})\])? # occurence (\$?([_A-Za-z0-9]+))? # subfields (/(\d+)?(-(\d+)?)?)? # position )ux 9 122 131
16 ([012.][0-9.][0-9.][A-Z@.]) # tag
17 (\[([0-9.]{2,3})\])? # occurence
18 (\$?([_A-Za-z0-9]+))? # subfields
19 (\/(\d+)?(-(\d+)?)?)? # position
20 /x;
21  
22 122 my $field = $1;
23 122 my $occurrence = $3;
24 122 ▶ my $subfield = defined $5 ? "[$5]" : "[_A-Za-z0-9]";
Branch: 1 branch
BranchTrueFalseTotal
defined $5 ? : 60 62 122
25  
26 122 my @position;
27 122 ▶ if (defined $6) { # from, to
Branch: 1 branch
BranchTrueFalseTotal
if (defined $6) 26 96 122
28 26 my ($from, $dash, $to, $length) = ($7, $8, $9, 0);
29  
30 26 ▶ if ($dash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash) 14 12 26
31 14 ▶ confess "invalid pica path" unless defined($from // $to); # /-
Branch: 1 branch
BranchTrueFalseTotal
unless defined($from // $to) 1 13 14
Condition100%3/3
$from // $to
l!l&&r!l&&!r
1211
MC/DC100%2/2
$from // $to
$from $to
Truth table100%3/3
A // B
A= $fromB= $to
ABresult
1X1
011
000
32 }
33  
34 25 ▶ if (defined $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to) { } 7 18 25
35 7 ▶ if (!$from and $dash) { # /-X
Branch: 1 branch
BranchTrueFalseTotal
if (not $from and $dash) 4 3 7
Condition66%2/3
not $from and $dash
!ll&&!rl&&r
304
MC/DC50%1/2
not $from and $dash
not $from $dash
Truth table66%2/3
not(A) and B
A= not $fromB= $dash
ABresult
0X0
100
111
36 4 $from = 0;
37 }
38 7 $length = $to - $from + 1;
39 }
40 else {
41 18 ▶ if ($8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($8) { } 6 12 18
42 6 $length = undef;
43 }
44 else {
45 12 $length = 1;
46 }
47 }
48  
49 25 ▶ if (!defined $length or $length >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $length or $length >= 1) 25 0 25
Condition66%2/3
not defined $length or $length >= 1
l!l&&r!l&&!r
6190
MC/DC0%0/2
not defined $length or $length >= 1
not defined $length $length >= 1
Truth table66%2/3
not(A) or B
A= not defined $lengthB= $length >= 1
ABresult
1X1
011
000
50 25 ▶ unless (!$from and !defined $length) { # /0-
Branch: 1 branch
BranchTrueFalseTotal
if ($from or defined $length) 22 3 25
Condition100%3/3
$from or defined $length
l!l&&r!l&&!r
9133
MC/DC100%2/2
$from or defined $length
$from defined $length
Truth table100%3/3
A or B
A= $fromB= defined $length
ABresult
1X1
011
000
51 22 @position = ($from, $length);
52 }
53 }
54 }
55  
56 121 $field = qr{$field};
57  
58 121 ▶ if (defined $occurrence) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $occurrence) 4 117 121
59 4 $occurrence = qr{$occurrence};
60 }
61  
62 121 $subfield = qr{$subfield};
63  
64 121 bless [$field, $occurrence, $subfield, @position], $class;
65 }
66  
67 sub match_record {
68 54 ▶ my ($self, $record, %args) = @_;
Subroutine match_record: called CC=14 SCAR=26.4
Pod documented
69  
70 # my $subfield_regex = $self->[2];
71  
72 54 my %default_args = (
73 force_array => 0,
74 join => '',
75 nested_arrays => 0,
76 pluck => 0,
77 split => 0,
78 value => undef,
79 );
80 54 %args = (%default_args, %args);
81 54 ▶ if ($args{nested_arrays}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"nested_arrays"}) 11 43 54
82 11 $args{split} = 1;
83 }
84  
85 # check if path exists
86 54 ▶ if ($args{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"value"}) 3 51 54
87 3 my $value = $self->record_subfields($record);
88 3 ▶ return $value ? $args{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 1 3
89 }
90  
91 # gather values from matched subfields
92 51 my @matches;
93 51 for my $field (@{$self->record_fields($record)}) {
94 122 ▶ next unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 122 122
95 my @matched_subfields
96 = $args{pluck}
97 122 ▶ ? $self->match_subfields($field, pluck => 1)
Branch: 1 branch
BranchTrueFalseTotal
$args{'pluck'} ? : 4 118 122
98 : $self->match_subfields($field);
99 122 ▶ next unless defined $matched_subfields[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matched_subfields[0] 18 104 122
100 104 ▶ if ($args{split}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'split'}) { } 65 39 104
101 65 ▶ if ($args{nested_arrays}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'nested_arrays'}) { } 33 32 65
102 33 push @matches, \@matched_subfields;
103 }
104 else {
105 32 push @matches, @matched_subfields;
106 }
107 }
108 else {
109 39 push @matches, join($args{join}, @matched_subfields);
110 }
111  
112 }
113 51 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 49 2 51
114  
115 # return matched fields as array reference
116 49 ▶ if ($args{split}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'split'}) { } 26 23 49
117 26 ▶ return $args{force_array} ? [\@matches] : \@matches;
Branch: 1 branch
BranchTrueFalseTotal
$args{'force_array'} ? : 8 18 26
118 }
119  
120 # ... or string
121 else {
122 return $args{force_array}
123 ? \@matches
124 23 ▶ : join($args{join}, @matches);
Branch: 1 branch
BranchTrueFalseTotal
$args{'force_array'} ? : 5 18 23
125 }
126 }
127 2 return;
128  
129 }
130  
131 sub match_field {
132 789 ▶ my ($self, $field) = @_;
Subroutine match_field: called CC=5 SCAR=16.1
Pod documented
133  
134 789 ▶ if ($field->[0] =~ $self->[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[0] =~ /$self->[0]/u and !$self->[1] || defined $field->[1] && $field->[1] =~ /$self->[1]/u) 155 634 789
Condition88%8/9
$field->[0] =~ /$self->[0]/u and !$self->[1] || defined $field->[1] && $field->[1] =~ /$self->[1]/u
!ll&&!rl&&r
6339858
not $self->[1] or defined $field->[1] and $field->[1] =~ /$self->[1]/u
l!l&&r!l&&!r
15231
defined $field->[1] and $field->[1] =~ /$self->[1]/u
!ll&&!rl&&r
013
MC/DC50%2/4
$field->[0] =~ /$self->[0]/u and !$self->[1] || defined $field->[1] && $field->[1] =~ /$self->[1]/u
$field->[0] =~ /$self->[0]/u not $self->[1] defined $field->[1] $field->[1] =~ /$self->[1]/u
Truth table60%3/5
A and not(B) or C and D
A= $field->[0] =~ /$self->[0]/uB= not $self->[1]C= defined $field->[1]D= $field->[1] =~ /$self->[1]/u
ABCDresult
0XXX0
100X0
10100
11XX1
10111
135 && (!$self->[1] || (defined $field->[1] && $field->[1] =~ $self->[1]))
136 )
137 {
138 155 return $field;
139 }
140  
141 634 return;
142 }
143  
144 sub match_subfields {
145 917 ▶ my ($self, $field, %args) = @_;
Subroutine match_subfields: called CC=13 SCAR=25.7
Pod documented
146  
147 917 my $subfield_regex = $self->[2];
148 917 my $from = $self->[3];
149 917 my $length = $self->[4];
150  
151 917 my @values;
152  
153 917 ▶ if ($args{pluck}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'pluck'}) { } 4 913 917
154  
155 # Treat the subfields as a hash index
156 4 my $subfield_href = {};
157 4 ▶ for (my $i = 2; $i < @{$field}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 8 4 12
158 8 push @{$subfield_href->{$field->[$i]}}, $field->[$i + 1];
159 }
160  
161 4 my $subfields = $self->[2];
162 4 $subfields =~ s{.*\[(.+)\].*}{\1}g;
163 4 for my $subfield (split('', $subfields)) {
164 8 ▶ my $value = $subfield_href->{$subfield} // [undef];
Condition50%1/2
$subfield_href->{$subfield} // [undef]
l!l
80
MC/DC0%0/1
$subfield_href->{$subfield} // [undef]
$subfield_href->{$subfield}
Truth table50%1/2
A
A= $subfield_href->{$subfield}
Aresult
11
00
165 8 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) { } 2 6 8
166 push @values, map {
167 2 ▶ $length
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 2 0 2
168 ? substr($_, $from, $length)
169 : substr($_, $from)
170 2 } @{$value};
171 }
172 else {
173 6 push @values, @{$value};
174 }
175  
176 }
177 }
178 else {
179 913 ▶ for (my $i = 2; $i < @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 1161 913 2074
180 1161 ▶ if ($field->[$i] =~ $subfield_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$subfield_regex/u) 958 203 1161
181 958 my $value = $field->[$i + 1];
182 958 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 9 949 958
183 9 ▶ $value
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 7 2 9
184 = $length
185 ? substr($value, $from, $length)
186 : substr($value, $from);
187 9 ▶ next if '' eq ($value // '');
Branch: 1 branch
BranchTrueFalseTotal
if "" eq ($value // "") 1 8 9
Condition50%1/2
$value // ""
l!l
90
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
188 }
189 957 push @values, $value;
190 }
191 }
192 }
193  
194 917 return @values;
195 }
196  
197 sub record_fields {
198 51 ▶ my ($self, $record) = @_;
Subroutine record_fields: called CC=2 SCAR=7.0
Pod undocumented
199  
200 51 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 51 0 51
201 51 return [grep {$self->match_field($_)} @$record];
202 }
203  
204 sub record_subfields {
205 12 ▶ my ($self, $record) = @_;
Subroutine record_subfields: called CC=3 SCAR=11.0
Pod undocumented
206  
207 12 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 12 0 12
208  
209 12 my @values;
210  
211 12 foreach my $field (grep {$self->match_field($_)} @$record) {
212 16 push @values, $self->match_subfields($field);
213 }
214  
215 12 return @values;
216 }
217  
218 sub unescape {
219 33 ▶ my $exp = shift;
Subroutine unescape: called CC=2 SCAR=6.9
Pod undocumented
220 33 ▶ $exp =~ s/^\(\?[^:]*:(.*)\)$/$1/ if defined $exp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exp 24 9 33
221 33 return $exp;
222 }
223  
224 sub stringify {
225 9 ▶ my ($self, $short) = @_;
Subroutine stringify: called CC=6 SCAR=17.9
Pod documented
226  
227 9 my $str = $self->fields;
228  
229 9 my $occurrence = $self->occurrences;
230 9 ▶ $str .= "[$occurrence]" if defined $occurrence;
Branch: 1 branch
BranchTrueFalseTotal
if defined $occurrence 1 8 9
231  
232 9 my $subfields = $self->subfields;
233 9 ▶ if (defined $subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfields) 4 5 9
234 4 ▶ unless ($short and $subfields !~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($short and not $subfields =~ /^\$/u) 3 1 4
Condition66%2/3
$short and not $subfields =~ /^\$/u
!ll&&!rl&&r
301
MC/DC50%1/2
$short and not $subfields =~ /^\$/u
$short not $subfields =~ /^\$/u
Truth table66%2/3
A and not(B)
A= $shortB= not $subfields =~ /^\$/u
ABresult
0X0
100
111
235 3 $str .= '$';
236 }
237 4 $str .= $subfields;
238 }
239  
240 9 my $pos = $self->positions;
241 9 ▶ $str .= "/$pos" if defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 4 5 9
242  
243 9 $str;
244 }
245  
246 sub fields {
247 11 ▶ return unescape($_[0]->[0]);
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
248 }
249  
250 sub occurrences {
251 11 ▶ return unescape($_[0]->[1]);
Subroutine occurrences: called CC=1 SCAR=0.0
Pod documented
252 }
253  
254 sub subfields {
255 11 ▶ my $subfields = unescape($_[0]->[2]);
Subroutine subfields: called CC=3 SCAR=11.0
Pod documented
256 11 ▶ if (defined $subfields and $subfields ne '[_A-Za-z0-9]') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfields and $subfields ne "[_A-Za-z0-9]") 5 6 11
Condition66%2/3
defined $subfields and $subfields ne "[_A-Za-z0-9]"
!ll&&!rl&&r
065
MC/DC50%1/2
defined $subfields and $subfields ne "[_A-Za-z0-9]"
defined $subfields $subfields ne "[_A-Za-z0-9]"
Truth table66%2/3
A and B
A= defined $subfieldsB= $subfields ne "[_A-Za-z0-9]"
ABresult
0X0
100
111
257 5 $subfields =~ s/\[|\]//g;
258 5 return $subfields;
259 }
260 6 return;
261 }
262  
263 sub positions {
264 11 ▶ my ($self) = @_;
Subroutine positions: called CC=8 SCAR=20.8
Pod documented
265  
266 11 my ($from, $length, $pos) = ($self->[3], $self->[4]);
267 11 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 5 6 11
268 5 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 4 1 5
269 4 $pos = $from;
270 }
271 5 ▶ if (!defined $length) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $length) { } 1 4 5
elsif ($length > 1) { } 2 2 4
elsif ($length == 1 and not $from) { } 1 1 2
272 1 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 1 0 1
273 1 $pos = "$from-";
274 }
275 }
276 elsif ($length > 1) {
277 2 $pos .= '-' . ($from + $length - 1);
278 }
279 ▶ elsif ($length == 1 && !$from) {
Condition66%2/3
$length == 1 and not $from
!ll&&!rl&&r
011
MC/DC50%1/2
$length == 1 and not $from
$length == 1 not $from
Truth table66%2/3
A and not(B)
A= $length == 1B= not $from
ABresult
0X0
100
111
280 1 $pos = 0;
281 }
282 }
283  
284 11 return $pos;
285 }
286  
287 9 1;
288 __END__
« blib/lib/PICA/Writer/Base.pm ↑ Summary blib/lib/PICA/Schema/Builder.pm »