Fix/pica_update.pm

filter: Statementstmt 100% 69 / 69 Branchbran 83.3% 30 / 36 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 88.6% 117 / 132 CC 23 SCAR 24.0
CC
23
Cov
92%
fix
18.5
pica_parse_subfields
4.0
__ANON__
2.1
CRAP 11.0
↑ Summary Fix/pica_add.pm »
1 package Catmandu::Fix::pica_update;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.18';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PICA::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Catmandu::Fix::Inlineable';
13  
14 has path => (
15 fix_arg => 1,
16 12 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
17 12 ▶ PICA::Path->new( $_[0] =~ /^\$|^\./ ? "....$_[0]" : $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\$|^\./ ? : 0 12 12
18 }
19 1 );
20 1 has value => ( fix_arg => 1 );
21 9 ▶has all => ( fix_opt => 1, default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
22 9 ▶has add => ( fix_opt => 1, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 7 ▶sub pica_parse_subfields {
Subroutine pica_parse_subfields: called CC=4 SCAR=13.9
Pod undocumented
25 7 my ( $pp, $s, @sf ) = ( $_[0], $_[0] );
26 7 ▶ while ( $s =~ s/^\$([A-Za-u0-9])([^\$]+|\$\$)+(.*)/$3/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/^\$([A-Za-u0-9])([^\$]+|\$\$)+(.*)/$3/) 5 7 12
27 5 push @sf, $1, $2;
28 }
29 7 ▶ die "invalid PICA field value: $pp\n" if $pp eq '' or $s ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $pp eq "" or $s ne "" 3 4 7
Condition100%3/3
$pp eq "" or $s ne ""
l!l&&r!l&&!r
124
MC/DC100%2/2
$pp eq "" or $s ne ""
$pp eq "" $s ne ""
Truth table100%3/3
A or B
A= $pp eq ""B= $s ne ""
ABresult
1X1
011
000
30 4 return \@sf;
31 }
32  
33 12 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
34 12 my ($self) = @_;
35  
36 # Update full field, given in PICA Plain syntax
37 $self->{value} = pica_parse_subfields( $self->{value} )
38 12 ▶ unless $self->{path}{subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"path"}{"subfield"} 7 5 12
39 }
40  
41 9 ▶sub fix {
Subroutine fix: called CC=18 SCAR=29.2
Pod undocumented
42 9 my ( $self, $data ) = @_;
43  
44 9 ▶ return $data if reftype( $data->{record} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($data->{"record"}) ne "ARRAY" 0 9 9
45  
46 9 my $path = $self->path;
47 9 my $value = $self->value;
48 9 my $subfield_regex = $path->{subfield};
49  
50 9 my $add = $self->add;
51 9 for my $field ( @{ $data->{record} } ) {
52 12 ▶ if ( $path->match_field($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->match_field($field)) 8 4 12
53 8 ▶ if ( ref $value ) { # set full field
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 3 5 8
54 3 splice @$field, 2;
55 3 push @$field, @$value;
56 3 $add = 0;
57 3 ▶ last unless $self->all;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->all 1 2 3
58 }
59 else {
60 5 my $updated;
61 5 ▶ for ( my $i = 2 ; $i < @$field ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 9 4 13
62 9 ▶ if ( $field->[$i] =~ $subfield_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$subfield_regex/u) 7 2 9
63 7 $field->[ $i + 1 ] = $value;
64 7 $updated = 1;
65 7 ▶ last unless $self->all;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->all 1 6 7
66 }
67 }
68  
69 5 ▶ if ($updated) {
Branch: 2 branches
BranchTrueFalseTotal
if ($updated) { } 4 1 5
elsif ($self->add) { } 1 0 1
70 4 $add = 0; # no need to add full field
71 }
72 elsif ( $self->add ) {
73  
74 # add subfield(s) to current field
75 1 ▶ if ( $path->subfields =~ /^[A-Za-z0-9]+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->subfields =~ /^[A-Za-z0-9]+/) 1 0 1
76 1 push @{$field}, map { $_ => $value } split //,
77 $path->subfields;
78 }
79 }
80 }
81 }
82 }
83  
84 9 ▶ if ($add) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add) 2 7 9
85 2 my $tag = $path->fields;
86 2 ▶ my $occ = $path->occurrences // '';
Condition50%1/2
$path->occurrences // ""
l!l
02
MC/DC0%0/1
$path->occurrences // ""
$path->occurrences
Truth table50%1/2
A
A= $path->occurrences
Aresult
11
00
87  
88 2 ▶ if ( $tag =~ /^[0-9A-Z@]{4}$/ && $occ =~ /^[0-9]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^[0-9A-Z\@]{4}$/ and $occ =~ /^[0-9]*$/) 2 0 2
Condition33%1/3
$tag =~ /^[0-9A-Z\@]{4}$/ and $occ =~ /^[0-9]*$/
!ll&&!rl&&r
002
MC/DC0%0/2
$tag =~ /^[0-9A-Z\@]{4}$/ and $occ =~ /^[0-9]*$/
$tag =~ /^[0-9A-Z\@]{4}$/ $occ =~ /^[0-9]*$/
Truth table33%1/3
A and B
A= $tag =~ /^[0-9A-Z\@]{4}$/B= $occ =~ /^[0-9]*$/
ABresult
0X0
100
111
89 2 ▶ unless ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 1 1 2
90 1 ▶ if ( $path->subfields =~ /^[A-Za-z0-9]+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->subfields =~ /^[A-Za-z0-9]+/) 1 0 1
91 $value =
92 1 [ map { $_ => $value } split //, $path->subfields ];
93 }
94 }
95  
96 2 push @{ $data->{record} }, [ $tag, $occ, @$value ];
97 }
98 }
99  
100 9 return $data;
101 }
102  
103 1 1;
104 __END__
↑ Summary Fix/pica_add.pm »