| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$pp eq "" or $s ne ""
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 ""
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | if ( ref $value ) { # set full field | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 3 | splice @$field, 2; | |||||||||||||||||||||||||||
| 55 | 3 | push @$field, @$value; | |||||||||||||||||||||||||||
| 56 | 3 | $add = 0; | |||||||||||||||||||||||||||
| 57 | 3 | ▶ | last unless $self->all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 5 | my $updated; | |||||||||||||||||||||||||||
| 61 | 5 | ▶ | for ( my $i = 2 ; $i < @$field ; $i += 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | if ( $field->[$i] =~ $subfield_regex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 7 | $field->[ $i + 1 ] = $value; | |||||||||||||||||||||||||||
| 64 | 7 | $updated = 1; | |||||||||||||||||||||||||||
| 65 | 7 | ▶ | last unless $self->all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if ($updated) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 76 | 1 | push @{$field}, map { $_ => $value } split //, | |||||||||||||||||||||||||||
| 77 | $path->subfields; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 9 | ▶ | if ($add) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | my $tag = $path->fields; | |||||||||||||||||||||||||||
| 86 | 2 | ▶ | my $occ = $path->occurrences // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$path->occurrences // ""
MC/DC0%0/1
$path->occurrences // ""
$path->occurrences
Truth table50%1/2
A
A= $path->occurrences
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if ( $tag =~ /^[0-9A-Z@]{4}$/ && $occ =~ /^[0-9]*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tag =~ /^[0-9A-Z\@]{4}$/ and $occ =~ /^[0-9]*$/
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]*$/
| |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | unless ( ref $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if ( $path->subfields =~ /^[A-Za-z0-9]+/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||