File Coverage

blib/lib/Catmandu/Fix/marc_cut.pm
Criterion Covered Total %
statement 12 31 38.7
branch n/a
condition n/a
subroutine 4 6 66.6
pod 0 1 0.0
total 16 38 42.1


line stmt bran cond sub pod time code
1             package Catmandu::Fix::marc_cut;
2              
3 1     1   348256 use Catmandu::Sane;
  1         10  
  1         10  
4 1     1   670 use Catmandu::MARC;
  1         4  
  1         47  
5 1     1   10 use Moo;
  1         3  
  1         6  
6 1     1   894 use Catmandu::Fix::Has;
  1         1229  
  1         10  
7              
8             with 'Catmandu::Fix::Base';
9              
10             our $VERSION = '1.13';
11              
12             has marc_path => (fix_arg => 1);
13             has path => (fix_arg => 1);
14             has equals => (fix_opt => 1);
15              
16             sub emit {
17 0     0 0   my ($self,$fixer) = @_;
18 0           my $path = $fixer->split_path($self->path);
19 0           my $key = $path->[-1];
20 0           my $marc_obj = Catmandu::MARC->instance;
21              
22             # Precompile the marc_path to gain some speed
23 0           my $marc_context = $marc_obj->compile_marc_path($self->marc_path, subfield_wildcard => 0);
24 0           my $marc = $fixer->capture($marc_obj);
25 0           my $marc_path = $fixer->capture($marc_context);
26 0           my $equals = $fixer->capture($self->equals);
27              
28 0           my $var = $fixer->var;
29 0           my $result = $fixer->generate_var;
30 0           my $current_value = $fixer->generate_var;
31              
32 0           my $perl = "";
33 0           $perl .= $fixer->emit_declare_vars($current_value, "[]");
34 0           $perl .=<<EOF;
35             if (my ${result} = ${marc}->marc_copy(
36             ${var},
37             ${marc_path},
38             ${equals},1) ) {
39             ${result} = ref(${result}) ? ${result} : [${result}];
40             for ${current_value} (\@{${result}}) {
41             EOF
42              
43             $perl .= $fixer->emit_create_path(
44             $var,
45             $path,
46             sub {
47 0     0     my $var2 = shift;
48 0           "${var2} = ${current_value}"
49             }
50 0           );
51              
52 0           $perl .=<<EOF;
53             }
54             }
55             EOF
56 0           $perl;
57             }
58              
59             1;
60              
61             __END__
62              
63             =head1 NAME
64              
65             Catmandu::Fix::marc_cut - cut marc data in a structured way to a new field
66              
67             =head1 SYNOPSIS
68              
69             # Cut the 001 field out of the MARC record into the fixed001
70             marc_cut(001, fixed001)
71              
72             # Cut all 650 fields out of the MARC record into the subjects array
73             marc_cut(650, subjects)
74              
75             =head1 DESCRIPTION
76              
77             This Fix work like L<Catmandu::Fix::marc_copy> except it will also remove all
78             mathincg fields from the MARC record
79              
80             =head1 METHODS
81              
82             =head2 marc_cut(MARC_PATH, JSON_PATH, [equals: REGEX])
83              
84             Cut this MARC fields referred by a MARC_PATH to a JSON_PATH.
85              
86             When the MARC_PATH points to a MARC tag then only the fields mathching the MARC
87             tag will be copied. When the MATCH_PATH contains indicators or subfields, then
88             only the MARC_FIELDS which contain data in these subfields will be copied. Optional,
89             a C<equals> regular expression can be provided that should match the subfields that
90             need to be copied:
91              
92             # Cut all the 300 fields
93             marc_cut(300,tmp)
94              
95             # Cut all the 300 fields with indicator 1 = 1
96             marc_cut(300[1],tmp)
97              
98             # Cut all the 300 fields which have subfield c
99             marc_cut(300c,tmp)
100              
101             # Cut all the 300 fields which have subfield c equal to 'ABC'
102             marc_cut(300c,tmp,equal:"^ABC")
103              
104             =head1 JSON PATHS
105              
106             Catmandu Fixes can be used to edit the data in the cut fields. To have easy access
107             to the data in the copied fields, these JSON paths can be used (where VAR is the
108             name of field into which you copied the data)
109              
110             VAR.*.tag - The names of all MARC tags
111             VAR.*.ind1 - All first indicators
112             VAR.*.ind2 - All second indicators
113             VAR.*.subfields.*.a - The value of all $a subfields
114             VAR.*.subfields.$first.a - The value of the first $a subfield
115             VAR.*.subfields.$last.a - The value of the last $a subfield
116             VAR.*.content - The value of the first control field
117              
118             VAR.$first.subfields.$first.z - The value of the second $z subfield in the first MARC field
119              
120             These JSON paths can be used like:
121              
122             # Set the first indicator of all 300 fields
123             do marc_each()
124             if marc_has(300)
125             marc_cut(300,tmp)
126              
127             # Set the first indicator to 1
128             set_field(tmp.*.ind1,1)
129              
130             marc_paste(tmp)
131             end
132             end
133              
134             =head1 INLINE
135              
136             This Fix can be used inline in a Perl script:
137              
138             use Catmandu::Fix::marc_copy as => 'marc_cut';
139              
140             my $data = { record => ['650', ' ', 0, 'a', 'Perl'] };
141              
142             $data = marc_cut($data,'650','subject');
143              
144             print $data->{subject}->[0]->{tag} , "\n"; # '650'
145             print $data->{subject}->[0]->{ind1} , "\n"; # ' '
146             print $data->{subject}->[0]->{ind2} , "\n"; # 0
147             print $data->{subject}->[0]->{subfields}->[0]->{a} , "\n"; # 'Perl'
148              
149             =head1 SEE ALSO
150              
151             =over
152              
153             =item * L<Catmandu::Fix::marc_copy>
154              
155             =item * L<Catmandu::Fix::marc_paste>
156              
157             =back
158              
159             =head1 LICENSE AND COPYRIGHT
160              
161             This program is free software; you can redistribute it and/or modify it
162             under the terms of either: the GNU General Public License as published
163             by the Free Software Foundation; or the Artistic License.
164              
165             See http://dev.perl.org/licenses/ for more information.
166              
167             =cut