Filter/Internal.pm

filter: Statementstmt 95.2% 81 / 85 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.4% 17 / 18 Podpod 71.4% 5 / 7 totaltotal 93.9% 109 / 116 CC 5 SCAR 6.7
CC
5
Cov
96%
qualify_storage_guts
5.0
set_default_stash
2.0
BEGIN
1.0
CRAP 2.0
« Filter.pm ↑ Summary Filter/StorageBase.pm »
1 package List::Filter::Internal;
2 2 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 List::Filter::Internal - internal methods for parameter qualification, etc.
7  
8 =head1 SYNOPSIS
9  
10 use List::Filter::Internal;
11 my $lfi = List::Filter::Internal->new( { default_stash => $default_stash } );
12 my $storage = $lfi->qualify_storage( $args->{ storage } );
13  
14 # alternate (preferred?), creates:
15 # $HOME/.list-filter/filters.yaml
16 my $lfi = List::Filter::Internal->new();
17 my $storage = $lfi->qualify_storage_from_namespace(
18 $args->{ storage },
19 'filter',
20 );
21  
22 =head1 DESCRIPTION
23  
24 A collection of miscellanious utility methods expected to be used
25 only internally by L<List::Filter> and it's relatives.
26  
27 The primary focus is on interface qualification routines that
28 need to be applied at various levels (since client code
29 might be written to access the system at any level), and hence
30 can't be part of any one class.
31  
32 =head2 METHODS
33  
34 =over
35  
36 =cut
37  
38 2 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 my $DEBUG = 0;
42 2 ▶use Carp qw(carp croak cluck confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use Hash::Util qw( lock_keys unlock_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Env qw( HOME );
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 2 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use File::Basename qw(fileparse basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use File::Copy qw(copy move);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 2 our $VERSION = '0.01';
52  
53 =item new
54  
55 Instantiates a new List::Filter object.
56  
57 Takes an optional hashref as an argument, with named fields
58 identical to the names of the object attributes.
59  
60 With no arguments, the newly created filter will be empty.
61  
62 =cut
63  
64 # Note:
65 # "new" is inherited from Class::Base.
66 # It calls the following "init" routine automatically.
67  
68 =item init
69  
70 Initialize object attributes and then lock them down to prevent
71 accidental creation of new ones.
72  
73 Note: there is no leading underscore on name "init", though it's
74 arguably an "internal" routine (i.e. not likely to be of use to
75 client code).
76  
77 =cut
78  
79 29 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
80 29 my $self = shift;
81 29 my $args = shift;
82 29 unlock_keys( %{ $self } );
83  
84 # define new attributes
85 my $attributes = {
86 ### fill-in name/value pairs of attributes here
87 # name => $args->{ name },
88 default_stash => $args->{ default_stash },
89  
90 29 };
91  
92 # add attributes to object
93 29 my @fields = (keys %{ $attributes });
94 29 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
95  
96 29 lock_keys( %{ $self } );
97 29 return $self;
98 }
99  
100  
101 =item qualify storage
102  
103 Qualifies the "storage" paramameter (used by L<List::Filter> and
104 it's relatives).
105  
106 Input: the storage argument (scalar or aref)
107  
108 Return: the qualified storage argument (aref)
109  
110 Note that this uses the object's "default_stash" argument (scalar)
111 as a fallback.
112  
113 =cut
114  
115 ### if $storage is an aref, then it should contain scalars and hrefs.
116 ### if it contains another aref, it should toss an error, or at least a
117 ### warning: that's an indication of a typo, e.g. "storage => [ $storage ]".
118  
119 6 ▶sub qualify_storage {
Subroutine qualify_storage: called CC=1 SCAR=0.0
Pod undocumented
120 6 my $self = shift;
121 6 my $storage = shift;
122 6 my $default_stash = $self->default_stash;
123  
124 6 $storage = qualify_storage_guts( $storage, $default_stash );
125  
126 5 return $storage;
127 }
128  
129 =item qualify_storage_from_namespace
130  
131 Qualifies the "storage" paramameter (used by L<List::Filter> and
132 it's relatives).
133  
134 Input: (1) the storage argument (scalar or aref)
135 (2) the "namespace" to use to generate a fall back yaml file (scalar).
136  
137 Return: the qualified storage argument (aref)
138  
139 =cut
140  
141  
142 31 ▶sub qualify_storage_from_namespace {
Subroutine qualify_storage_from_namespace: called CC=1 SCAR=0.0
Pod documented
143 31 my $self = shift;
144 31 my $storage = shift;
145 31 my $namespace = shift;
146  
147 31 my $default_stash = $self->define_yaml_default( $namespace );
148  
149 31 $storage = qualify_storage_guts( $storage, $default_stash );
150  
151 31 return $storage;
152 }
153  
154  
155 # qualify_storage_guts is an internally used sub (yes, in the
156 # Internal.pm module), which exists because if both of the front
157 # end methods call a sub, then "caller(1)" can point at the place
158 # where either were called. (If one method were to call the other,
159 # then it'd be a more complicated problem).
160 37 ▶sub qualify_storage_guts {
Subroutine qualify_storage_guts: called CC=5 SCAR=16.1
Pod undocumented
161 37 my $storage = shift;
162 37 my $default_stash = shift;
163 37 my ($package, $file, $line) = caller(1);
164  
165 37 ▶ if (not ( ref $storage eq 'ARRAY' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $storage eq 'ARRAY') { } 20 17 37
166 20 $storage = [ $storage ];
167 } else { # we've (probably) got an aref...
168 # But no arefs are allowed inside the main aref
169 17 foreach my $entry ( @{ $storage } ) {
170 18 ▶ if ( ref $entry eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry eq "ARRAY") 1 17 18
171 1 confess "The storage parameter should not be an aref inside an aref, in $file at line $line";
172 }
173 }
174 }
175  
176 # Make sure there's a defined entry in the aref, or use the default
177 36 ▶ if ( defined( $storage->[0] ) ) { # just the first one -- no point in going hogwild
Branch: 1 branch
BranchTrueFalseTotal
if (defined $storage->[0]) { } 31 5 36
178 31 $storage = $storage;
179 } else {
180 5 $storage = [ $default_stash ];
181 # # make sure the directory exists
182 # mkpath( dirname( $default_stash ) ) or croak "Could not create location for $default_stash: $!";
183 }
184 36 return $storage;
185 }
186  
187  
188  
189  
190 =item define_yaml_default
191  
192 Internally used routine defines the default filter storage location
193  
194 Input: basename (aka the namespace)
195 Output: default yaml file to use for storage
196 E.g. $HOME/.list-filter/<namespace>s.yaml
197  
198 (superceeds older define_storage_default in Handler.pm)
199  
200 =cut
201  
202 32 ▶sub define_yaml_default {
Subroutine define_yaml_default: called CC=1 SCAR=0.0
Pod documented
203 32 my $self = shift;
204 32 my $basename = shift;
205  
206 # Using plural for default filename
207 32 my $filename = $basename . 's'; # i18n? what's that?
208  
209 32 my $default = "$HOME/.list-filter/$filename.yaml";
210 32 mkpath( dirname($default) );
211  
212 # my $storage = [ $default ];
213 # $self->set_storage( $storage );
214 # $self->set_write_storage ( $default );
215 32 return $default;
216 }
217  
218 =item default_stash
219  
220 Getter for object attribute default_stash
221  
222 =cut
223  
224 6 ▶sub default_stash {
Subroutine default_stash: called CC=1 SCAR=0.0
Pod documented
225 6 my $self = shift;
226 6 my $default_stash = $self->{ default_stash };
227 6 return $default_stash;
228 }
229  
230 =item set_default_stash
231  
232 Setter for object attribute set_default_stash
233  
234 =cut
235  
236 0 ▶sub set_default_stash {
Subroutine set_default_stash: not called CC=1 SCAR=6.9
Pod documented
237 0 my $self = shift;
238 0 my $default_stash = shift;
239 0 $self->{ default_stash } = $default_stash;
240 0 return $default_stash;
241 }
242  
243  
244  
245  
246 2 1;
247  
248 =back
249  
250 =head1 SEE ALSO
251  
252 L<List::Filter>
253 L<List::Filter::Project>
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
272  
« Filter.pm ↑ Summary Filter/StorageBase.pm »