File Coverage

blib/lib/List/Filter/Storage/CODE.pm
Criterion Covered Total %
statement 71 76 93.4
branch 6 8 75.0
condition 3 9 33.3
subroutine 13 14 92.8
pod 6 6 100.0
total 99 113 87.6


line stmt bran cond sub pod time code
1             package List::Filter::Storage::CODE;
2             #use base qw( List::Filter::StorageBase );
3 1     1   5 use base qw( List::Filter::Storage::MEM );
  1         3  
  1         118  
4              
5             =head1 NAME
6              
7             List::Filter::Storage::CODE - reads in standard libraries of filters from code
8              
9             =head1 SYNOPSIS
10              
11             # This is a plugin, not intended for direct use.
12             # See: List:Filter:Storage
13              
14             use List::Filter::Storage::CODE;
15              
16             # load all of transforms from standard library module location
17             my $storage = List::Filter::Storage::CODE->new({
18             type => 'transform',
19             });
20              
21             # load just the specified filter libraries
22             # (Note: allows non-standard locations, and/or conserves memory)
23             my $storage = List::Filter::Storage::CODE->new({
24             connect_to => [ Some::Odd::Library::Module And::Another ],
25             type => 'filter',
26             });
27              
28             # Retrieving a filter
29             my $filter = $storage->lookup( ':omit' );
30              
31             =head1 DESCRIPTION
32              
33             The L project ships with some standard filters defined
34             in perl code. The "CODE" storage location format allows these filters
35             to be looked up by name using the standard storage interface.
36              
37             This format may be used in two ways:
38              
39             (1) by default it will slurp in all definitions found in all of
40             the modules found in the standard library location for the data
41             type (e.g. for "filter" it will look in
42             "List::Filter::Library::*" , for "transform" it will look in
43             "List::Filter::Transform::Library::*").
44              
45             (2) It can be provided with a list of libary names, (which should
46             correspond to file names: <*>.pm), and it will only load the
47             filters from those particular files.
48              
49             =head2 METHODS
50              
51             =over
52              
53             =cut
54              
55 1     1   16 use 5.8.0;
  1         5  
  1         44  
56 1     1   5 use strict;
  1         2  
  1         33  
57 1     1   6 use warnings;
  1         1  
  1         35  
58 1     1   7 use Carp;
  1         1  
  1         84  
59 1     1   5 use Data::Dumper;
  1         1  
  1         58  
60 1     1   5 use Hash::Util qw( lock_keys unlock_keys );
  1         2  
  1         9  
61 1     1   73 use Module::List::Pluggable qw( list_modules_under import_modules );
  1         1  
  1         9415  
