Fix/pica_remove.pm

filter: Statementstmt 100% 43 / 43 Branchbran 90.0% 18 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 95.8% 69 / 72 CC 12 SCAR 19.3
CC
12
Cov
97%
fix
11.0
__ANON__
2.0
BEGIN
1.0
CRAP 6.9
« Fix/pica_add.pm ↑ Summary Fix/Bind/pica_each.pm »
1 package Catmandu::Fix::pica_remove;
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 has path => (
13 fix_arg => 1,
14 5 ▶ coerce => sub { $_[0] ? PICA::Path->new( $_[0] ) : undef },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 4 1 5
15 1 ▶ default => sub { undef }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 );
17  
18 5 ▶sub fix {
Subroutine fix: called CC=11 SCAR=24.0
Pod undocumented
19 5 my ( $self, $data ) = @_;
20 5 ▶ return $data if reftype $data->{record} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($data->{"record"}) ne "ARRAY" 0 5 5
21  
22 # TODO: put this into PICA::Data?
23  
24 5 ▶ my $path = $self->path ? PICA::Path->new( $self->path ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->path ? : 4 1 5
25 5 my $fields = $data->{record};
26  
27 5 ▶ if ( !$path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $path) { } 1 4 5
elsif ($path->subfields) { } 2 2 4
28 1 $fields = [];
29 }
30 elsif ( $path->subfields ) {
31 2 my $subfield_regex = $path->{subfield};
32 2 $fields = [];
33  
34 2 for my $field ( @{ $data->{record} } ) {
35 4 ▶ if ( $path->match_field($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->match_field($field)) { } 2 2 4
36 2 my @result = @$field[ 0 .. 1 ];
37 2 ▶ for ( my $i = 2 ; $i < @$field ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 4 2 6
38 4 ▶ if ( $field->[$i] !~ $subfield_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->[$i] =~ /$subfield_regex/u) 1 3 4
39 1 push @result, $field->[$i], $field->[ $i + 1 ];
40 }
41 }
42 2 ▶ if ( @result > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result > 2) 1 1 2
43 1 ▶ push @result, $field->[-1] if @$field % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$field % 2 0 1 1
44 1 push @$fields, \@result;
45 }
46 }
47 else {
48 2 push @$fields, $field;
49 }
50 }
51 }
52 else {
53 2 $fields = [ grep { !$path->match_field($_) } @$fields ];
54 }
55  
56 5 $data->{record} = $fields;
57  
58 5 return $data;
59 }
60  
61 =head1 NAME
62  
63 Catmandu::Fix::pica_remove - remove PICA (sub)fields
64  
65 =head1 SYNOPSIS
66  
67 # remove all 041A subject fields
68 pica_remove(041A)
69  
70 # remove all $9 subfields from all level 0 fields
71 pica_remove('0...$9')
72
73 # remove all fields, resulting in an empty record
74 pica_remove()
75  
76 =head1 FUNCTIONS
77  
78 =head2 pica_remove([PATH])
79  
80 Delete all (sub)fields from the PICA record, referenced by a L<PICA Path expression|https://format.gbv.de/query/picapath>.
81 Fields are also removed if all subfields have been removed.
82  
83 =head2 SEE ALSO
84  
85 L<PICA::Path>, L<Catmandu::Fix::pica_keep>
86  
87 =cut
88  
89 1 1;
« Fix/pica_add.pm ↑ Summary Fix/Bind/pica_each.pm »