File Coverage

blib/lib/Keystone/Resolver/Descriptor.pm
Criterion Covered Total %
statement 46 49 93.8
branch 6 10 60.0
condition n/a
subroutine 14 15 93.3
pod 10 10 100.0
total 76 84 90.4


line stmt bran cond sub pod time code
1             # $Id: Descriptor.pm,v 1.1 2007-01-26 12:33:15 mike Exp $
2              
3             package Keystone::Resolver::Descriptor;
4              
5 1     1   6837 use strict;
  1         2  
  1         38  
6 1     1   6 use warnings;
  1         1  
  1         34  
7 1     1   44161 use LWP;
  1         102426  
  1         2009  
8              
9              
10             =head1 NAME
11              
12             Keystone::Resolver::Descriptor - a Descriptor in an OpenURL v1.0 ContextObject
13              
14             =head1 SYNOPSIS
15              
16             $des = new Keystone::Resolver::Descriptor("rft");
17             $des->superdata(ref => "http://some.host/path/entity.kev");
18             $des->metadata(aulast => [ "Wedel" ]);
19             $ids = $des->superdata("id");
20             @authors = $des->metadata("aulast");
21              
22             =head1 DESCRIPTION
23              
24             A Descriptor is a small data structure containing information
25             describing one of the six OpenURL v1.0 entities (referent, referer,
26             etc.) Each Descriptor has a name, and contains both metadata (author,
27             title, etc.) and what we will call superdata (identifier, descriptor
28             format and suchlike), which are held in two different spaces.
29              
30             Although this module neither knows nor cares what kind of information
31             is stored in the metadata and superdata hashes, it's worth knowing
32             that the way Keystone Resolver uses this is by storing references to
33             arrays of scalars. In other words, instead of storing
34             C,
35             we store
36             C.
37              
38             Three utility methods are provided for application such as Keystone
39             Resolver that use C objects in this way:
40             C,
41             C
42             and
43             C
44              
45             =head1 METHODS
46              
47             =cut
48              
49              
50             =head2 new()
51              
52             $des = new Keystone::Resolver::Descriptor($name);
53              
54             Constructs a new Descriptor with the specified name, and with
55             (initially) no metadata or superdata.
56              
57             =cut
58              
59             sub new {
60 1     1 1 119 my $class = shift();
61 1         4 my($name) = @_;
62              
63 1         10 return bless {
64             name => $name,
65             metadata => {},
66             superdata => {},
67             }, $class;
68             }
69              
70              
71             =head2 name()
72              
73             Returns the name with which the descriptor was created.
74              
75             =cut
76              
77 1     1 1 41 sub name { my $this = shift(); return $this->{name} }
  1         10  
78              
79              
80             =head2 metadata(), superdata()
81              
82             $oldfoo = $des->metadata("foo");
83             $des->metadata(foo => $newfoo);
84             # ...
85             $des->metadata(foo => $oldfoo);
86              
87             These two methods behave the same way, but operate on different
88             data-spaces. Each one returns the value associated with the key whose
89             name is specified in by the first parameter. If a second parameter is
90             also specified, then it becomes the new value associated with that key
91             (although the old value is still returned).
92              
93             =cut
94              
95 4     4 1 41 sub metadata { my $this = shift(); return $this->_data("metadata", @_); }
  4         14  
96 5     5 1 48 sub superdata { my $this = shift(); return $this->_data("superdata", @_); }
  5         14  
97              
98             sub _data {
99 9     9   10 my $this = shift();
100 9         18 my($table, $name, $value) = @_;
101              
102 9         16 my $old = $this->{$table}->{$name};
103 9 100       28 $this->{$table}->{$name} = $value
104             if defined $value;
105              
106 9         29 return $old;
107             }
108              
109              
110             =head2 metadata1(), superdata1()
111              
112             $des->metadata(foo => [ "bar" ]);
113             $res = $des->metadata("foo");
114             die if ref($ref) ne "ARRAY";
115             $scalar = $des->metadata1("foo");
116             die if ref($ref);
117              
118             C returns the first element of the array whose reference
119             is stored in a descriptor's metadata space under the specified key.
120             It is a fatal error if the array has zero elements, and a warning is
121             issued if it has more than one.
122              
123             C behaves the same but operates on the descriptor's
124             superdata space instead of its metadata space.
125              
126             =cut
127              
128 2     2 1 3 sub metadata1 { my $this = shift(); return $this->_data1("metadata", @_); }
  2         8  
129 0     0 1 0 sub superdata1 { my $this = shift(); return $this->_data1("superdata", @_); }
  0         0  
130              
131             sub _data1 {
132 2     2   3 my $this = shift();
133 2         4 my($table, $name, $value) = @_;
134 2 50       7 die "Oops! value '$value' supplied to data1()" if defined $value;
135              
136 2         5 my $refs = $this->{$table}->{$name};
137 2 50       7 return undef if !defined $refs;
138 2 50       19 die "data1(): no $table values for " . $this->name()
139             if @$refs == 0;
140 1 50       4 if (@$refs > 1) {
141             ### Should use $openURL->warn(), but we don't have a $openURL
142 0         0 warn("data1(): multiple $table values for " . $this->name() .
143             "($name): " . join(", ", @$refs));
144             }
145              
146 1         39 return $refs->[0];
147             }
148              
149              
150             =head2 metadata_keys(), superdata_keys()
151              
152             foreach my $name ($des->metadata_keys()) {
153             print $name, " -> ", $des->metadata($name), "\n";
154             }
155              
156             C returns a list of all the keys for which the
157             descriptor has a metadata value.
158             C returns a list of all the keys for which the
159             descriptor has a superdata value.
160              
161             =cut
162              
163             sub metadata_keys {
164 2     2 1 37 my $this = shift();
165              
166 2         2 return sort keys %{ $this->{metadata} };
  2         19  
167             }
168              
169             sub superdata_keys {
170 2     2 1 3 my $this = shift();
171              
172 2         4 return sort keys %{ $this->{superdata} };
  2         16  
173             }
174              
175              
176             =head2 delete_superdata()
177              
178             $des->delete_superdata("ref");
179             $oldval = $des->delete_superdata("ref_fmt");
180              
181             Deletes the named superdata element from the descriptor, returning its
182             old value if any.
183              
184             There is at present no corresponding C.
185              
186             =cut
187              
188             sub delete_superdata {
189 1     1 1 2 my $this = shift();
190 1         2 my($name) = @_;
191              
192 1         5 return delete $this->{superdata}->{$name};
193             }
194              
195              
196             =head2 push_metadata()
197              
198             $des->push_metadata(foo => $extraFoo1, $extraFoo2, ...);
199              
200             To be used only when the metadata keys are list-references. Appends
201             the specified values to the list associated with the specified name.
202             The following two code-fragments are equivalent:
203              
204             $des->metadata(foo => []);
205             $des->push_metadata(foo => 1);
206             $des->push_metadata(foo => 2, 3);
207              
208             and
209              
210             $des->metadata(foo => [ 1 2 ]);
211             $des->push_metadata(foo => 3);
212              
213             There is at present no corresponding C.
214              
215             =cut
216              
217             sub push_metadata {
218 2     2 1 4 my $this = shift();
219 2         6 my($name, @values) = @_;
220              
221 2         2 push @{ $this->{metadata}->{$name} }, @values;
  2         11  
222             }
223              
224              
225             1;