Fix/marc_map.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 2 / 2 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 98.3% 61 / 62 CC 6 SCAR 13.2
CC
6
Cov
100%
emit
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.7
« Validator/MARC.pm ↑ Summary Exporter/MARC.pm »
1 package Catmandu::Fix::marc_map;
2  
3 15 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Catmandu::MARC;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 with 'Catmandu::Fix::Base';
9  
10 15 our $VERSION = '1.33';
11  
12 15 has marc_path => (fix_arg => 1);
13 15 has path => (fix_arg => 1);
14 15 has split => (fix_opt => 1);
15 15 has join => (fix_opt => 1);
16 15 has value => (fix_opt => 1);
17 15 has pluck => (fix_opt => 1);
18 15 has nested_arrays => (fix_opt => 1);
19  
20 107 ▶sub emit {
Subroutine emit: called CC=6 SCAR=17.9
Pod undocumented
21 107 my ($self,$fixer) = @_;
22 107 my $path = $fixer->split_path($self->path);
23 107 my $key = $path->[-1];
24 107 my $marc_obj = Catmandu::MARC->instance;
25  
26 # Precompile the marc_path to gain some speed
27 107 my $marc_context = $marc_obj->compile_marc_path($self->marc_path,subfield_wildcard => 1);
28 107 my $marc = $fixer->capture($marc_obj);
29 107 my $marc_path = $fixer->capture($marc_context);
30 107 ▶ my $marc_opt = $fixer->capture({
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^(\$.*|[0-9]+)$/ ? : 31 76 107
Condition100%8/8
$self->join // ""
l!l
4103
$self->split // 0
l!l
2780
$self->pluck // 0
l!l
5102
$self->nested_arrays // 0
l!l
1691
MC/DC100%1/1
$self->join // ""
$self->join
MC/DC100%1/1
$self->split // 0
$self->split
MC/DC100%1/1
$self->pluck // 0
$self->pluck
MC/DC100%1/1
$self->nested_arrays // 0
$self->nested_arrays
Truth table100%2/2
A
A= $self->join
Aresult
11
00
Truth table100%2/2
A
A= $self->split
Aresult
11
00
Truth table100%2/2
A
A= $self->pluck
Aresult
11
00
Truth table100%2/2
A
A= $self->nested_arrays
Aresult
11
00
31 '-join' => $self->join // '' ,
32 '-split' => $self->split // 0 ,
33 '-pluck' => $self->pluck // 0 ,
34 '-nested_arrays' => $self->nested_arrays // 0 ,
35 '-value' => $self->value ,
36 '-force_array' => ($key =~ /^(\$.*|[0-9]+)$/) ? 1 : 0
37 });
38  
39 107 my $var = $fixer->var;
40 107 my $result = $fixer->generate_var;
41 107 my $current_value = $fixer->generate_var;
42  
43 107 my $perl = "";
44 107 $perl .= $fixer->emit_declare_vars($current_value, "[]");
45 107 $perl .=<<EOF;
46 if (defined(my ${result} = ${marc}->marc_map(
47 ${var},
48 ${marc_path},
49 ${marc_opt})) ) {
50 ${result} = ref(${result}) ? ${result} : [${result}];
51 for ${current_value} (\@{${result}}) {
52 EOF
53  
54 $perl .= $fixer->emit_create_path(
55 $var,
56 $path,
57 107 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 107 my $var2 = shift;
59 107 "${var2} = ${current_value}"
60 }
61 107 );
62  
63 107 $perl .=<<EOF;
64 }
65 }
66 EOF
67 107 $perl;
68 }
69  
70 15 1;
71  
72 =head1 NAME
73  
74 Catmandu::Fix::marc_map - copy marc values of one field to a new field
75  
76 =head1 SYNOPSIS
77  
78 # Append all 245 subfields to my.title field the values are joined into one string
79 marc_map('245','my.title')
80  
81 # Append al 245 subfields to the my.title keeping all subfields as an array
82 marc_map('245','my.title', split:1)
83  
84 # Copy the 245-$a$b$c subfields into the my.title hash in the order provided in the record
85 marc_map('245abc','my.title')
86  
87 # Copy the 245-$c$b$a subfields into the my.title hash in the order c,b,a
88 marc_map('245cba','my.title', pluck:1)
89  
90 # Add the 100 subfields into the my.authors array
91 marc_map('100','my.authors.$append')
92  
93 # Add the 710 subfields into the my.authors array
94 marc_map('710','my.authors.$append')
95  
96 # Add the 600-$x subfields into the my.subjects array while packing each into a genre.text hash
97 marc_map('600x','my.subjects.$append.genre.text')
98  
99 # Copy the 008 characters 35-37 into the my.language hash
100 marc_map('008/35-37','my.language')
101  
102 # Copy all the 600 fields into a my.stringy hash joining them by '; '
103 marc_map('600','my.stringy', join:'; ')
104  
105 # When 024 field exists create the my.has024 hash with value 'found'
106 marc_map('024','my.has024', value:found)
107  
108 # When 260c field exists create the my.has260c hash with value 'found'
109 marc_map('260c','my.has260c', value:found)
110  
111 # Copy all 100 subfields except the digits to the 'author' field
112 marc_map('100^0-9','author')
113  
114 # Map all the 500 - 599 fields to my.notes
115 marc_map('5..','my.motes')
116  
117 # Map the 100-a field where indicator-1 is 3
118 marc_map('100[3]a','name.family')
119  
120 # Map the 245-a field where indicator-2 is 0
121 marc_map('245[,0]a','title')
122  
123 # Map the 245-a field where indicator-1 is 1 and indicator-2 is 0
124 marc_map('245[1,0]a','title')
125  
126 =head1 DESCRIPTION
127  
128 Copy data from a MARC field to JSON path.
129  
130 This module implements a small subset of the L<MARCspec|http://marcspec.github.io/MARCspec/>
131 specification to map MARC fields. For a more extensive MARC path implementation
132 please take a look at Casten Klee's MARCSpec module: L<Catmandu::Fix::marc_spec>
133  
134 =head1 METHODS
135  
136 =head2 marc_map(MARC_PATH, JSON_PATH, OPT:VAL, OPT2:VAL,...)
137  
138 Copy the value(s) of the data found at a MARC_PATH to a JSON_PATH.
139  
140 The MARC_PATH can point to a MARC field. For instance:
141  
142 marc_map('245',title)
143 marc_map('020',isbn)
144  
145 The MARC_PATH can point to one or more MARC subfields. For instamce:
146  
147 marc_map('245a',title)
148 marc_map('245ac',title)
149  
150 You can also use dollar signs to indicate subfields
151  
152 marc_map('245$a$c',title)
153  
154 Wildcards are allowed in the field names:
155  
156 # Map all the 200-fields to a title
157 marc_map('2..'',title)
158  
159 To filter out specific fields indicators can be used:
160  
161 # Only map the MARC fields with indicator-1 is '1' to title
162 marc_map('245[1,]',title)
163  
164 Also a substring of a field value can be mapped:
165  
166 # Map 008 position 35 to 37 to the language field
167 marc_map('008/35-37',language)
168  
169 By default all matched fields in a MARC_PATH will be joined into one string.
170 This behavior can be changed using one more more options (see below).
171  
172 Visit our Wiki L<https://github.com/LibreCat/Catmandu-MARC/wiki/Mapping-rules>
173 for a complete overview of all allowed mappings.
174  
175 =head1 OPTIONS
176  
177 =head2 split: 0|1
178  
179 When split is set to 1 then all mapped values will be joined into an array
180 instead of a string.
181  
182 # The subject field will contain an array of strings (one string
183 # for each 500 field found)
184 marc_map('500',subject, split: 1)
185  
186 # The subject field will contain a string
187 marc_map('500', subject)
188  
189 =head2 join: Str
190  
191 By default all the values are joined into a string without a field separator.
192 Use the join function to set the separator.
193  
194 # All subfields of the 245 field will be separated with a space " "
195 marc_map('245',title, join: " ")
196  
197 =head2 pluck: 0|1
198  
199 Be default, all subfields are added to the mapping in the order they are found
200 in the record. Using the pluck option, one can select the required order of
201 subfields to map.
202  
203 # First write the subfield-c to the title, then the subfield_a
204 marc_map('245ca',title, pluck:1)
205  
206 =head2 value: Str
207  
208 Don't write the value of the MARC (sub)field to the JSON_PATH but the specified
209 string value.
210  
211 # has_024_a will contain the value 'Y' if the MARC field 024 subfield-a
212 # exists
213 marc_map('024a',has_024_a,value:Y)
214  
215 =head2 nested_arrays: 0|1
216  
217 When the split option is specified the output of the mapping will always be an
218 array of strings (one string for each subfield found). Using the nested_array
219 option the output will be an array of array of strings (one array item for
220 each matched field, one array of strings for each matched subfield).
221  
222 =head1 INLINE
223  
224 This Fix can be used inline in a Perl script:
225  
226 use Catmandu::Fix::marc_map as => 'marc_map';
227  
228 my $data = { record => [...] };
229  
230 $data = marc_map($data,'245a','title');
231  
232 print $data->{title} , "\n";
233  
234 =head1 SEE ALSO
235  
236 L<Catmandu::Fix>
237 L<Catmandu::Fix::marc_spec>
238  
239 =cut
« Validator/MARC.pm ↑ Summary Exporter/MARC.pm »