add fix marc_sort()
Johann Rolschewski
4 years ago
0 | package Catmandu::Fix::marc_sort; | |
1 | ||
2 | use Catmandu::Sane; | |
3 | use Moo; | |
4 | use Catmandu::MARC; | |
5 | ||
6 | our $VERSION = '1.251'; | |
7 | ||
8 | sub fix { | |
9 | my ( $self, $data ) = @_; | |
10 | return Catmandu::MARC->instance->marc_sort($data); | |
11 | } | |
12 | ||
13 | =head1 NAME | |
14 | ||
15 | Catmandu::Fix::marc_sort - sort MARC record fields by tag | |
16 | ||
17 | =head1 SYNOPSIS | |
18 | ||
19 | # Sort MARC record fields by tag | |
20 | marc_sort() | |
21 | ||
22 | =head1 DESCRIPTION | |
23 | ||
24 | Sort MARC record fields by tag. | |
25 | ||
26 | =head1 METHODS | |
27 | ||
28 | =head2 marc_sort() | |
29 | ||
30 | If you added new fields to a MARC record with L<Catmandu::Fix::marc_add> or L<Catmandu::Fix::marc_paste>, use I<marc_sort> to sort them by tag. | |
31 | ||
32 | =head1 SEE ALSO | |
33 | ||
34 | L<Catmandu::Fix::marc_add>, | |
35 | L<Catmandu::Fix::marc_copy>, | |
36 | L<Catmandu::Fix::marc_cut>, | |
37 | L<Catmandu::Fix::marc_paste> | |
38 | ||
39 | =cut | |
40 | ||
41 | 1; |
1295 | 1295 | $data; |
1296 | 1296 | } |
1297 | 1297 | |
1298 | sub marc_sort { | |
1299 | my ( $self, $data ) = @_; | |
1300 | if ( defined $data->{record} ) { | |
1301 | my @record | |
1302 | = map { $_->[0] } | |
1303 | sort { $a->[1] <=> $b->[1] } | |
1304 | map { [ $_, $_->[0] =~ m/^[0-9]+$/ ? $_->[0] : '000' ] } | |
1305 | @{ $data->{record} }; | |
1306 | $data->{record} = \@record; | |
1307 | } | |
1308 | return $data; | |
1309 | } | |
1310 | ||
1298 | 1311 | 1; |
1299 | 1312 | |
1300 | 1313 | __END__ |
1383 | 1396 | |
1384 | 1397 | =item * L<Catmandu::Fix::marc_paste> |
1385 | 1398 | |
1399 | =item * L<Catmandu::Fix::marc_sort> | |
1400 | ||
1386 | 1401 | =item * L<Catmandu::Fix::Bind::marc_each> |
1387 | 1402 | |
1388 | 1403 | =item * L<Catmandu::Fix::Condition::marc_match> |
0 | #!perl | |
1 | ||
2 | use strict; | |
3 | use warnings; | |
4 | use Test::More; | |
5 | use Test::Exception; | |
6 | use Catmandu::Importer::MARC; | |
7 | use Catmandu::Fix; | |
8 | use utf8; | |
9 | ||
10 | my $pkg; | |
11 | ||
12 | BEGIN { | |
13 | $pkg = 'Catmandu::Fix::marc_sort'; | |
14 | use_ok $pkg; | |
15 | } | |
16 | ||
17 | require_ok $pkg; | |
18 | ||
19 | my $record = { | |
20 | _id => '000000002', | |
21 | record => [ | |
22 | [ '001', ' ', ' ', '_', '000000002' ], | |
23 | [ 'LDR', ' ', ' ', '_', '00209nam a2200097 i 4500' ], | |
24 | [ '022', ' ', ' ', 'a', '1940-5758' ], | |
25 | [ '245', '1', '0', 'a', 'Catmandu Test' ], | |
26 | [ '650', ' ', '0', 'a', 'RegEx' ], | |
27 | [ '008', ' ', ' ', '_', '050601s1921 xx |||||||||||||| ||dut ' ], | |
28 | [ '650', ' ', '0', 'a', 'Perl' ] | |
29 | ] | |
30 | }; | |
31 | ||
32 | my $record_sorted = { | |
33 | _id => '000000002', | |
34 | record => [ | |
35 | [ 'LDR', ' ', ' ', '_', '00209nam a2200097 i 4500' ], | |
36 | [ '001', ' ', ' ', '_', '000000002' ], | |
37 | [ '008', ' ', ' ', '_', '050601s1921 xx |||||||||||||| ||dut ' ], | |
38 | [ '022', ' ', ' ', 'a', '1940-5758' ], | |
39 | [ '245', '1', '0', 'a', 'Catmandu Test' ], | |
40 | [ '650', ' ', '0', 'a', 'RegEx' ], | |
41 | [ '650', ' ', '0', 'a', 'Perl' ] | |
42 | ] | |
43 | }; | |
44 | ||
45 | my $fixer = Catmandu::Fix->new( fixes => [q|marc_sort()|] ); | |
46 | $record = $fixer->fix($record); | |
47 | ||
48 | is_deeply $record, $record_sorted, 'record sorted'; | |
49 | ||
50 | done_testing; |