MARC.pm

filter: Statementstmt 100% 66 / 66 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 99.0% 100 / 101 CC 12 SCAR 12.2
CC
12
Cov
99%
init
7.0
filter
4.0
new
3.0
CRAP 3.4
« MARC/Field.pm ↑ Summary MARC/Subfield.pm »
1 package Template::Plugin::MARC;
2  
3 1 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Template::Plugin::MARC - Template::Toolkit plugin to make MARC friendly
10  
11 =head1 SYNOPSIS
12  
13 [% USE record = MARC(mymarc) %] <!-- translate MARC::Record to T::T hash -->
14 <h1>[% record.f245.sa %]</h1> <!-- subfield 245$a -->
15 [% record.f245.all %] <!-- all subfields concatenated together -->
16 [% FOREACH link IN record.f856s %] <!-- process each 856 field -->
17 <a href="whatever/[% link.su %]">[% link.sy %]</a> <!-- create a link on 856$y -->
18 [% END %] <!-- /FOREACH link IN record.856s -->
19 [% FOREACH contents IN record.f505s %] <!-- process each 505 field -->
20 [% FOREACH subf IN contents.subfields %] <!-- process each subfield -->
21 [% SWITCH subf.code %]
22 [% CASE 'a' %]
23 <span class='contents'>[% subf.value %]</span>
24 [% CASE 't' %]
25 <span class='title'>[% subf.value %]</span>
26 [% CASE 'r' %]
27 <span class='responsibility'>[% subf.value %]</span>
28 [% END %]
29 [% END %] <!-- /FOREACH contents.subfields -->
30 [% END %] <!-- /FOREACH contents IN record.f505s -->
31 [% FOREACH subj IN record.f6xxs %]
32 <a href="whatever/[% subj.s9 %]">[% subj.sa %]</a> <!-- create a link on 6[0-9]{2}$a -->
33 [% END %]
34 [% FOREACH field IN record.fields %]
35 [% SWITCH field.tag %]
36 [% CASE '600' %]
37 Subject: [% field.all %] is what we are all about
38 [% CASE '700' %]
39 Co-author: [% field.all %], I presume?
40 [% END %]
41 [% END %]
42  
43 =head1 DESCRIPTION
44  
45 A Template::Toolkit plugin which given a MARC::Record object parses it into a
46 hash that can be accessed directly in Template::Toolkit.
47  
48 =head1 ACCESSORS
49  
50 By using some clever AUTOLOAD acrobatics, this plugin offers the user six
51 types of accessors.
52  
53 =head2 Direct accessors
54  
55 [% record.f245.sa %]
56
57 print $record->f245->sa;
58  
59 By prefixing field numbers with an 'f' and subfield codes with an 's', the first
60 field/subfield with a given tag/code can be accessed.
61  
62 =head2 Concatenated accessors
63  
64 [% record.f245.all %]
65
66 print $record->f245->all;
67  
68 A string consisting of all subfields concatenated together is accessible through
69 the all member of field objects.
70  
71 =head2 Subfield iterators
72  
73 [% FOREACH subfield IN record.f245.subfields %]
74 [% subfield.code %] = [% subfield.value %]
75 [% END %]
76  
77 foreach my $subfield ($record->f245) {
78 print $subfield->code, ' = ', $subfield->value;
79 }
80  
81 Subfield iterators are accessible through the subfields member of field objects.
82  
83 =head2 Field iterators
84  
85 [% FOREACH field IN record.f500s %]
86 [% field.all %]
87 [% END %]
88  
89 foreach my $field ($record->f500s) {
90 print $field->all;
91 }
92  
93 Field iterators are accessible by adding an 's' to the end of field names:
94 f500s, etc.
95  
96 =head2 Section iterators
97  
98 [% FOREACH field IN record.f5xxs %]
99 [% field.all %]
100 [% END %]
101  
102 foreach my $field ($record->f5xxs) {
103 print $field->all;
104 }
105  
106 All the fields in a section (identified by the first digit of the tag) can
107 be accessed with 'fNxxs' and then iterated through.
108  
109 =head2 Complete field list
110  
111 [% FOREACH field IN record.fields %]
112 [% field.all %]
113 [% END %]
114  
115 foreach my $field ($record->fields) {
116 print $field->all;
117 }
118  
119 All the fields in a record can be accessed via the fields object method.
120  
121 =head1 WHAT THIS PLUGIN DOES NOT DO
122  
123 This plugin will not sanity-check your code to make sure that you are accessing
124 fields and subfields with proper allowances for repetition. If you access a value
125 using [% record.f505.st %] it is presumed that was intentional, and there will be
126 no warning or error of any sort.
127  
128 However, the flip-side of this is that this plugin will not dictate your code
129 style. You can access the data using direct (non-repeatable) accessors, by
130 iterating over subfields, by iterating over subfields of a specific tag, by
131 iterating over fields in a particular block (0xx, 1xx, 2xx, etc.), or by
132 iterating over all fields.
133  
134 =cut
135  
136 1 ▶use MARC::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 1 ▶use MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 1 ▶use Template::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶use base qw( Template::Plugin );
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶use Template::Plugin::MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 1 our $VERSION = '0.04';
144  
145 1 our $AUTOLOAD;
146  
147 =head1 METHODS
148  
149 =head2 load
150  
151 Used by Template::Toolkit for loading this plugin.
152  
153 =cut
154  
155 2 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
156 2 my ($class, $context) = @_;
157 2 return $class;
158 }
159  
160 =head2 new
161  
162 Instantiates a new object for the given MARC::Record. Can be called
163 using any of the following declarations:
164  
165 [% USE MARC(mymarc) %]
166 [% USE marc(mymarc) %]
167 [% USE MARC mymarc %]
168 [% USE MARC(marc=mymarc) %]
169 [% USE MARC marc=mymarc %]
170  
171 When run from Perl, the object can be created with either of the following
172 two calling conventions:
173  
174 $record = Template::Plugin::MARC->new({}, $marc, [\%config]);
175 $record = Template::Plugin::MARC->new([$context], { marc => $marc });
176  
177 The $context hashref passed as the first argument is mandatory when using
178 positional parameters and optional when using named parameters.
179  
180 =cut
181  
182 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
183 3 ▶ my $config = ref($_[-1]) eq 'HASH' ? pop(@_) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 1 2 3
184 3 my ($class, $context, $marc) = @_;
185  
186 # marc can be a positional or named argument
187 3 ▶ $marc = $config->{'marc'} unless defined $marc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $marc 1 2 3
188  
189 3 return bless {
190 %$config,
191 marc => $marc,
192 }, $class;
193 }
194  
195 =head2 init
196  
197 Initializes the MARC object. This is called only on the first access attempt
198 on the object, to avoid unnecessary processing.
199  
200 =cut
201  
202 20 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod documented
203 20 my $self = shift;
204 20 ▶ return $self if $self->{'record'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"record"} 18 2 20
205  
206 2 my $recordhash = { fields => [] };
207  
208 2 ▶ if ( ref $self->{'marc'} eq 'MARC::Record' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"marc"} eq "MARC::Record") 2 0 2
209 2 foreach my $field ($self->{'marc'}->fields()) {
210 18 my $fieldobj = Template::Plugin::MARC::Field->new($field);
211 18 my $tag = $fieldobj->tag();
212 18 my $section = 'f' . substr($tag, 0, 1) . 'xxs';
213 18 ▶ $recordhash->{"f$tag"} = $fieldobj unless $recordhash->{"f$tag"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$recordhash{"f$tag"} 14 4 18
214 18 ▶ $recordhash->{"f$tag" . 's'} = [] unless $recordhash->{"f$tag" . 's'};
Branch: 1 branch
BranchTrueFalseTotal
unless $$recordhash{"f$tag" . 's'} 14 4 18
215 18 push @{$recordhash->{"f$tag" . 's'}}, $fieldobj;
216 18 ▶ $recordhash->{"$section"} = [] unless $recordhash->{"$section"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$recordhash{"$section"} 12 6 18
217 18 push @{$recordhash->{"$section"}}, $fieldobj;
218 18 push @{$recordhash->{'fields'}}, $fieldobj;
219 }
220 }
221  
222 2 $self->{'record'} = $recordhash;
223 2 return $self;
224 }
225  
226 =head2 filter
227  
228 $record->filter({ '4' => 'edt' })->[0]->sa
229  
230 [% record.filter('4'='edt').0.sa
231  
232 Filters a set of fields according to the specified criteria
233  
234 =cut
235  
236 6 ▶sub filter {
Subroutine filter: called CC=4 SCAR=13.9
Pod documented
237 6 my ($self, $selectors) = @_;
238  
239 6 $self->init();
240  
241 6 my $fields = $self->{'record'}->{'fields'};
242 6 foreach my $selector (keys %$selectors) {
243 6 my $possibilities = [];
244 6 foreach my $testfield (@$fields) {
245 48 ▶ push @$possibilities, $testfield if $testfield->has($selector, $selectors->{$selector});
Branch: 1 branch
BranchTrueFalseTotal
if $testfield->has($selector, $selectors->{$selector}) 11 37 48
246 }
247 6 $fields = $possibilities;
248 }
249  
250 6 return $fields;
251 }
252  
253 =head2 marc
254  
255 Returns the MARC::Record object associated with the instance.
256  
257 =cut
258  
259 2 ▶sub marc {
Subroutine marc: called CC=1 SCAR=0.0
Pod documented
260 2 my $self = shift;
261 2 return $self->{'marc'};
262 }
263  
264 14 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
265 14 my $self = shift;
266 14 (my $a = $AUTOLOAD) =~ s/.*:://;
267  
268 14 $self->init;
269
270 14 return $self->{'record'}->{"$a"};
271 }
272  
273 1 1;
274 __END__
« MARC/Field.pm ↑ Summary MARC/Subfield.pm »