blib/lib/Keystone/Resolver/Descriptor.pm

filter: Statementstmt 94.0% 47 / 50 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 10 / 10 totaltotal 90.5% 77 / 85 CC 6 SCAR 8.4
CC
6
Cov
88%
_data1
5.6
_data
2.0
superdata1
2.0
CRAP 2.3
« blib/lib/Keystone/Resolver/plugins/ServiceType/citation.pm ↑ Summary blib/lib/Keystone/Resolver/LogLevel.pm »
1 # $Id: Descriptor.pm,v 1.1 2007-01-26 12:33:15 mike Exp $
2  
3 package Keystone::Resolver::Descriptor;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<author = "taylor">,
35 we store
36 C<author = [ "taylor", "wedel" ]>.
37  
38 Three utility methods are provided for application such as Keystone
39 Resolver that use C<Descriptor> objects in this way:
40 C<metadata1()>,
41 C<superdata1()>
42 and
43 C<push_metadata()>
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 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
60 1 my $class = shift();
61 1 my($name) = @_;
62  
63 1 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 ▶sub name { my $this = shift(); return $this->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
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 ▶sub metadata { my $this = shift(); return $this->_data("metadata", @_); }
Subroutine metadata: called CC=1 SCAR=0.0
Pod documented
96 5 ▶sub superdata { my $this = shift(); return $this->_data("superdata", @_); }
Subroutine superdata: called CC=1 SCAR=0.0
Pod documented
97  
98 9 ▶sub _data {
Subroutine _data: called CC=2 SCAR=6.9
99 9 my $this = shift();
100 9 my($table, $name, $value) = @_;
101  
102 9 my $old = $this->{$table}->{$name};
103 9 ▶ $this->{$table}->{$name} = $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 3 6 9
104 if defined $value;
105  
106 9 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<metadata1()> 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<superdata1()> behaves the same but operates on the descriptor's
124 superdata space instead of its metadata space.
125  
126 =cut
127  
128 2 ▶sub metadata1 { my $this = shift(); return $this->_data1("metadata", @_); }
Subroutine metadata1: called CC=1 SCAR=0.0
Pod documented
129 0 ▶sub superdata1 { my $this = shift(); return $this->_data1("superdata", @_); }
Subroutine superdata1: not called CC=1 SCAR=6.9
Pod documented
130  
131 2 ▶sub _data1 {
Subroutine _data1: called CC=5 SCAR=17.3
132 2 my $this = shift();
133 2 my($table, $name, $value) = @_;
134 2 ▶ die "Oops! value '$value' supplied to data1()" if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 2 2
135  
136 2 my $refs = $this->{$table}->{$name};
137 2 ▶ return undef if !defined $refs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $refs 0 2 2
138 2 ▶ die "data1(): no $table values for " . $this->name()
Branch: 1 branch
BranchTrueFalseTotal
if @$refs == 0 0 1 1
139 if @$refs == 0;
140 1 ▶ if (@$refs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$refs > 1) 0 1 1
141 ### Should use $openURL->warn(), but we don't have a $openURL
142 0 warn("data1(): multiple $table values for " . $this->name() .
143 "($name): " . join(", ", @$refs));
144 }
145  
146 1 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<metadata_keys()> returns a list of all the keys for which the
157 descriptor has a metadata value.
158 C<superdata_keys()> returns a list of all the keys for which the
159 descriptor has a superdata value.
160  
161 =cut
162  
163 2 ▶sub metadata_keys {
Subroutine metadata_keys: called CC=1 SCAR=0.0
Pod documented
164 2 my $this = shift();
165  
166 2 return sort keys %{ $this->{metadata} };
167 }
168  
169 2 ▶sub superdata_keys {
Subroutine superdata_keys: called CC=1 SCAR=0.0
Pod documented
170 2 my $this = shift();
171  
172 2 return sort keys %{ $this->{superdata} };
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<delete_metadata()>.
185  
186 =cut
187  
188 1 ▶sub delete_superdata {
Subroutine delete_superdata: called CC=1 SCAR=0.0
Pod documented
189 1 my $this = shift();
190 1 my($name) = @_;
191  
192 1 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<push_superdata()>.
214  
215 =cut
216  
217 2 ▶sub push_metadata {
Subroutine push_metadata: called CC=1 SCAR=0.0
Pod documented
218 2 my $this = shift();
219 2 my($name, @values) = @_;
220  
221 2 push @{ $this->{metadata}->{$name} }, @values;
222 }
223  
224  
225 1 1;
« blib/lib/Keystone/Resolver/plugins/ServiceType/citation.pm ↑ Summary blib/lib/Keystone/Resolver/LogLevel.pm »