Codebase list libcatmandu-marc-perl / e8dffb7
add fix marc_sort() Johann Rolschewski 4 years ago
3 changed file(s) with 108 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
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;
12951295 $data;
12961296 }
12971297
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
12981311 1;
12991312
13001313 __END__
13831396
13841397 =item * L<Catmandu::Fix::marc_paste>
13851398
1399 =item * L<Catmandu::Fix::marc_sort>
1400
13861401 =item * L<Catmandu::Fix::Bind::marc_each>
13871402
13881403 =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;