Toader/Page/Helper.pm

filter: Statementstmt 15.7% 20 / 127 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 100% 6 / 6 totaltotal 16.5% 32 / 193 CC 25 SCAR 36.4
CC
25
Cov
11%
validPageName
90.0
pageExists
42.0
summary
30.0
CRAP 38.1
« Toader/Directory.pm ↑ Summary Toader/Entry/Helper.pm »
1 package Toader::Page::Helper;
2  
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Toader::isaToaderDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Toader::pathHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Toader::Page::Manage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Toader::Page::Helper - Misc helper methods for pages.
13  
14 =head1 VERSION
15  
16 Version 1.0.0
17  
18 =cut
19  
20 5 our $VERSION = '1.0.0';
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 This initializes this object.
27  
28 This method will not error.
29  
30 my $foo = Toader::Page::FileHelper->new();
31  
32 =cut
33  
34 0 ▶sub new{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
35 0 my $toader=$_[1];
36  
37 0 my $self={
38 error=>undef,
39 errorString=>'',
40 perror=>undef,
41 directory=>undef,
42 errorExtra=>{
43 flags=>{
44 1=>'notAtoaderDir',
45 2=>'noDirSpecified',
46 3=>'noPageSpecified',
47 4=>'noDirSet',
48 5=>'invalidPageName',
49 6=>'pageManageErrored',
50 7=>'noToaderObj',
51 8=>'notAtoaderObj',
52 },
53 },
54 };
55 0 bless $self;
56  
57 #if we have a Toader object, reel it in
58 0 ▶ if ( ! defined( $toader ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $toader) 0 0 0
59 0 $self->{perror}=1;
60 0 $self->{error}=7;
61 0 $self->{errorString}='No Toader object specified';
62 0 $self->warn;
63 0 return $self;
64 }
65 0 ▶ if ( ref( $toader ) ne "Toader" ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $toader ne "Toader") 0 0 0
66 0 $self->{perror}=1;
67 0 $self->{error}=8;
68 0 $self->{errorString}='The object specified is a "'.ref($toader).'"';
69 0 $self->warn;
70 0 return $self;
71 }
72 0 $self->{toader}=$toader;
73  
74 0 return $self;
75 }
76  
77 =head2 pageDirectory
78  
79 This returns the page directory.
80  
81 This requires setDir to be called previously.
82  
83 If setDir has been successfully called, this will not error.
84  
85 my $pageDirectory=$foo->pageDirectory;
86 if($foo->error){
87 warn('error: '.$foo->error.":".$foo->errorString);
88 }
89  
90 =cut
91  
92 0 ▶sub pageDirectory{
Subroutine pageDirectory: not called CC=3 SCAR=24.8
Pod documented
93 0 my $self=$_[0];
94  
95 #blank any previous errors
96 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
97 0 return undef;
98 }
99  
100 #make sure setDir has been called with out issue
101 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
102 0 $self->{error}=4;
103 0 $self->{errorString}='No directory has been set yet';
104 0 $self->warn;
105 0 return undef;
106 }
107
108 0 return $self->{directory}.'/.toader/pages/';
109 }
110  
111 =head2 pageExists
112  
113 This checks if the specified page exists.
114  
115 One argument is accepted and it is the page in question.
116  
117 This requires setDir to be called previously.
118  
119 my $retruned=$foo->pageExists($page);
120 if($foo->error){
121 warn('error: '.$foo->error.":".$foo->errorString);
122 }
123 if($returned){
124 print "It exists.\n";
125 }
126  
127 =cut
128  
129 0 ▶sub pageExists{
Subroutine pageExists: not called CC=6 SCAR=37.4
Pod documented
130 0 my $self=$_[0];
131 0 my $page=$_[1];
132  
133 #blank any previous errors
134 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
135 0 return undef;
136 }
137  
138 #make sure setDir has been called with out issue
139 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
140 0 $self->{error}=4;
141 0 $self->{errorString}='No directory has been set yet';
142 0 $self->warn;
143 0 return undef;
144 }
145  
146 #make sure a page is specified.
147 0 ▶ if (!defined($page)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $page) 0 0 0
148 0 $self->{error}=0;
149 0 $self->{errorString}='No page specified';
150 0 $self->warn;
151 0 return undef;
152 }
153  
154 #make sure we have a valid page name.
155 0 my $returned=$self->validPageName($page);
156 0 ▶ if (!$returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($returned) 0 0 0
157 0 $self->{error}=5;
158 0 $self->{errorString}='The page name is not valid';
159 0 $self->warn;
160 0 return undef;
161 }
162
163 #check if it exists
164 0 ▶ if (-f $self->{directory}.'/.toader/pages/'.$page ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"directory"} . '/.toader/pages/' . $page) 0 0 0
165 0 return 1;
166 }
167  
168 0 return 0;
169 }
170  
171 =head2 setDir
172  
173 This sets the directory to operate on.
174  
175 One argument is required. It is the directory to use.
176  
177 $foo->setDir($directory);
178 if($foo->error){
179 warn('error: '.$foo->error.":".$foo->errorString);
180 }
181  
182 =cut
183  
184 0 ▶sub setDir{
Subroutine setDir: not called CC=4 SCAR=30.0
Pod documented
185 0 my $self=$_[0];
186 0 my $directory=$_[1];
187  
188 #blank any previous errors
189 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
190 0 return undef;
191 }
192  
193 #error if no directory is specified
194 0 ▶ if (!defined( $directory )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directory) 0 0 0
195 0 $self->{error}=2;
196 0 $self->{errorString}='No directory specified';
197 0 $self->warn;
198 0 return undef;
199 }
200  
201 #cleans up the naming
202 0 my $pathHelper=Toader::pathHelper->new( $directory );
203 0 $directory=$pathHelper->cleanup( $directory );
204  
205  
206 #make sure it is a Toader directory
207 0 my $tdc=Toader::isaToaderDir->new;
208 0 my $isaToaderDir=$tdc->isaToaderDir($directory);
209 0 ▶ if ( ! $isaToaderDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isaToaderDir) 0 0 0
210 0 $self->{error}=1;
211 0 $self->{errorString}='Not a Toader directory according to Toader::isaToaderDir->isaToaderDir ';
212 0 $self->warn;
213 0 return undef;
214 }
215  
216 #save the directory
217 0 $self->{directory}=$directory;
218  
219 0 return 1;
220 }
221  
222 =head2 summary
223  
224 This builds a summary of of the the pages.
225  
226  
227  
228 =cut
229  
230 0 ▶sub summary{
Subroutine summary: not called CC=5 SCAR=34.0
Pod documented
231 0 my $self=$_[0];
232  
233 #blank any previous errors
234 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
235 0 return undef;
236 }
237  
238 #make sure setDir has been called with out issue
239 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
240 0 $self->{error}=4;
241 0 $self->{errorString}='No directory has been set yet';
242 0 $self->warn;
243 0 return undef;
244 }
245
246 #initalize Toader::Entry::Manage
247 0 my $pmanage=Toader::Page::Manage->new;
248 0 $pmanage->setDir( $self->{directory} );
249 0 ▶ if ( $pmanage->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($pmanage->error) 0 0 0
250 0 $self->{error}=6;
251 0 $self->{errorString}='Failed to initialize Toader::Page::Manage. '.
252 'error="'.$pmanage->error.'" errorString="'.$pmanage->errorString.'"';
253 0 $self->warn;
254 0 return undef;
255 }
256
257 0 my @pages=$pmanage->list;
258 0 my $int=0;
259 0 ▶ while ( defined[$int] ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined [$int]) 0 0 0
260
261  
262 0 $int++;
263 }
264  
265 }
266  
267 =head2 validPageName
268  
269 This verifies that the name is a valid file name.
270  
271 One arguemnet is taken and that is the name of the page
272 name to check.
273  
274 This will not error. If the name is not defined, false, '0', will
275 be returned as undefined is not a valid name.
276  
277 my $valid=$foo->validPageName($name);
278 if($valid){
279 print '"'.$name.'" is a valid name.';
280 }
281  
282 =cut
283  
284 0 ▶sub validPageName{
Subroutine validPageName: not called CC=9 SCAR=45.0
Pod documented
285 0 my $self=$_[0];
286 0 my $name=$_[1];
287  
288 #blank any previous errors
289 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
290 0 return undef;
291 }
292  
293 0 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
294 0 return undef;
295 }
296  
297 #checks for invalid characters
298 0 ▶ if ($name =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\n/) 0 0 0
299 0 return 0;
300 }
301 0 ▶ if ($name =~ /\t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\t/) 0 0 0
302 0 return 0;
303 }
304 0 ▶ if ($name =~ /\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/]) 0 0 0
305 0 return 0;
306 }
307 0 ▶ if ($name =~ /^ /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^ /) 0 0 0
308 0 return 0;
309 }
310 0 ▶ if ($name =~ / $/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ / $/) 0 0 0
311 0 return 0;
312 }
313 0 ▶ if ($name =~ /^\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\./) 0 0 0
314 0 return 0;
315 }
316
317 0 return 1;
318 }
319  
320 =head1 ERROR CODES
321  
322 =head2 1, notAtoaderDir
323  
324 Not a L<Toader> directory.
325  
326 =head2 2, noDirSpecified
327  
328 No directory specified.
329  
330 =head2 3, noPageSpecified
331  
332 No page specified.
333  
334 =head2 4, noDirSet
335  
336 No directory has been set yet.
337  
338 =head2 5, invalidPageName
339  
340 The page name is not valid.
341  
342 =head2 6, pageManageErrored
343  
344 Failed to initialize L<Toader::Page::Manage>.
345  
346 =head2 7, noToaderObj
347  
348 No L<Toader> object specified.
349  
350 =head2 8, notAtoaderObj
351  
352 The object specified is not a L<Toader> object.
353  
354  
355 =head1 AUTHOR
356  
357 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
358  
359 =head1 BUGS
360  
361 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
362 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
363 automatically be notified of progress on your bug as I make changes.
364  
365  
366  
367  
368 =head1 SUPPORT
369  
370 You can find documentation for this module with the perldoc command.
371  
372 perldoc Toader::Page::Helper
373  
374  
375 You can also look for information at:
376  
377 =over 4
378  
379 =item * RT: CPAN's request tracker
380  
381 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
382  
383 =item * AnnoCPAN: Annotated CPAN documentation
384  
385 L<http://annocpan.org/dist/Toader>
386  
387 =item * CPAN Ratings
388  
389 L<http://cpanratings.perl.org/d/Toader>
390  
391 =item * Search CPAN
392  
393 L<http://search.cpan.org/dist/Toader/>
394  
395 =back
396  
397  
398 =head1 ACKNOWLEDGEMENTS
399  
400  
401 =head1 LICENSE AND COPYRIGHT
402  
403 Copyright 2011 Zane C. Bowers-Hadley.
404  
405 This program is free software; you can redistribute it and/or modify it
406 under the terms of either: the GNU General Public License as published
407 by the Free Software Foundation; or the Artistic License.
408  
409 See http://dev.perl.org/licenses/ for more information.
410  
411  
412 =cut
413  
414 5 1; # End of Toader::Page::Helper
« Toader/Directory.pm ↑ Summary Toader/Entry/Helper.pm »