62              
63             our $VERSION = '0.01';
64             my $DEBUG = 0;
65              
66             =item new
67              
68             Instantiates a new List::Filter::Storage::CODE object.
69              
70             With no arguments, the newly created profile will be loaded
71             with all filters from the appropriate installed code libraries.
72              
73             If connect_to is defined as a list of library module names,
74             it will load only those library modules.
75              
76             =cut
77              
78             # Note: "new" (inherited from Class::Base)
79             # calls the following "init" routine automatically.
80              
81             =item init
82              
83             Initialize object attributes and then lock them down to prevent
84             accidental creation of new ones.
85              
86             Note: there is no leading underscore on name "init", though it's
87             arguably an "internal" routine (i.e. not likely to be of use to
88             client code).
89              
90             =cut
91              
92             sub init {
93 8     8 1 195 my $self = shift;
94 8         11 my $args = shift;
95 8         15 unlock_keys( %{ $self } );
  8         44  
96              
97 8         78 $self->SUPER::init( $args ); # uncomment if this is a child class
98              
99 8         13 my $lib_href = {};
100 8         15 my $libraries_list = $args->{ connect_to };
101 8 100       23 if ( $libraries_list ) {
102 2         5 $lib_href = $self->load_given_libraries( $libraries_list );
103             } else {
104 6         17 $lib_href = $self->load_all_libraries;
105             }
106              
107 8         31 $self->set_filter_data( $lib_href );
108              
109 8         11 lock_keys( %{ $self } );
  8         25  
110 8         73 return $self;
111             }
112              
113             =item lookup
114              
115             See "lookup" in L
116              
117             =cut
118              
119             =item save
120              
121             =cut
122              
123             sub save {
124 0     0 1 0 my $self = shift;
125              
126 0         0 $self->debug( "The 'save' method is not implemented for the 'CODE' storage format" );
127              
128 0         0 return 0;
129             }
130              
131             =back
132              
133             =head2 internally used methods
134              
135             =over
136              
137              
138             =item define_library_location
139              
140             From the type of the stored filters (e.g. 'filter', 'transform'),
141             determine the appropriate filter library location in the perl
142             module namespace.
143              
144             This implements the convention:
145              
146             List::Filter::::Library, except that for
147             type 'filter' the class is just "List::Filter::Library::".
148              
149             Gets "type" from the object data, unless supplied as an argument.
150              
151             =cut
152              
153             sub define_library_location {
154 6     6 1 12 my $self = shift;
155 6   33     18 my $type = shift || $self->type;
156 6         8 my $library_location;
157 6 50       26 if (not ($type) ) {
    100          
158 0         0 croak "define_library_location in CODE.pm at line " .
159             __LINE__ .
160             ": needs a defined 'type' (e.g. 'filter', 'transform').";
161              
162             } elsif ($type eq 'filter') {
163 4         6 $library_location = 'List::Filter::Library';
164             } else {
165 2         8 $library_location = 'List::Filter::' . ucfirst( $type ) . '::Library';
166             }
167 6         15 return $library_location;
168             }
169              
170             =item load_all_libraries
171              
172             Loads all available libraries of filters.
173              
174             The type ('filter', 'transform', etc.) will come from the
175             object data, unless passed in as a second argument.
176              
177             =cut
178              
179             sub load_all_libraries {
180 6     6 1 8 my $self = shift;
181 6   33     43 my $type = shift || $self->type;
182 6         22 my $library_location = $self->define_library_location( $type );
183 6         59 my $library_names = list_modules_under( $library_location );
184 6         5293 my $library_href = $self->load_given_libraries( $library_names );
185              
186 6         21 return $library_href;
187             }
188              
189              
190              
191             =item load_given_libraries
192              
193             Loads all requested libraries of filters, from the tree of
194             libraries for the type ('filter', 'transform'). This type
195             must be passed in in the attributes hash reference.
196             The given list of names should be an aref of module names.
197              
198             The type ('filter', 'transform', etc.) will come from the
199             object data, unless passed in as a second argument.
200              
201             =cut
202              
203             # Note, using this feature you can get it to look in nonstandard
204             # locations.
205              
206             sub load_given_libraries {
207 8     8 1 15 my $self = shift;
208 8         15 my $library_names = shift;
209 8   33     41 my $type = shift || $self->type;
210              
211 8         18 my $filter_lib = {};
212 8         12 foreach my $library (@{ $library_names }) {
  8         19  
213 12         942 eval "require $library";
214 12 50       1307 if ($@) {
215 0         0 carp "Problem with require $library:$@";
216             }
217 12         90 my $lib = $library->new();
218 12         121 my $new_lib = $lib->define_filters_href();
219 12         119 merge_hash( $filter_lib, $new_lib );
220             }
221              
222 8         23 return $filter_lib;
223             }
224              
225              
226             =back
227              
228             =head2 proceedural routines
229              
230             =over
231              
232             =item merge_hash
233              
234             This routine does hash addition, merging the key-value pairs of
235             one hash into another.
236              
237             It takes two hash references, and adds the values of the second
238             into the first.
239              
240             Inputs: (1) the summation href, (2) the href to be added into the first.
241              
242             Return: a copy of the summation href, for convenience: don't assume
243             that the first argument isn't modified.
244              
245             =cut
246              
247             sub merge_hash {
248 12     12 1 17 my $big_hash = shift;
249 12         14 my $add_hash = shift;
250              
251 12         14 my @keys = (keys %{ $add_hash });
  12         70  
252 12         32 @{ $big_hash }{ @keys } = @{ $add_hash }{ @keys };
  12         87  
  12         34  
253              
254 12         4144 return $big_hash;
255             }
256              
257             =back
258              
259             =head2 basic setters and getters
260              
261             See L for the basic accessors.
262              
263             =cut
264              
265             1;
266              
267             =head1 SEE ALSO
268              
269             L
270             L
271             L
272              
273             =head1 AUTHOR
274              
275             Joseph Brenner, Edoom@kzsu.stanford.eduE,
276             24 May 2007
277              
278             =head1 COPYRIGHT AND LICENSE
279              
280             Copyright (C) 2007 by Joseph Brenner
281              
282             This library is free software; you can redistribute it and/or modify
283             it under the same terms as Perl itself, either Perl version 5.8.2 or,
284             at your option, any later version of Perl 5 you may have available.
285              
286             =head1 BUGS
287              
288             None reported... yet.
289              
290             =cut