| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | # TODO: put this into PICA::Data? | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 5 | ▶ | my $path = $self->path ? PICA::Path->new( $self->path ) : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 5 | my $fields = $data->{record}; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 5 | ▶ | if ( !$path ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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
| |||||||||||||||
| 36 | 2 | my @result = @$field[ 0 .. 1 ]; | |||||||||||||
| 37 | 2 | ▶ | for ( my $i = 2 ; $i < @$field ; $i += 2 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 4 | ▶ | if ( $field->[$i] !~ $subfield_regex ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 1 | push @result, $field->[$i], $field->[ $i + 1 ]; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | 2 | ▶ | if ( @result > 2 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 1 | ▶ | push @result, $field->[-1] if @$field % 2; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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; | |||||||||||||