File Coverage

blib/lib/List/Filter/Internal.pm
Criterion Covered Total %
statement 79 83 95.1
branch 6 6 100.0
condition n/a
subroutine 17 18 94.4
pod 5 7 71.4
total 107 114 93.8


line stmt bran cond sub pod time code
1             package List::Filter::Internal;
2 2     2   59421 use base qw( Class::Base );
  2         5  
  2         1061  
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 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     2   1364 use 5.8.0;
  2         7  
  2         91  
39 2     2   11 use strict;
  2         3  
  2         60  
40 2     2   10 use warnings;
  2         4  
  2         92  
41             my $DEBUG = 0;
42 2     2   9 use Carp qw(carp croak cluck confess);
  2         3  
  2         171  
43 2     2   12 use Data::Dumper;
  2         4  
  2         140  
44 2     2   1124 use Hash::Util qw( lock_keys unlock_keys );
  2         2565  
  2         12  
45 2     2   1521 use Env qw( HOME );
  2         3574  
  2         14  
46              
47 2     2   401 use File::Path qw(mkpath);
  2         4  
  2         135  
48 2     2   21 use File::Basename qw(fileparse basename dirname);
  2         4  
  2         166  
49 2     2   907 use File::Copy qw(copy move);
  2         2890  
  2         1444  
50              
51             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             sub init {
80 29     29 1 2932 my $self = shift;
81 29         117 my $args = shift;
82 29         44 unlock_keys( %{ $self } );
  29         159  
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         250 };
91              
92             # add attributes to object
93 29         61 my @fields = (keys %{ $attributes });
  29         108  
94 29         53 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
  29         73  
  29         66  
95              
96 29         44 lock_keys( %{ $self } );
  29         113  
97 29         343 return $self;
98             }
99              
100              
101             =item qualify storage
102              
103             Qualifies the "storage" paramameter (used by L 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             sub qualify_storage {
120 6     6 0 3689 my $self = shift;
121 6         10 my $storage = shift;
122 6         15 my $default_stash = $self->default_stash;
123              
124 6         14 $storage = qualify_storage_guts( $storage, $default_stash );
125              
126 5         11 return $storage;
127             }
128              
129             =item qualify_storage_from_namespace
130              
131             Qualifies the "storage" paramameter (used by L 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             sub qualify_storage_from_namespace {
143 31     31 1 3034 my $self = shift;
144 31         46 my $storage = shift;
145 31         49 my $namespace = shift;
146              
147 31         93 my $default_stash = $self->define_yaml_default( $namespace );
148              
149 31         84 $storage = qualify_storage_guts( $storage, $default_stash );
150              
151 31         91 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             sub qualify_storage_guts {
161 37     37 0 58 my $storage = shift;
162 37         51 my $default_stash = shift;
163 37         306 my ($package, $file, $line) = caller(1);
164              
165 37 100       138 if (not ( ref $storage eq 'ARRAY' )) {
166 20         50 $storage = [ $storage ];
167             } else { # we've (probably) got an aref...
168             # But no arefs are allowed inside the main aref
169 17         32 foreach my $entry ( @{ $storage } ) {
  17         42  
170 18 100       74 if ( ref $entry eq 'ARRAY' ) {
171 1         238 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 100       115 if ( defined( $storage->[0] ) ) { # just the first one -- no point in going hogwild
178 31         44 $storage = $storage;
179             } else {
180 5         11 $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         84 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/s.yaml
197              
198             (superceeds older define_storage_default in Handler.pm)
199              
200             =cut
201              
202             sub define_yaml_default {
203 32     32 1 98 my $self = shift;
204 32         45 my $basename = shift;
205              
206             # Using plural for default filename
207 32         66 my $filename = $basename . 's'; # i18n? what's that?
208              
209 32         167 my $default = "$HOME/.list-filter/$filename.yaml";
210 32         3022 mkpath( dirname($default) );
211              
212             # my $storage = [ $default ];
213             # $self->set_storage( $storage );
214             # $self->set_write_storage ( $default );
215 32         98 return $default;
216             }
217              
218             =item default_stash
219              
220             Getter for object attribute default_stash
221              
222             =cut
223              
224             sub default_stash {
225 6     6 1 8 my $self = shift;
226 6         11 my $default_stash = $self->{ default_stash };
227 6         10 return $default_stash;
228             }
229              
230             =item set_default_stash
231              
232             Setter for object attribute set_default_stash
233              
234             =cut
235              
236             sub set_default_stash {
237 0     0 1   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             1;
247              
248             =back
249              
250             =head1 SEE ALSO
251              
252             L
253             L
254              
255             =head1 AUTHOR
256              
257             Joseph Brenner, Edoom@kzsu.stanford.eduE
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