Filter/Dispatcher.pm

filter: Statementstmt 88.2% 60 / 68 Branchbran n/a Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 85.4% 82 / 96 CC 2 SCAR 2.1
CC
2
Cov
86%
apply
2.0
set_plugin_root
2.0
set_plugin_exceptions
2.0
CRAP 1.2
« Filter/Storage/MEM.pm ↑ Summary
1 package List::Filter::Dispatcher;
2 4 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 List::Filter::Dispatcher -
7  
8 =head1 SYNOPSIS
9  
10 use List::Filter::Dispatcher;
11 my $dispatcher = List::Filter::Dispatcher->new(
12 { plugin_root => 'List::Filter::Filters',
13 plugin_exceptions => 'List::Filter::Filters::Ext::Nogoodnik',
14 } );
15 my $aref_out = $dispatcher->apply( $filter, $aref_in );
16  
17 =head1 DESCRIPTION
18  
19 The Dispatcher object is told where to look for modules that
20 contain the methods that can apply a L<List::Filter> filter.
21 During it's init phase the dispatcher does the necessary requires
22 of each of those method-supplying modules, which must be designed
23 to export these methods to the Dispatcher's namespace.
24  
25 It's expected that when a new Filter object (or one of it's
26 inheritors) is created, it will be assigned a dispatcher so that
27 it will be able to execute the filter's methods. See
28 L<List::Filter>.
29  
30  
31 =head2 MOTIVATION
32  
33 This is part of an extension mechanism to allow the creation of
34 additional filter filter methods that the existing code framework
35 will be able to use without modification.
36  
37 One advantage of this approach is that each filter object has a
38 default method (accessed via the L<apply> method), and yet it can
39 be applied with a different method if that seems desireable.
40  
41 For example: an "omit" filter could be inverted to display
42 only the items that are usually omitted.
43  
44 See L<"List::Filter::Project/Extension mechanisms"> for
45 instructions on writing methods, and creating filters that use
46 them.
47  
48 =head2 METHODS
49  
50 =over
51  
52 =cut
53  
54 4 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 my $DEBUG = 0;
58 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 4 ▶use Hash::Util qw(lock_keys unlock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 4 ▶use Module::List qw(list_modules);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 4 ▶use Module::List::Pluggable qw( list_modules_under import_modules );
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 4 our $VERSION = '0.01';
66  
67 =item new
68  
69 Instantiates a new List::Filter::Dispatcher object.
70  
71 Takes an optional hashref as an argument, with named fields
72 identical to the names of the object attributes:
73  
74 =over
75  
76 =item plugin_root
77  
78 The location to look for the "plugins" that define the actual
79 "methods" that tasks are dispatched to.
80  
81 =item plugin_exceptions
82  
83 A list of modules in the plugin_root that will be ignored.
84  
85 Note: if you absolutely must use inheritence to create a variant
86 of an existing plugin, the original parent class should be
87 entered in this list to avoid namespace collisions.
88  
89 =back
90  
91 =cut
92  
93 # Note:
94 # "new" is inherited from Class::Base.
95 # It calls the following "init" routine automatically.
96  
97 =item init
98  
99 Initialize object attributes and then lock them down to prevent
100 accidental creation of new ones.
101  
102 =cut
103  
104 27 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
105 27 my $self = shift;
106 27 my $args = shift;
107 27 unlock_keys( %{ $self } );
108  
109 # define new attributes
110 my $attributes = {
111 plugin_root => $args->{ plugin_root },
112 27 };
113  
114 # add attributes to object
115 27 my @fields = (keys %{ $attributes });
116 27 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
117  
118 27 $self->do_require_of_plugins;
119  
120 27 lock_keys( %{ $self } );
121 27 return $self;
122 }
123  
124  
125 =item do_require_of_plugins
126  
127 An internally used routine that loads all of the subs defined in
128 all of the plugins/extensions found in perl's module namespace
129 at or under the "plugin_root" location.
130  
131 Returns: the number of sucessfully loaded plugin modules.
132  
133 =cut
134  
135 27 ▶sub do_require_of_plugins {
Subroutine do_require_of_plugins: called CC=1 SCAR=0.0
Pod documented
136 27 my $self = shift;
137 27 my $plugin_root = $self->plugin_root;
138 27 my $plugin_exceptions = $self->plugin_exceptions;
139  
140 # See Module::List::Pluggable
141 27 import_modules( $plugin_root, {
142 exceptions => $plugin_exceptions,
143 });
144 }
145  
146  
147 =item apply
148  
149 Applies the filter object, typically acting as a filter.
150  
151 Inputs:
152 (1) filter object (note: contains an array of patterns)
153 (2) aref of input items to be operated on
154 (3) an options hash reference:
155  
156 Supported option(s):
157  
158 "method" -- routine to use to apply filter to input items
159 (defaults to method specified inside the filter).
160  
161 Return:
162 aref of output items
163  
164 Note:
165 The options href is also passed through to the "method" routine.
166  
167 =cut
168  
169 1 ▶sub apply {
Subroutine apply: called CC=2 SCAR=7.1
Pod documented
170 1 my $self = shift;
171 1 my $filter = shift;
172 1 my $items = shift;
173 1 my $opt = shift;
174  
175 1 ▶ my $method = $opt->{ method } || $filter->method;
Condition33%1/3
$opt->{'method'} || $filter->method
l!l&&r!l&&!r
010
MC/DC0%0/2
$opt->{'method'} || $filter->method
$opt->{'method'} $filter->method
Truth table33%1/3
A || B
A= $opt->{'method'}B= $filter->method
ABresult
1X1
011
000
176  
177 1 my $output_aref = $self->$method( $filter, $items, $opt );
178 1 return $output_aref;
179 }
180  
181 =back
182  
183 =head2 accessors (setters and getters)
184  
185 Note: because of the oddities of the current architecture,
186 accessors must be provided for any fields needed by either the
187 Filter or the Transform routines, since those are imported into
188 the Dispatcher namespace, they become Dispatcher methods.
189  
190 I'm making an effort to document them here, for that reason
191 (though in general I think they should be avoided, period).
192  
193 =over
194  
195 =item plugin_root
196  
197 Getter for object attribute plugin_root
198  
199 =cut
200  
201 27 ▶sub plugin_root {
Subroutine plugin_root: called CC=1 SCAR=0.0
Pod documented
202 27 my $self = shift;
203 27 my $plugin_root = $self->{ plugin_root };
204 27 return $plugin_root;
205 }
206  
207 =item set_plugin_root
208  
209 Setter for object attribute set_plugin_root
210  
211 =cut
212  
213 0 ▶sub set_plugin_root {
Subroutine set_plugin_root: not called CC=1 SCAR=6.9
Pod documented
214 0 my $self = shift;
215 0 my $plugin_root = shift;
216 0 $self->{ plugin_root } = $plugin_root;
217 0 return $plugin_root;
218 }
219  
220  
221  
222 =item plugin_exceptions
223  
224 Getter for object attribute plugin_exceptions
225  
226 =cut
227  
228 27 ▶sub plugin_exceptions {
Subroutine plugin_exceptions: called CC=1 SCAR=0.0
Pod documented
229 27 my $self = shift;
230 27 my $plugin_exceptions = $self->{ plugin_exceptions };
231 27 return $plugin_exceptions;
232 }
233  
234 =item set_plugin_exceptions
235  
236 Setter for object attribute set_plugin_exceptions
237  
238 =cut
239  
240 0 ▶sub set_plugin_exceptions {
Subroutine set_plugin_exceptions: not called CC=1 SCAR=6.9
Pod documented
241 0 my $self = shift;
242 0 my $plugin_exceptions = shift;
243 0 $self->{ plugin_exceptions } = $plugin_exceptions;
244 0 return $plugin_exceptions;
245 }
246  
247  
248 4 1;
249  
250 =head1 SEE ALSO
251  
252 L<List::Filter>
253 L<Module::List::Pluggable>
254  
255 =head1 AUTHOR
256  
257 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>
258  
259 =head1 COPYRIGHT AND LICENSE
260  
261 Copyright (C) 2007 by Joseph Brenner
262  
263 This library is free software; you can redistribute it and/or modify
264 it under the same terms as Perl itself, either Perl version 5.8.2 or,
265 at your option, any later version of Perl 5 you may have available.
266  
267 =head1 BUGS
268  
269 None reported... yet.
270  
271 =cut
« Filter/Storage/MEM.pm ↑ Summary