File Coverage

blib/lib/DublinCore/Record.pm
Criterion Covered Total %
statement 80 86 93.0
branch 5 14 35.7
condition 0 3 0.0
subroutine 25 25 100.0
pod 20 20 100.0
total 130 148 87.8


line stmt bran cond sub pod time code
1             package DublinCore::Record;
2              
3             =head1 NAME
4              
5             DublinCore::Record - Container for Dublin Core metadata elements
6              
7             =head1 SYNOPSIS
8              
9             use DublinCore::Record;
10            
11             my $record = DublinCore::Record->new();
12            
13             # later ...
14              
15             # print the title
16             print $record->element( 'title' )->content;
17              
18             ## list context will retrieve all of a particular element
19             foreach my $element ( $record->element( 'Creator' ) ) {
20             print "creator: ", $element->content(), "\n";
21             }
22              
23             ## qualified dublin core
24             my $creation = $record->element( 'Date.created' )->content();
25              
26             =head1 DESCRIPTION
27              
28             DublinCore::Record is an abstract class for manipulating DublinCore metadata.
29             The Dublin Core is a small set of metadata elements for describing information
30             resources. For more information on embedding DublinCore in HTML see RFC 2731
31             L or L
32              
33             =cut
34              
35 4     4   5157 use strict;
  4         11  
  4         5937  
36 4     4   26 use warnings;
  4         10  
  4         295  
37              
38 4     4   36 use Carp qw( croak );
  4         9  
  4         328  
39 4     4   3226 use DublinCore::Element;
  4         17  
  4         52  
