-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package Catmandu::Fix::pica_append; | ||
|
||
use Catmandu::Sane; | ||
|
||
our $VERSION = '1.16'; | ||
|
||
use Moo; | ||
use Catmandu::Fix::Has; | ||
use PICA::Path; | ||
use Scalar::Util 'reftype'; | ||
use PICA::Data 'pica_parser'; | ||
|
||
with 'Catmandu::Fix::Inlineable'; | ||
|
||
has fields => ( | ||
fix_arg => 1, | ||
coerce => sub { | ||
my $record = pica_parser( plain => \$_[0], strict => 1 )->next; | ||
return $record ? $record->{record} : []; | ||
} | ||
); | ||
|
||
sub fix { | ||
my ( $self, $data ) = @_; | ||
|
||
if ( $data->{record} ) { | ||
return $data if reftype( $data->{record} ) ne 'ARRAY'; | ||
} | ||
else { | ||
$data->{record} = []; | ||
} | ||
|
||
push @{ $data->{record} }, @{ $self->fields }; | ||
|
||
return $data; | ||
} | ||
|
||
1; | ||
__END__ | ||
=head1 NAME | ||
Catmandu::Fix::pica_append - parse and append full PICA fields | ||
=head1 SYNOPSIS | ||
pica_append('021A $abook$hto read'); | ||
=head1 DESCRIPTION | ||
=head1 FUNCTIONS | ||
=head2 pica_append(PICA) | ||
Add one or more PICA+ fields given in PICA Plain syntax. | ||
=head1 SEE ALSO | ||
See L<Catmandu::Fix::pica_update> and L<Catmandu::Fix::pica_add> to add | ||
subfield values to existing fields and optionally add non-existing fields. | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use v5.14; | ||
use Test::More; | ||
use Test::Exception; | ||
|
||
use Catmandu::Fix::pica_append as => 'pica_append'; | ||
|
||
my $record = {}; | ||
pica_append($record, '012X $ab$cd'); | ||
is_deeply $record->{record}, [ | ||
['012X', '', 'a', 'b', 'c', 'd'], | ||
], 'append fields'; | ||
|
||
throws_ok { pica_append($record) } qr/Missing/; | ||
throws_ok { pica_append($record, 'xy' ) } qr/invalid/; | ||
|
||
# FIXME: https://github.com/gbv/PICA-Data/issues/136 | ||
#throws_ok { pica_append($record, '021A -$' ) } qr/invalid/; | ||
|
||
done_testing; |