Filter/StorageBase.pm

filter: Statementstmt 52.4% 53 / 101 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 12 / 24 Podpod 100% 17 / 17 totaltotal 58.1% 86 / 148 CC 4 SCAR 5.6
CC
4
Cov
53%
define_filter_class
3.0
init
2.0
lookup
2.0
CRAP 1.7
« Filter/Internal.pm ↑ Summary Filter/Storage/YAML.pm »
1 package List::Filter::StorageBase;
2 1 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4  
5 =head1 NAME
6  
7 StorageBase - base class for filter storage plugins
8  
9 =head1 SYNOPSIS
10  
11 package List::Filter::Storage::NewFormat;
12 use base qw( List::Filter::StorageBase );
13  
14 sub init {
15 }
16  
17 sub lookup {
18 # ...
19 }
20  
21 sub save {
22 # ...
23 }
24  
25 sub list_filters {
26 # ...
27 }
28  
29  
30 1;
31  
32  
33 =head1 DESCRIPTION
34  
35 This is module is purely an interface, which the storage plugins
36 are intended to inherit from. The documentation of this module
37 is thus oriented toward people interested in writing new storage plugins,
38 see the existing plugins for documentation on how to use them.
39  
40 =head1 OBJECT DATA
41  
42 The main fields inside the object:
43  
44 =over
45  
46 =item connect_to
47  
48 In the case of DBI, this will be the database connection string,
49 indicating the DBD driver and the database name.
50  
51 =item owner
52  
53 A user name, when required to make the connection.
54  
55 =item password
56  
57 A password, to go with the user name.
58  
59 =item attributes
60  
61 A hash reference of additional attributes to be used by the
62 storage back-end in any way that seems appropriate.
63  
64 In the case of DBI, this hash ref might contain something like this:
65  
66 { RaiseError => 1, AutoCommit => 1 }
67  
68 (Though there's no particular point in manipulating AutoCommit with
69 storage needs this simple).
70  
71 =item type
72  
73 The type of the filters being stored (e.g. 'filter', 'transform').
74 Not to be confused with the data storage format (e.g 'YAML', 'DBI')
75  
76 =item extra
77  
78 A catch-all hash reference intended to be used primarily for
79 internal storage purposes by the subclasses, e.g. in the case of
80 YAML, this will contain a reference to the contents of an entire
81 YAML file that has been slurped into memory. Writing additional
82 accessors for the data inside of "extra" is strongly advised, to
83 make it easier to modifiy the internal structure at a later date.
84  
85 =back
86  
87 =head1 METHODS
88  
89 There are two main methods that need to be implemented:
90  
91 =over
92  
93 =cut
94  
95 1 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use Hash::Util qw( lock_keys unlock_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 1 our $VERSION = '0.01';
103 1 my $DEBUG = 0;
104  
105 =item new
106  
107 Instantiates a new object.
108  
109 Takes an optional hashref as an argument, with named fields
110 identical to the names of the object attributes.
111  
112 With no arguments, the newly created filter will be empty.
113  
114 =cut
115  
116 # Note: "new" (inherited from Class::Base)
117 # calls the following "init" routine automatically.
118  
119 =item init
120  
121 Initialize object attributes and then lock them down to prevent
122 accidental creation of new ones.
123  
124 Note: there is no leading underscore on name "init", though it's
125 arguably an "internal" routine (i.e. not likely to be of use to
126 client code).
127  
128 Optionally, a plugin may run additional initialization code inside
129 of an init method that overrides this stub.
130  
131 =cut
132  
133 53 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod documented
134 53 my $self = shift;
135 53 my $args = shift;
136 53 unlock_keys( %{ $self } );
137  
138 53 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 53 53
139 0 $self->debugging(1);
140 }
141  
142 # define new attributes
143 my $attributes = {
144 connect_to => $args->{ connect_to },
145 owner => $args->{ owner },
146 password => $args->{ password },
147 attributes => $args->{ attributes },
148 type => $args->{ type }, # type of filters to be stored
149 53 extra => {}, # internal storage, etc. keys not locked.
150 };
151  
152 # add attributes to object
153 53 my @fields = (keys %{ $attributes });
154 53 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
155  
156 53 lock_keys( %{ $self } );
157 53 return $self;
158 }
159  
160  
161  
162 =item define_filter_class
163  
164 From the type of the stored filters (e.g. 'filter', 'transform'),
165 determine the appropriate class.
166  
167 This implements the convention:
168 List::Filter::<type>, except that for type 'filter' the class is
169 just "List::Filter".
170  
171 =cut
172  
173 12 ▶sub define_filter_class {
Subroutine define_filter_class: called CC=3 SCAR=11.1
Pod documented
174 12 my $self = shift;
175 12 my $type = $self->type;
176 12 my $class;
177 12 ▶ if (not ($type) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 0 12 12
elsif ($type eq 'filter') { } 9 3 12
178 0 croak "define_filter_class in StorageBase at line " . __LINE__ . ": needs a defined 'type' (e.g. 'filter', 'transform').";
179 } elsif ($type eq 'filter') {
180 9 $class = 'List::Filter';
181 } else {
182 3 $class = 'List::Filter::' . ucfirst( $type );
183 }
184 12 return $class;
185 }
186  
187  
188  
189 =back
190  
191 =head2 main methods (stubs)
192  
193 =over
194  
195 =item lookup
196  
197 Given a name, returns the first filter found with a matching name.
198  
199 =cut
200  
201 0 ▶sub lookup {
Subroutine lookup: not called CC=1 SCAR=6.9
Pod documented
202 0 my $self = shift;
203 0 my $name = shift;
204 0 my $class = ref $self;
205 0 carp "The lookup method has not been implemented for this class: $class.";
206 0 return undef;
207 }
208  
209  
210 =item save
211  
212 Given a filter saves it to the storage location indicated by the
213 "write_storage" setting of the List::Filter::Storage
214 object, using the name indicated by the "name" field inside of
215 the filter.
216  
217 =cut
218  
219 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
220 0 my $self = shift;
221 0 my $filter = shift;
222 0 my $class = ref $self;
223 0 carp "The save method has not been implemented for this class: $class";
224 0 return undef;
225 }
226  
227  
228  
229 =item list_filters
230  
231 Returns a list of all available filters.
232  
233 =cut
234  
235 0 ▶sub list_filters {
Subroutine list_filters: not called CC=1 SCAR=6.9
Pod documented
236 0 my $self = shift;
237 0 carp "The list_filters method has not been implemented for this class.";
238 0 return undef;
239 }
240  
241  
242  
243 =back
244  
245 =head2 basic accessors
246  
247 =over
248  
249 =item connect_to
250  
251 Getter for object attribute connect_to
252  
253 =cut
254  
255 20 ▶sub connect_to {
Subroutine connect_to: called CC=1 SCAR=0.0
Pod documented
256 20 my $self = shift;
257 20 my $connect_to = $self->{ connect_to };
258 20 return $connect_to;
259 }
260  
261  
262 =item owner
263  
264 Getter for object attribute owner
265  
266 =cut
267  
268 0 ▶sub owner {
Subroutine owner: not called CC=1 SCAR=6.9
Pod documented
269 0 my $self = shift;
270 0 my $owner = $self->{ owner };
271 0 return $owner;
272 }
273  
274 =item set_owner
275  
276 Setter for object attribute set_owner
277  
278 =cut
279  
280 0 ▶sub set_owner {
Subroutine set_owner: not called CC=1 SCAR=6.9
Pod documented
281 0 my $self = shift;
282 0 my $owner = shift;
283 0 $self->{ owner } = $owner;
284 0 return $owner;
285 }
286  
287  
288 =item password
289  
290 Getter for object attribute password
291  
292 =cut
293  
294 0 ▶sub password {
Subroutine password: not called CC=1 SCAR=6.9
Pod documented
295 0 my $self = shift;
296 0 my $password = $self->{ password };
297 0 return $password;
298 }
299  
300 =item set_password
301  
302 Setter for object attribute set_password
303  
304 =cut
305  
306 0 ▶sub set_password {
Subroutine set_password: not called CC=1 SCAR=6.9
Pod documented
307 0 my $self = shift;
308 0 my $password = shift;
309 0 $self->{ password } = $password;
310 0 return $password;
311 }
312  
313 =item set_connect_to
314  
315 Setter for object attribute set_connect_to
316  
317 =cut
318  
319 0 ▶sub set_connect_to {
Subroutine set_connect_to: not called CC=1 SCAR=6.9
Pod documented
320 0 my $self = shift;
321 0 my $connect_to = shift;
322 0 $self->{ connect_to } = $connect_to;
323 0 return $connect_to;
324 }
325  
326  
327  
328 =item attributes
329  
330 Getter for object attribute attributes
331  
332 =cut
333  
334 0 ▶sub attributes {
Subroutine attributes: not called CC=1 SCAR=6.9
Pod documented
335 0 my $self = shift;
336 0 my $attributes = $self->{ attributes };
337 0 return $attributes;
338 }
339  
340 =item set_attributes
341  
342 Setter for object attribute set_attributes
343  
344 =cut
345  
346 0 ▶sub set_attributes {
Subroutine set_attributes: not called CC=1 SCAR=6.9
Pod documented
347 0 my $self = shift;
348 0 my $attributes = shift;
349 0 $self->{ attributes } = $attributes;
350 0 return $attributes;
351 }
352  
353 =item type
354  
355 Getter for object attribute type
356  
357 =cut
358  
359 26 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
360 26 my $self = shift;
361 26 my $type = $self->{ type };
362 26 return $type;
363 }
364  
365 =item set_type
366  
367 Setter for object attribute set_type
368  
369 =cut
370  
371 0 ▶sub set_type {
Subroutine set_type: not called CC=1 SCAR=6.9
Pod documented
372 0 my $self = shift;
373 0 my $type = shift;
374 0 $self->{ type } = $type;
375 0 return $type;
376 }
377  
378 =item extra
379  
380 Getter for object attribute extra
381  
382 =cut
383  
384 78 ▶sub extra {
Subroutine extra: called CC=1 SCAR=0.0
Pod documented
385 78 my $self = shift;
386 78 my $extra = $self->{ extra };
387 78 return $extra;
388 }
389  
390 =item set_extra
391  
392 Setter for object attribute set_extra
393  
394 =cut
395  
396 0 ▶sub set_extra {
Subroutine set_extra: not called CC=1 SCAR=6.9
Pod documented
397 0 my $self = shift;
398 0 my $extra = shift;
399 0 $self->{ extra } = $extra;
400 0 return $extra;
401 }
402  
403  
404  
405  
406  
407  
408 1 1;
409  
410  
411 =back
412  
413 =head1 SEE ALSO
414  
415 L<List::Filter>
416 L<List::Filter::Storage::YAML>
417 L<List::Filter::Storage::DBI>
418  
419 =head1 AUTHOR
420  
421 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>,
422 18 May 2007
423  
424 =head1 COPYRIGHT AND LICENSE
425  
426 Copyright (C) 2007 by Joseph Brenner
427  
428 This library is free software; you can redistribute it and/or modify
429 it under the same terms as Perl itself, either Perl version 5.8.2 or,
430 at your option, any later version of Perl 5 you may have available.
431  
432 =head1 BUGS
433  
434 None reported... yet.
435  
436 =cut
« Filter/Internal.pm ↑ Summary Filter/Storage/YAML.pm »