40              
41             our $VERSION = '0.03';
42             our @VALID_ELEMENTS = qw(
43             title
44             creator
45             subject
46             description
47             publisher
48             contributor
49             date
50             type
51             format
52             identifier
53             source
54             language
55             relation
56             coverage
57             rights
58             );
59              
60             =head1 METHODS
61              
62             =head2 new()
63              
64             The constructor. Takes no arguments.
65              
66             $record = DublinCore::Record->new();
67              
68             =cut
69              
70             sub new {
71 4     4 1 4857 my $class = shift;
72 4         12 my $self = {};
73              
74 4         177 $self->{ "DC_$_" } = [] for @VALID_ELEMENTS;
75              
76 4         28 bless $self, $class;
77              
78 4         18 $self->add( @_ );
79              
80 4         12 return $self;
81             }
82              
83             =head2 add( @elements )
84              
85             Adds valid DublinCore::Element objects to the record.
86              
87             =cut
88              
89             sub add {
90 34     34 1 226 my $self = shift;
91              
92 34         61 for my $element ( @_ ) {
93 30         30 push @{ $self->{ 'DC_' . lc( $element->name ) } }, $element;
  30         74  
94             }
95             }
96              
97             =head2 remove( @elements )
98              
99             Removes valid DublinCore::Element object from the record.
100              
101             =cut
102              
103             sub remove {
104 1     1 1 6 my $self = shift;
105              
106 1         3 for my $element ( @_ ) {
107 1         8 my $name = 'DC_' . lc( $element->name );
108 1         7 $self->{ $name } = [
109 1         15 grep { $element ne $_ } @{ $self->{ $name } }
  1         3  
110             ];
111             }
112             }
113              
114             =head2 element()
115              
116             This method will return a relevant DublinCore::Element object. When
117             called in a scalar context element() will return the first relevant element
118             found, and when called in a list context it will return all the relevant
119             elements (since Dublin Core elements are repeatable).
120              
121             ## retrieve first title element
122             my $element = $record->element( 'Title' );
123             my $title = $element->content();
124            
125             ## shorthand object chaining to extract element content
126             my $title = $record->element( 'Title' )->content();
127            
128             ## retrieve all creator elements
129             @creators = $record->element( 'Creator' );
130              
131             You can also retrieve qualified elements in a similar fashion.
132              
133             my $date = $record->element( 'Date.created' )->content();
134              
135             In order to fascilitate chaining element() will return an empty
136             DublinCore::Element object when the requested element does not
137             exist. You can check if you're getting an empty empty back by using
138             the is_empty() method.
139              
140             if( $record->element( 'title' )->is_empty ) {
141             # no title
142             }
143              
144             =cut
145              
146             sub element {
147 1     1 1 553 my ( $self, $name ) = @_;
148 1         3 $name = lc( $name );
149              
150             ## must be a valid DC element (with additional qualifier)
151 15         241 croak( "invalid Dublin Core element: $name" )
152 1 50       4 if ! grep { $name =~ /^$_/ } @VALID_ELEMENTS;
153              
154             ## extract qualifier if present
155 1         3 my $qualifier;
156 1         6 ( $name, $qualifier ) = split /\./, $name;
157              
158 1         4 my @elements = ();
159 1         3 foreach my $element ( @{ $self->{ "DC_$name" } } ) {
  1         11  
160 0 0 0     0 if ( $qualifier and $element->qualifier() =~ /$qualifier/i ) {
    0          
161 0         0 push( @elements, $element );
162             } elsif ( !$qualifier ) {
163 0         0 push( @elements, $element );
164             }
165             }
166              
167 1 50       5 if ( wantarray ) { return @elements };
  0         0  
168 1 50       7 return( $elements[ 0 ] ) if $elements[ 0 ];
169              
170             ## otherwise return an empty element object to fascilitate
171             ## chaining when the element doesn't exist :
172             ## $dc->element( 'Title' )->content().
173              
174 1         8 return( DublinCore::Element->new() );
175             }
176              
177             =head2 elements()
178              
179             Returns all the Dublin Core elements found as DublinCore::Element
180             objects which you can then manipulate further.
181              
182             foreach my $element ( $record->elements() ) {
183             print "name=", $element->name(), "\n";
184             print "content=", $element->content(), "\n";
185             }
186              
187             =cut
188              
189             sub elements {
190 2     2 1 2427 my $self = shift;
191 2         6 my @elements = ();
192 2         6 foreach my $type ( @VALID_ELEMENTS ) {
193 30         25 push( @elements, @{ $self->{ "DC_$type" } } );
  30         92  
194             }
195 2         36 return( @elements );
196             }
197              
198             =head2 title()
199              
200             Returns a DublinCore::Element object for the title element. You can then
201             retrieve content, qualifier, scheme, lang attributes like so.
202              
203             my $title = $record->title();
204             print "content: ", $title->content(), "\n";
205             print "qualifier: ", $title->qualifier(), "\n";
206             print "scheme: ", $title->scheme(), "\n";
207             print "language: ", $title->language(), "\n";
208              
209             Since there can be multiple instances of a particular element type (title,
210             creator, subject, etc) you can retrieve multiple title elements by calling
211             title() in a list context.
212              
213             my @titles = $record->title();
214             foreach my $title ( @titles ) {
215             print "title: ", $title->content(), "\n";
216             }
217              
218             =cut
219              
220             sub title {
221 2     2 1 37 my $self = shift;
222 2         8 return( $self->_getElement( 'title', wantarray ) );
223             }
224              
225             =head2 creator()
226              
227             Retrieve creator information in the same manner as title().
228              
229             =cut
230              
231             sub creator {
232 2     2 1 4283 my $self = shift;
233 2         10 return( $self->_getElement( 'creator', wantarray ) );
234             }
235              
236             =head2 subject()
237              
238             Retrieve subject information in the same manner as title().
239              
240             =cut
241              
242             sub subject {
243 2     2 1 3647 my $self = shift;
244 2         8 return( $self->_getElement( 'subject', wantarray ) );
245             }
246              
247             =head2 description()
248              
249             Retrieve description information in the same manner as title().
250              
251             =cut
252              
253             sub description {
254 2     2 1 3758 my $self = shift;
255 2         13 return( $self->_getElement( 'description', wantarray ) );
256             }
257              
258             =head2 publisher()
259              
260             Retrieve publisher information in the same manner as title().
261              
262             =cut
263              
264             sub publisher {
265 2     2 1 4271 my $self = shift;
266 2         733 return( $self->_getElement( 'publisher', wantarray ) );
267             }
268              
269             =head2 contributor()
270              
271             Retrieve contributor information in the same manner as title().
272              
273             =cut
274              
275             sub contributor {
276 3     3 1 14270 my $self = shift;
277 3         15 return( $self->_getElement( 'contributor', wantarray ) );
278             }
279              
280             =head2 date()
281              
282             Retrieve date information in the same manner as title().
283              
284             =cut
285              
286             sub date {
287 2     2 1 3402 my $self = shift;
288 2         8 return( $self->_getElement( 'date', wantarray ) );
289             }
290              
291             =head2 type()
292              
293             Retrieve type information in the same manner as title().
294              
295             =cut
296              
297             sub type {
298 2     2 1 3349 my $self = shift;
299 2         8 return( $self->_getElement( 'type', wantarray ) );
300             }
301              
302             =head2 format()
303              
304             Retrieve format information in the same manner as title().
305              
306             =cut
307              
308             sub format {
309 2     2 1 3410 my $self = shift;
310 2         9 return( $self->_getElement( 'format', wantarray ) );
311             }
312              
313             =head2 identifier()
314              
315             Retrieve identifier information in the same manner as title().
316              
317             =cut
318              
319             sub identifier {
320 2     2 1 3243 my $self = shift;
321 2         10 return( $self->_getElement( 'identifier', wantarray ) );
322             }
323              
324             =head2 source()
325              
326             Retrieve source information in the same manner as title().
327              
328             =cut
329              
330             sub source {
331 2     2 1 3731 my $self = shift;
332 2         11 return( $self->_getElement( 'source', wantarray ) );
333             }
334              
335             =head2 language()
336              
337             Retrieve language information in the same manner as title().
338              
339             =cut
340              
341             sub language {
342 2     2 1 3965 my $self = shift;
343 2         11 return( $self->_getElement( 'language', wantarray ) );
344             }
345              
346             =head2 relation()
347              
348             Retrieve relation information in the same manner as title().
349              
350             =cut
351              
352             sub relation {
353 2     2 1 3274 my $self = shift;
354 2         15 return( $self->_getElement( 'relation', wantarray ) );
355             }
356              
357             =head2 coverage()
358              
359             Retrieve coverage information in the same manner as title().
360              
361             =cut
362              
363             sub coverage {
364 2     2 1 3141 my $self = shift;
365 2         9 return( $self->_getElement( 'coverage', wantarray ) );
366             }
367              
368             =head2 rights()
369              
370             Retrieve rights information in the same manner as title().
371              
372             =cut
373              
374             sub rights {
375 2     2 1 3778 my $self = shift;
376 2         10 return( $self->_getElement( 'rights', wantarray ) );
377             }
378              
379             sub _getElement {
380 31     31   86 my ( $self, $element, $wantarray ) = @_;
381 31         85 my $contents = $self->{ "DC_$element" };
382              
383 31 50       151 if ( $wantarray ) {
    50          
384 0         0 return( @$contents );
385             }
386             elsif ( scalar( @$contents ) > 0 ) {
387 31         118 return( $contents->[ 0 ] );
388             }
389              
390 0           return DublinCore::Element->new();
391             }
392              
393             =head1 SEE ALSO
394              
395             =over 4
396              
397             =item * DublinCore::Element
398              
399             =item * Dublin Core L
400              
401             =item * RFC 2731 L
402              
403             =item * perl4lib L
404              
405             =back
406              
407             =head1 AUTHOR
408              
409             =over 4
410              
411             =item * Ed Summers Eehs@pobox.comE
412              
413             =item * Brian Cassidy Ebricas@cpan.orgE
414              
415             =back
416              
417             =head1 COPYRIGHT AND LICENSE
418              
419             Copyright 2007 by Ed Summers, Brian Cassidy
420              
421             This library is free software; you can redistribute it and/or modify
422             it under the same terms as Perl itself.
423              
424             =cut
425              
426             1;