Record/MiJ.pm

filter: Statementstmt 100% 86 / 86 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 100% 125 / 125 CC 10 SCAR 6.2
CC
10
Cov
100%
to_mij_structure
3.0
new_field_from_mij_structure
3.0
new_datafield_from_mij_structure
3.0
CRAP 1.9
« File/MiJ.pm ↑ Summary
1 package MARC::Record::MiJ;
2 1 ▶use ex::monkeypatched;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 MARC::Record::MiJ - Convert MARC::Record to/from marc-in-json structure
11  
12 =head1 VERSION
13  
14 Version 0.04
15  
16 =cut
17  
18 1 our $VERSION = '0.04';
19  
20 =head1 SYNOPSIS
21  
22 use MARC::Record;
23 use MARC::Record::MIJ
24  
25 my $str = get_marc_in_json_from_somewhere;
26  
27 # The most common use will be to use methods monkeypatched into MARC::Record
28 my $r = MARC::Record->new_from_mij($str);
29 my $json = $r->to_mij;
30  
31 # You can also work with the underlying hash/array structure if you're dealing with
32 # json serialization/deserialization on your own
33  
34 my $mij_structure = $r->to_mij_structure;
35 my $r = MARC::Record->new_from_mij_structure($mij_structure);
36  
37 # You can also call things on MARC::Record::MiJ
38  
39 my $r = MARC::Record::MiJ->new($str);
40 my $json = MARC::Record::MiJ->to_mij($r);
41 my $mij_structure = MARC::Record::MiJ->to_mij_structure($r);
42 my $r = MARC::Record::MiJ->new_from_mij_structure($mij_structure);
43  
44 =head1 DESCRIPTION
45  
46 Reads and writes MARC-in-JSON structures and strings as supported by pymarc/ruby-marc/marc4j and
47 described at http://dilettantes.code4lib.org/blog/2010/09/a-proposal-to-serialize-marc-in-json/
48  
49 Don't confuse with another (incompatible) JSON encoding in the module C<MARC::File::JSON>, which
50 to the best of my knowledge isn't supported by other readers/writers.
51  
52 For reading, you probably don't need to use this directly; take a look at C<MARC::File::MiJ> for reading in
53 newline-delimited marc-in-json files by itself or in conjunction with C<MARC::Batch>.
54  
55 The MARC::Record distribution doesn't so much do do writing out files. You can do something like this:
56  
57 # convert file from marc binary to marc-in-json
58 use MARC::Batch;
59 use MARC::Record::MiJ;
60 my $batch = MARC::Batch->new('USMARC', 'file.mrc');
61 open(my $jsonfile, '>', 'file.ndj' );
62 while (my $r = $batch->next) {
63 print $jsonfile MARC::Record::MiJ->to_mij($r), "\n";
64 }
65 close $jsonfile;
66  
67 ...to produce newline-delimited marc-in-json from a binary file.
68  
69  
70 =head1 SUBROUTINES/METHODS
71  
72 =head2 json
73  
74 Get a json object to work with (memoized). We want to control it so we make sure
75 it's not doing anything pretty (like, say, putting newlines in, which woudl make it
76 harder to produce newline-delimited json file).
77  
78 =cut
79  
80 1 my $json;
81 23 ▶sub json {
Subroutine json: called CC=2 SCAR=6.9
Pod documented
82 23 ▶ return $json if ($json);
Branch: 1 branch
BranchTrueFalseTotal
if $json 22 1 23
83 1 my $self = shift;
84 1 $json = JSON->new->utf8;
85 1 $json->pretty(0);
86 1 return $json;
87 }
88  
89  
90 =head2 new($str)
91  
92 Take a JSON string and turn it into a MARC::Record object
93  
94 =cut
95  
96 11 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
97 11 my $self = shift;
98 11 my $str = shift;
99 11 return $self->new_from_mij_structure($self->json->decode($str));
100 }
101  
102 =head2 to_mij($r)
103  
104 Take a record; return a JSON string
105  
106 =cut
107  
108 10 ▶sub to_mij {
Subroutine to_mij: called CC=1 SCAR=0.0
Pod documented
109 10 my $self = shift;
110 10 my $r = shift;
111 10 return $self->json->encode($self->to_mij_structure($r));
112 }
113  
114  
115  
116 =head2 MARC::Record::JSON->to_mij_structure($r)
117  
118 Turn a record into a marc-in-json compatible hash; return the hash pointer
119  
120 =cut
121  
122 14 ▶sub to_mij_structure {
Subroutine to_mij_structure: called CC=3 SCAR=11.0
Pod documented
123 14 my $class = shift;
124 14 my $r = shift;
125
126 14 my $h = {};
127 14 my @fields;
128 14 $h->{leader} = $r->leader;
129
130 14 foreach my $f ($r->fields) {
131 410 ▶ if ($f->is_control_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_control_field) { } 65 345 410
132 65 push @fields, controlfield_to_mij_structure($f);
133 } else {
134 345 push @fields, valuefield_to_mij_structure($f);
135 }
136 }
137 14 $h->{fields} = \@fields;
138 14 return $h;
139 }
140  
141  
142 =head2 controlfield_to_mij_structure($field)
143  
144 Turn a MARC::Record controlfield into an appropriate hash
145  
146 =cut
147  
148 65 ▶sub controlfield_to_mij_structure {
Subroutine controlfield_to_mij_structure: called CC=1 SCAR=0.0
Pod documented
149 65 my $cf = shift;
150 65 return {$cf->tag => $cf->data };
151 }
152  
153 =head2 valuefield_to_mij_structure($field)
154  
155 Turn a MARC::Record valuefield into an appropriate hash
156  
157  
158 =cut
159  
160 345 ▶sub valuefield_to_mij_structure {
Subroutine valuefield_to_mij_structure: called CC=2 SCAR=6.9
Pod documented
161 345 my $vf = shift;
162 345 my @subfields;
163 345 my $h = {ind1=>$vf->indicator(1), ind2=>$vf->indicator(2)};
164 345 foreach my $sf ($vf->subfields) {
165 638 push @subfields, subfield_to_mij_structure($sf);
166 }
167 345 $h->{subfields} = \@subfields;
168 345 return {$vf->tag => $h};
169
170 }
171  
172 =head2 subfield_to_mij_structure($sf)
173  
174 Turn a MARC::Record subfield pair (arrayref duple of code/value) into an appropriate hash
175  
176  
177 =cut
178  
179 638 ▶sub subfield_to_mij_structure {
Subroutine subfield_to_mij_structure: called CC=1 SCAR=0.0
Pod documented
180 638 my $sf = shift;
181 638 return {$sf->[0]=> $sf->[1]};
182 }
183  
184  
185 =head2 my $r = MARC::Record::JSON->new_from_mij_structure($mij_structure)
186  
187 Given a marc-in-json structure, return a MARC::Record object
188  
189 =cut
190  
191 14 ▶sub new_from_mij_structure {
Subroutine new_from_mij_structure: called CC=2 SCAR=6.9
Pod documented
192 14 my $self = shift;
193 14 my $h = shift;
194
195 14 my $r = new MARC::Record;
196
197 14 $r->leader($h->{leader});
198
199 14 my @fields;
200 14 foreach my $f (@{$h->{fields}}) {
201 427 push @fields, new_field_from_mij_structure($f);
202 }
203 14 $r->append_fields(@fields);
204 14 return $r;
205 }
206  
207 =head2 new_field_from_mij_structure($f)
208  
209 Given a field structure, create an appropriate (control or variable) field
210  
211 =cut
212  
213 427 ▶sub new_field_from_mij_structure {
Subroutine new_field_from_mij_structure: called CC=3 SCAR=11.0
Pod documented
214 427 my $mijf = shift;
215 427 my ($tag, $h);
216  
217  
218 427 ▶ while (my ($k, $v) = each(%$mijf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$mijf) 427 427 854
219 427 $tag = $k;
220 427 $h = $v;
221 }
222
223 427 ▶ if (ref($h)) { # if it's a hashref
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h) { } 361 66 427
224 361 return new_datafield_from_mij_structure($tag, $h);
225 } else { # create and return a control field
226 66 return MARC::Field->new($tag, $h);
227 }
228  
229 }
230  
231 =head2 new_datafield_from_mij_structure
232  
233 Support for new_field_from_mij_structure; do the more complex work
234 of creating a datafield
235  
236 =cut
237  
238 361 ▶sub new_datafield_from_mij_structure {
Subroutine new_datafield_from_mij_structure: called CC=3 SCAR=11.0
Pod documented
239 361 my ($tag, $h) = @_;
240 361 my @subfields;
241 361 foreach my $sf (@{$h->{subfields}}) {
242 677 ▶ while (my ($code, $data) = each %$sf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sf) 677 677 1354
243 677 push @subfields, $code, $data
244 }
245 }
246 361 return MARC::Field->new($tag, $h->{ind1}, $h->{ind2}, @subfields);
247  
248 }
249  
250 =head1 Monkeypatching MARC::Record
251  
252 Add C<new_from_mij_structure($mij_structure) and C<to_mij_structure()> to MARC::Record
253  
254 my $r = MARC::Record->new_from_mij_structure($mij_structure);
255 $mij_structure = $r->to_mij_structure;
256  
257 =cut
258  
259 ex::monkeypatched->inject('MARC::Record' =>
260 2 ▶ new_from_mij_structure => sub { my $class = shift; my $mij = shift; return
Subroutine new_from_mij_structure: called CC=1 SCAR=0.0
261 2 MARC::Record::MiJ->new_from_mij_structure($mij)},
262 2 ▶ to_mij_structure => sub { my $self = shift; return MARC::Record::MiJ->to_mij_structure($self) },
Subroutine to_mij_structure: called CC=1 SCAR=0.0
263 1 ▶ new_from_mij => sub { my $class = shift; my $mij = shift; return MARC::Record::MiJ->new($mij) },
Subroutine new_from_mij: called CC=1 SCAR=0.0
264 1 ▶ to_mij => sub { my $self = shift; return MARC::Record::MiJ->to_mij($self) }
Subroutine __ANON__: called CC=1 SCAR=0.0
265
266 1 );
267  
268  
269  
270 =head1 AUTHOR
271  
272 Bill Dueber, C<< <dueberb at umich.edu> >>
273  
274 =head1 BUGS
275  
276 Please report any bugs or feature requests to C<bug-MARC-File-MiJ at rt.cpan.org>, or through
277 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MARC-File-MiJ>. I will be notified, and then you'll
278 automatically be notified of progress on your bug as I make changes.
279  
280  
281  
282  
283 =head1 SUPPORT
284  
285 You can find documentation for this module with the perldoc command.
286  
287 perldoc MARC::Record::MiJ
288  
289  
290 You can also look for information at:
291  
292 =over 4
293  
294 =item * RT: CPAN's request tracker (report bugs here)
295  
296 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MARC-File-MiJ>
297  
298 =item * AnnoCPAN: Annotated CPAN documentation
299  
300 L<http://annocpan.org/dist/MARC-File-MiJ>
301  
302 =item * CPAN Ratings
303  
304 L<http://cpanratings.perl.org/d/MARC-File-MiJ>
305  
306 =item * Search CPAN
307  
308 L<http://search.cpan.org/dist/MARC-File-MiJ/>
309  
310 =back
311  
312  
313 =head1 ACKNOWLEDGEMENTS
314  
315  
316 =head1 LICENSE AND COPYRIGHT
317  
318 Copyright 2013 Bill Dueber.
319  
320 This software is free software and may be distributed under the same
321 terms as Perl itself.
322  
323  
324 =cut
325  
326 1 1; # End of MARC::Record::MiJ
« File/MiJ.pm ↑ Summary