Toader/Entry/Helper.pm

filter: Statementstmt 14.9% 23 / 154 Branchbran 0.0% 0 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 46.6% 7 / 15 Podpod 100% 8 / 8 totaltotal 16.4% 38 / 231 CC 28 SCAR 34.1
CC
28
Cov
11%
generateEntryName
56.0
summary
56.0
entryExists
42.0
CRAP 30.4
« Toader/Page/Helper.pm ↑ Summary Toader/Render/CSS.pm »
1 package Toader::Entry::Helper;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Toader::isaToaderDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Toader::pathHelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Toader::Entry::Manage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Time::HiRes qw( gettimeofday );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Toader::Entry::Helper - Misc helper methods for entries.
14  
15 =head1 VERSION
16  
17 Version 1.0.0
18  
19 =cut
20  
21 7 our $VERSION = '1.0.0';
22  
23 =head1 METHODS
24  
25 =head2 new
26  
27 This initializes this object.
28  
29 On argument is required and it is a L<Toader> object.
30  
31 my $foo = Toader::Entry::Helper->new( $toader );
32 if($foo->error){
33 warn('error: '.$foo->error.":".$foo->errorString);
34 }
35  
36 =cut
37  
38 0 ▶sub new{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
39 0 my $toader=$_[1];
40  
41 0 my $self={
42 error=>undef,
43 errorString=>'',
44 perror=>undef,
45 directory=>undef,
46 regex=>'[12][90][0123456789][0123456789]'.
47 '[01][0123456789]'.
48 '[012][0123456789]-'.
49 '[012][0123456789]'.
50 '[012345][0123456789]'.
51 '[012345][0123456789].'.
52 '[0123456789]*',
53 errorExtra=>{
54 flags=>{
55 1=>'notAtoaderDir',
56 2=>'noDirSpecified',
57 3=>'noEntrySpecified',
58 4=>'noDirSet',
59 5=>'invalidEntryName',
60 6=>'entryManageErrored',
61 7=>'listEntriesErrored',
62 8=>'readEntryErrored',
63 9=>'noToaderObj',
64 10=>'notAtoaderObj',
65 },
66 },
67 };
68 0 bless $self;
69  
70 #if we have a Toader object, reel it in
71 0 ▶ if ( ! defined( $toader ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $toader) 0 0 0
72 0 $self->{perror}=1;
73 0 $self->{error}=9;
74 0 $self->{errorString}='No Toader object specified';
75 0 $self->warn;
76 0 return $self;
77 }
78 0 ▶ if ( ref( $toader ) ne "Toader" ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $toader ne "Toader") 0 0 0
79 0 $self->{perror}=1;
80 0 $self->{error}=10;
81 0 $self->{errorString}='The object specified is a "'.ref($toader).'"';
82 0 $self->warn;
83 0 return $self;
84 }
85 0 $self->{toader}=$toader;
86  
87 0 return $self;
88 }
89  
90 =head2 entryDirectory
91  
92 This returns the entry directory.
93  
94 This requires setDir to be called previously.
95  
96 If setDir has been successfully called, this will not error.
97  
98 my $entryDirectory=$foo->entryDirectory;
99 if($foo->error){
100 warn('error: '.$foo->error.":".$foo->errorString);
101 }
102  
103 =cut
104  
105 0 ▶sub entryDirectory{
Subroutine entryDirectory: not called CC=3 SCAR=24.8
Pod documented
106 0 my $self=$_[0];
107  
108 #blank any previous errors
109 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
110 0 return undef;
111 }
112  
113 #make sure setDir has been called with out issue
114 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
115 0 $self->{error}=4;
116 0 $self->{errorString}='No directory has been set yet';
117 0 $self->warn;
118 0 return undef;
119 }
120
121 0 return $self->{directory}.'/.toader/entries/';
122 }
123  
124 =head2 entryExists
125  
126 This checks if the specified helper exists.
127  
128 One argument is accepted and it is
129  
130 This requires setDir to be called previously.
131  
132 my $retruned=$foo->entryExists($entry);
133 if($foo->error){
134 warn('error: '.$foo->error.":".$foo->errorString);
135 }
136 if($returned){
137 print "It exists.\n";
138 }
139  
140 =cut
141  
142 0 ▶sub entryExists{
Subroutine entryExists: not called CC=6 SCAR=37.4
Pod documented
143 0 my $self=$_[0];
144 0 my $entry=$_[1];
145  
146 #blank any previous errors
147 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
148 0 return undef;
149 }
150  
151 #make sure setDir has been called with out issue
152 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
153 0 $self->{error}=4;
154 0 $self->{errorString}='No directory has been set yet';
155 0 $self->warn;
156 0 return undef;
157 }
158  
159 #make sure a entry is specified.
160 0 ▶ if (!defined($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $entry) 0 0 0
161 0 $self->{error}=0;
162 0 $self->{errorString}='No entry specified';
163 0 $self->warn;
164 0 return undef;
165 }
166  
167 #make sure we have a valid entry name.
168 0 my $returned=$self->validEntryName($entry);
169 0 ▶ if (!$returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($returned) 0 0 0
170 0 $self->{error}=5;
171 0 $self->{errorString}='The entry name is not valid';
172 0 $self->warn;
173 0 return undef;
174 }
175  
176 #check if it exists
177 0 ▶ if (-f $self->{directory}.'/.toader/entries/'.$entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"directory"} . '/.toader/entries/' . $entry) 0 0 0
178 0 return 1;
179 }
180  
181 0 return 0;
182 }
183  
184 =head2 generateEntryName
185  
186 This generates a entry name.
187  
188 my $entryName=$foo->generateEntryName;
189  
190 =cut
191  
192 0 ▶sub generateEntryName{
Subroutine generateEntryName: not called CC=7 SCAR=40.3
Pod documented
193 0 my $self=$_[0];
194  
195 #blank any previous errors
196 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
197 0 return undef;
198 }
199  
200 #gets the time
201 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
202 0 $year += 1900;
203  
204 #makes sure there are no single digit time items
205 0 ▶ if ($mon=~/^[0123456789]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon =~ /^[0123456789]$/) 0 0 0
206 0 $mon='0'.$mon;
207 }
208 0 ▶ if ($mday=~/^[0123456789]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mday =~ /^[0123456789]$/) 0 0 0
209 0 $mday='0'.$mday;
210 }
211 0 ▶ if ($hour=~/^[0123456789]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hour =~ /^[0123456789]$/) 0 0 0
212 0 $hour='0'.$hour;
213 }
214 0 ▶ if ($min=~/^[0123456789]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min =~ /^[0123456789]$/) 0 0 0
215 0 $min='0'.$min;
216 }
217 0 ▶ if ($sec=~/^[0123456789]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sec =~ /^[0123456789]$/) 0 0 0
218 0 $sec='0'.$sec;
219 }
220 0 $mon++;
221 0 my $hsec=gettimeofday;
222 0 $hsec=~s/.*\.//;
223  
224 #generate it and return it
225 0 return $year.$mon.$mday.'-'.$hour.$min.$sec.'.'.$hsec;
226 }
227  
228 =head2 setDir
229  
230 This sets the directory to operate on.
231  
232 One argument is required. It is the directory to use.
233  
234 $foo->setDir($directory);
235 if($foo->error){
236 warn('error: '.$foo->error.":".$foo->errorString);
237 }
238  
239 =cut
240  
241 0 ▶sub setDir{
Subroutine setDir: not called CC=4 SCAR=30.0
Pod documented
242 0 my $self=$_[0];
243 0 my $directory=$_[1];
244  
245 #blank any previous errors
246 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
247 0 return undef;
248 }
249  
250 #error if no directory is specified
251 0 ▶ if (!defined( $directory )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directory) 0 0 0
252 0 $self->{error}=2;
253 0 $self->{errorString}='No directory specified';
254 0 $self->warn;
255 0 return undef;
256 }
257  
258 #cleans up the naming
259 0 my $pathHelper=Toader::pathHelper->new( $directory );
260 0 $directory=$pathHelper->cleanup( $directory );
261  
262 #make sure it is a Toader directory
263 0 my $tdc=Toader::isaToaderDir->new;
264 0 my $isaToaderDir=$tdc->isaToaderDir($directory);
265 0 ▶ if ( ! $isaToaderDir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($isaToaderDir) 0 0 0
266 0 $self->{error}=1;
267 0 $self->{errorString}='Not a Toader directory according to Toader::isaToaderDir->isaToaderDir ';
268 0 $self->warn;
269 0 return undef;
270 }
271  
272 #save the directory
273 0 $self->{directory}=$directory;
274  
275 0 return 1;
276 }
277  
278 =head2 summary
279  
280 This builds a summary of the of the entries in the directory.
281  
282 =head3 returned hash
283  
284 The key values are the entry IDs. Each subhash then
285 contains the following keys.
286  
287 from
288 renderer
289 title
290 summary
291  
292 =cut
293  
294 0 ▶sub summary{
Subroutine summary: not called CC=7 SCAR=40.3
Pod documented
295 0 my $self=$_[0];
296  
297 #blank any previous errors
298 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
299 0 return undef;
300 }
301  
302 #make sure setDir has been called with out issue
303 0 ▶ if (!defined($self->{directory})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"directory"}) 0 0 0
304 0 $self->{error}=4;
305 0 $self->{errorString}='No directory has been set yet';
306 0 $self->warn;
307 0 return undef;
308 }
309  
310 #initalize Toader::Entry::Manage
311 0 my $emanage=Toader::Entry::Manage->new( $self->{toader} ) ;
312 0 $emanage->setDir( $self->{directory} );
313 0 ▶ if ( $emanage->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($emanage->error) 0 0 0
314 0 $self->{error}=6;
315 0 $self->{errorString}='Failed to initialize Toader::Entry::Manage. '.
316 'error="'.$emanage->error.'" errorString="'.$emanage->errorString.'"';
317 0 $self->warn;
318 0 return undef;
319 }
320  
321 0 my @entries=$emanage->list;
322 0 ▶ if ( $emanage->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($emanage->error) 0 0 0
323 0 $self->{error}=7;
324 0 $self->{errorString}='Failed to list the entries. error="'
325 .$emanage->error.'" errorString="'.$emanage->errorString.'"';
326 0 $self->warn;
327 0 return undef;
328 }
329  
330 0 my %summary;
331  
332 0 my $int=0;
333 0 ▶ while( defined( $entries[$int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entries[$int]) 0 0 0
334 0 my $entry=$emanage->read( $entries[$int] );
335 0 ▶ if( $emanage->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($emanage->error) 0 0 0
336 0 $self->{error}=8;
337 0 $self->{errorString}='Failed to read the entry "'.$entries[$int].'". error="'
338 .$emanage->error.'" errorString="'.$emanage->errorString.'"';
339 0 $self->warn;
340 0 return undef;
341 }
342  
343 0 $summary{$entries[$int]}={
344 from=>$entry->fromGet,
345 title=>$entry->titleGet,
346 renderer=>$entry->rendererGet,
347 summary=>$entry->summaryGet,
348 };
349
350 0 $int++;
351 }
352  
353 0 return %summary;
354 }
355  
356 =head2 validEntryName
357  
358 This verifies that the name is a valid file name.
359  
360 One arguemnet is taken and that is the name of the entry
361 name to check.
362  
363 This will not error. If the name is not defined, false, '0', will
364 be returned as undefined is not a valid name.
365  
366 my $valid=$foo->validEntryName($name);
367 if($valid){
368 print '"'.$name.'" is a valid name.';
369 }
370  
371 =cut
372  
373 0 ▶sub validEntryName{
Subroutine validEntryName: not called CC=4 SCAR=30.0
Pod documented
374 0 my $self=$_[0];
375 0 my $name=$_[1];
376  
377 #blank any previous errors
378 0 ▶ if (!$self->errorblank) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
379 0 return undef;
380 }
381  
382 0 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
383 0 return undef;
384 }
385  
386 0 ▶ if ($name =~ /$self->{regex}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$self->{"regex"}/) 0 0 0
387 0 return 1;
388 }
389
390 0 return 0;
391 }
392  
393 =head2 validEntryNameRegex
394  
395 This returns the regular expression for validating a entry name.
396  
397 This method does not call errorBlank for ease simplicity. This means
398 a error check should not be done on this message as if any error was
399 set previously then one will still be set.
400  
401 my $regex=$foo->validEntryNameRegex($name);
402  
403 =cut
404  
405 0 ▶sub validEntryNameRegex{
Subroutine validEntryNameRegex: not called CC=1 SCAR=6.9
Pod documented
406 0 return $_[0]->{regex};
407 }
408  
409 =head1 ERROR CODES
410  
411 =head2 1, notAtoaderDir
412  
413 Not a L<Toader> directory.
414  
415 =head2 2, noDirSpecified
416  
417 No directory specified.
418  
419 =head2 3, noEntrySpecified
420  
421 No entry specified.
422  
423 =head2 4, noDirSet
424  
425 No directory has been set yet.
426  
427 =head2 5, invalidEntryName
428  
429 The entry name is not valid.
430  
431 =head2 6, entryManageErrored
432  
433 Failed to initialize L<Toader::Entry::Manage>.
434  
435 =head2 7, listEntriesErrored
436  
437 Failed to list the entires.
438  
439 =head2 8, readEntryErrored
440  
441 Failed to read a entry.
442  
443 =head2 9, noToaderObj
444  
445 No L<Toader> object specified.
446  
447 =head2 10, notAtoaderObj
448  
449 The object specified is not a L<Toader> object.
450  
451 =head1 AUTHOR
452  
453 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
454  
455 =head1 BUGS
456  
457 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
458 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
459 automatically be notified of progress on your bug as I make changes.
460  
461 =head1 SUPPORT
462  
463 You can find documentation for this module with the perldoc command.
464  
465 perldoc Toader::Entry::Helper
466  
467  
468 You can also look for information at:
469  
470 =over 4
471  
472 =item * RT: CPAN's request tracker
473  
474 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
475  
476 =item * AnnoCPAN: Annotated CPAN documentation
477  
478 L<http://annocpan.org/dist/Toader>
479  
480 =item * CPAN Ratings
481  
482 L<http://cpanratings.perl.org/d/Toader>
483  
484 =item * Search CPAN
485  
486 L<http://search.cpan.org/dist/Toader/>
487  
488 =back
489  
490  
491 =head1 ACKNOWLEDGEMENTS
492  
493  
494 =head1 LICENSE AND COPYRIGHT
495  
496 Copyright 2011 Zane C. Bowers-Hadley.
497  
498 This program is free software; you can redistribute it and/or modify it
499 under the terms of either: the GNU General Public License as published
500 by the Free Software Foundation; or the Artistic License.
501  
502 See http://dev.perl.org/licenses/ for more information.
503  
504  
505 =cut
506  
507 7 1; # End of Toader
« Toader/Page/Helper.pm ↑ Summary Toader/Render/CSS.pm »