Resource/Location/Type.pm

filter: Statementstmt 37.3% 28 / 75 Branchbran 25.0% 7 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 72.7% 8 / 11 Podpod 100% 7 / 7 totaltotal 39.6% 50 / 126 CC 17 SCAR 23.8
CC
17
Cov
33%
commit
23.5
delete
14.1
all_types
12.0
CRAP 10.8
« Message.pm ↑ Summary Review.pm »
1 package MyLibrary::Resource::Location::Type;
2  
3 4 ▶use MyLibrary::DB;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 MyLibrary::Resource::Location::Type
10  
11 =head1 SYNOPSIS
12  
13  
14 # require the necessary module
15 use MyLibrary::Resource::Location::Type;
16  
17 # create a new Location Type object
18 my $location_type = MyLibrary::Resource::Location::Type->new();
19  
20 # set the attributes of a Location Type object
21 $location_type->name();
22 $location_type->description();
23  
24 # commit Location Type
25 $location_type->commit();
26  
27 # output the Location Type id
28 my $location_type_id = $location_type->location_type_id();
29  
30 # delete a Location Type from the database
31 $location_type->delete();
32  
33 # return a list of all type ids for processing
34 my @location_type_ids = MyLibrary::Resource::Location::Type->all_types();
35  
36 =head1 DESCRIPTION
37  
38 This is a sub-class of the Location class which is used to represent individual resource location types and allow manipulation of resource location type data. Each location will be assigned one location type. Multiple location types can exist, each representing a method by which a resource can be accessed. Certain resources will be accessed only via physical methods, while others will be purely digital. Each type should have a description assigned which clearly states the mode of access for that type.
39  
40 =head1 METHODS
41  
42 =head2 new()
43  
44 This constructor method is used to create a resource location type object. The object can then be manipulated using the various accessor methods supplied with this module.
45  
46 # create a new resource location type object
47 my $resource_location_type = MyLibrary::Resource::Location::Type->new();
48  
49 This method can also be called using an id parameter which will then return an object using the persistent data store. If the called location does not exist, this method will return 'undef'.
50  
51 # create an object from persistent data using the location type id
52 my $resource_location_type = MyLibrary::Resource::Location::Type->new(id => $loc_id);
53  
54 This method can also be called using the 'name' parameter, so that an object can be created based on the name of the location type. Each location type name must be unique, so only one type will be retrieved using the method in this fashion.
55  
56 # create an object from persistent data using the location type name
57 my $resource_location_type = MyLibrary::Resource::Location::Type->new(name => $loc_name);
58  
59 =cut
60  
61 2 ▶sub new {
Subroutine new: called CC=5 SCAR=24.4
Pod documented
62  
63 2 my ($class, %opts) = @_;
64 2 my $self = {};
65  
66 2 ▶ if ($opts{id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'id'}) { } 0 2 2
elsif ($opts{'name'}) { } 1 1 2
67  
68 0 my $dbh = MyLibrary::DB->dbh();
69 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM resource_location_type WHERE type_id = ?', undef, $opts{id});
70  
71 0 ▶ if (ref($rv) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
72 0 $self = $rv;
73 } else {
74 0 return;
75 }
76  
77 } elsif ($opts{name}) {
78  
79 1 my $dbh = MyLibrary::DB->dbh();
80 0 my $rv = $dbh->selectrow_hashref('SELECT * FROM resource_location_type WHERE type_name = ?', undef, $opts{name});
81  
82 0 ▶ if (ref($rv) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rv eq 'HASH') { } 0 0 0
83 0 $self = $rv;
84 } else {
85 0 return;
86 }
87 }
88  
89 # return the object
90 1 return bless $self, $class;
91  
92 }
93  
94  
95 =head2 name()
96  
97 This attribute method should be used to either set or retrieve the name for this location type. This name will appear in any context where location type labeling is required.
98  
99 # get location type name
100 my $location_type_name = $location_type->name();
101  
102 # set the location type name
103 $location_type->name('URL WEB SITE');
104  
105 =cut
106  
107 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
108  
109 2 my ($self, $name) = @_;
110 2 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 1 2
111 1 $self->{type_name} = $name;
112 } else {
113 1 return $self->{type_name};
114 }
115 }
116  
117 =head2 description()
118  
119 The description should indicate the mode of access for a location type. This will assist in the usage of a particular type.
120  
121 # retrieve the current location type
122 my $location_type_description = $location_type->description();
123  
124 # set the location type description
125 $location_type->description('This location type applies to any resource which is web accessible.');
126  
127 =cut
128  
129 2 ▶sub description {
Subroutine description: called CC=2 SCAR=6.9
Pod documented
130  
131 2 my ($self, $description) = @_;
132 2 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) { } 1 1 2
133 1 $self->{type_description} = $description;
134 } else {
135 1 return $self->{type_description};
136 }
137  
138 }
139  
140 =head2 location_type_id()
141  
142 This method can only be used to retrieve the location type id of the current location type. It cannot be used to set the location type id, as this is set internally.
143  
144 # get the current location type id
145 my $location_type_id = location_type_id();
146  
147 =cut
148  
149 0 ▶sub location_type_id {
Subroutine location_type_id: not called CC=2 SCAR=17.9
Pod documented
150  
151 0 my $self = shift;
152 0 ▶ if ($self->{type_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type_id'}) { } 0 0 0
153 0 return $self->{type_id};
154 } else {
155 0 return;
156 }
157  
158 }
159  
160 =head2 commit()
161  
162 This method is used to save a location type to the database.
163  
164 # commit the location type
165 $location_type->commit();
166  
167 =cut
168  
169 1 ▶sub commit {
Subroutine commit: called CC=5 SCAR=31.6
Pod documented
170  
171 1 my $self = shift;
172  
173 1 my $dbh = MyLibrary::DB->dbh();
174  
175 0 ▶ if ($self->location_type_id()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->location_type_id) { } 0 0 0
176  
177 0 my $return = $dbh->do('UPDATE resource_location_type SET type_name = ?, type_description = ? WHERE type_id = ?', undef, $self->name(), $self->description(), $self->location_type_id());
178 0 ▶ if ($return > 1 || $return eq undef) { croak "Location type update in commit() failed. $return records were updated." }
Branch: 1 branch
BranchTrueFalseTotal
if ($return > 1 or $return eq undef) 0 0 0
Condition0%0/3
$return > 1 or $return eq undef
l!l&&r!l&&!r
000
MC/DC0%0/2
$return > 1 or $return eq undef
$return > 1 $return eq undef
Truth table0%0/3
A or B
A= $return > 1B= $return eq undef
ABresult
1X1
011
000
179  
180 } else {
181
182 0 my $id = MyLibrary::DB->nextID();
183  
184 0 my $return = $dbh->do('INSERT INTO resource_location_type (type_id, type_name, type_description) VALUES (?, ?, ?)', undef, $id, $self->name(), $self->description());
185 0 ▶ if ($return != 1) { croak 'Location type commit() failed.'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($return != 1) 0 0 0
186 0 $self->{type_id} = $id;
187 }
188  
189 0 return 1;
190  
191 }
192  
193 =head2 delete()
194  
195 Use this method to remove a location type from the database
196  
197 # remove a location type from the database
198 $location_type->delete();
199  
200 =cut
201  
202 0 ▶sub delete {
Subroutine delete: not called CC=4 SCAR=26.4
Pod documented
203  
204 0 my $self = shift;
205 0 my $dbh = MyLibrary::DB->dbh();
206 0 my $rv = $dbh->do('DELETE FROM resource_location_type WHERE type_id = ?', undef, $self->location_type_id());
207 0 my $return_code;
208 0 ▶ if ($rv != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 1) { } 0 0 0
209 0 croak ('Deletion of resource location failed in delete() method.');
210 } else {
211 0 $return_code = "$rv";
212 }
213  
214 # delete any dangling locations using this location type
215 0 my $location_ids = $dbh->selectcol_arrayref('SELECT resource_location_id FROM resource_location WHERE resource_location_type = ?', undef, $self->location_type_id());
216 0 ▶ if (scalar(@{$location_ids}) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$location_ids;} >= 1) 0 0 0
217 0 foreach my $location_id (@$location_ids) {
218 4 ▶ use MyLibrary::Resource::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 my $delete_location = MyLibrary::Resource::Location->new(id => $location_id);
220 0 $delete_location->delete();
221 }
222 }
223  
224 0 return $return_code;
225  
226 }
227  
228 =head2 all_types()
229  
230 This is a class method which will return upon invocation the full list of location type ids. If no location types exist in the databse, the method will return undef.
231  
232 # return a list of all location type ids
233 my @location_type_ids = MyLibrary::Resource::Location::Type->all_types();
234  
235 =cut
236  
237 0 ▶sub all_types {
Subroutine all_types: not called CC=3 SCAR=24.8
Pod documented
238  
239 0 my $class = shift;
240  
241 0 ▶ unless ($class eq 'MyLibrary::Resource::Location::Type') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class eq "MyLibrary::Resource::Location::Type") 0 0 0
242  
243 0 croak ("This method must be called as a class method. $class was the invocant.");
244  
245 }
246
247 0 my $dbh = MyLibrary::DB->dbh();
248 0 my $type_ids = $dbh->selectcol_arrayref('SELECT type_id FROM resource_location_type');
249  
250 0 ▶ if (scalar(@{$type_ids}) >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$type_ids;} >= 1) { } 0 0 0
251  
252 0 return @{$type_ids};
253  
254 } else {
255  
256 0 return;
257  
258 }
259  
260 }
261  
262 =head1 SEE ALSO
263  
264 For more information, see the MyLibrary home page: http://dewey.library.nd.edu/mylibrary/.
265  
266 =head1 AUTHOR
267  
268 Robert Fox <rfox2@nd.edu>
269  
270 =cut
271  
272 4 return 1;
« Message.pm ↑ Summary Review.pm »