Toader/pathHelper.pm

filter: Statementstmt 12.7% 23 / 181 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.6% 7 / 15 Podpod 100% 8 / 8 totaltotal 14.2% 38 / 267 CC 31 SCAR 33.5
CC
31
Cov
10%
relative
72.0
back2root
30.0
relative2root
30.0
CRAP 28.5
« Toader/Render/CSS.pm ↑ Summary Toader/isaToaderDir.pm »
1 package Toader::pathHelper;
2  
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Toader::isaToaderDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Toader::findToaderRoot;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Toader::pathHelper - Various path related helpers.
14  
15 =head1 VERSION
16  
17 Version 0.1.0
18  
19 =cut
20  
21 16 our $VERSION = '0.1.0';
22  
23 =head1 SYNOPSIS
24  
25 use Toader::pathHelper;
26  
27 my $foo = Toader::pathHelper->new;
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 This initiates the object.
34  
35 One argument is taken. That is the a L<Toader> directory.
36  
37 my $foo=Toader::pathHelper->new($toaderDir);
38 if($foo->error){
39 warn('error: '.$foo->error.":".$foo->errorString);
40 }
41  
42 =cut
43  
44 0 ▶sub new{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
45 0 my $dir=$_[1];
46  
47 0 my $self={
48 error=>undef,
49 errorString=>'',
50 isatd=>Toader::isaToaderDir->new,
51 perror=>undef,
52 errorExtra=>{
53 flags=>{
54 1=>'noDirSpecified',
55 2=>'notAtoaderDir',
56 3=>'otherToaderDir',
57 4=>'notAbleToFindRoot',
58 5=>'notUnderToaderRoot',
59 },
60 },
61 };
62 0 bless $self;
63  
64 #make sure a directory has been specified
65 0 ▶ if (!defined($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
66 0 $self->{error}=1;
67 0 $self->{errorString}='No directory specified';
68 0 $self->{perror}=1;
69 0 $self->warn;
70 0 return undef;
71 }
72  
73 0 my $returned=$self->{isatd}->isaToaderDir($dir);
74 0 ▶ if (! $returned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($returned) 0 0 0
75 0 $self->{error}=2;
76 0 $self->{errorString}='"'.$dir.'" is not a Toader directory';
77 0 $self->{perror}=1;
78 0 $self->warn;
79 0 return undef;
80 }
81  
82 #finds the root
83 0 my $findroot = Toader::findToaderRoot->new;
84 0 my $root=$findroot->findToaderRoot($dir);
85 0 ▶ if($findroot->error){
Branch: 1 branch
BranchTrueFalseTotal
if ($findroot->error) 0 0 0
86 0 $self->{error}=4;
87 0 $self->{errorString}='Could not find the root for "'.$dir.'" '.
88 'error="'.$findroot->error.'" '.
89 'errorString="'.$findroot->errorString.'"';
90 0 $self->warn;
91 0 return undef;
92 }
93 0 $self->cleanup($root);
94 0 $self->{root}=$root;
95  
96 0 return $self;
97 }
98  
99 =head2 atRoot
100  
101 This checks if a directory is the root or not.
102  
103 =cut
104  
105 0 ▶sub atRoot{
Subroutine atRoot: not called CC=4 SCAR=30.0
Pod documented
106 0 my $self=$_[0];
107 0 my $dir=$_[1];
108  
109 0 ▶ if( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
110 0 return undef;
111 }
112  
113 #make sure a directory has been specified
114 0 ▶ if ( ! defined( $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
115 0 $self->{error}=1;
116 0 $self->{errorString}='No directory specified';
117 0 $self->warn;
118 0 return undef;
119 }
120  
121 #cleans up the directory
122 0 $dir=$self->cleanup($dir);
123  
124 #check if it is the same
125 0 ▶ if ( $self->{root} eq $dir ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"root"} eq $dir) 0 0 0
126 0 return 1;
127 }
128  
129 0 return 0;
130 }
131  
132 =head2 back2root
133  
134 This returns relative path from specified directory,
135 back to the L<Toader> root directory.
136  
137 One argument is taken and that is the Toader directory
138 under the root L<Toader> directory.
139  
140 =cut
141  
142 0 ▶sub back2root{
Subroutine back2root: not called CC=5 SCAR=34.0
Pod documented
143 0 my $self=$_[0];
144 0 my $dir=$_[1];
145  
146 0 ▶ if( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
147 0 return undef;
148 }
149  
150 #make sure a directory has been specified
151 0 ▶ if ( ! defined( $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
152 0 $self->{error}=1;
153 0 $self->{errorString}='No directory specified';
154 0 $self->warn;
155 0 return undef;
156 }
157  
158 #make sure it is a Toader dir
159 0 ▶ if (! $self->{isatd}->isaToaderDir($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"isatd"}->isaToaderDir($dir)) 0 0 0
160 0 $self->{error}=2;
161 0 $self->{errorString}='"'.$dir.'" is not a Toader directory';
162 0 $self->warn;
163 0 return undef;
164 }
165  
166 #makes sure they are both under the root directory
167 0 ▶ if( ! $self->underRoot( $dir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->underRoot($dir)) 0 0 0
168 0 $self->{error}=5;
169 $self->{errorString}='"'.$dir.'" is not under the root Toader directory "'.
170 0 $self->{root}.'",';
171 0 return undef;
172 }
173  
174 0 return $self->relative( $dir, $self->{root} );
175 }
176  
177 =head2 cleanup
178  
179 This cleans up the path for a L<Toader> directory.
180  
181 my $cleandir=$foo->cleanup($dir);
182 if($foo->error){
183 warn('Error:'.$foo->error.': '.$foo->errorString);
184 }
185  
186 =cut
187  
188 0 ▶sub cleanup{
Subroutine cleanup: not called CC=4 SCAR=30.0
Pod documented
189 0 my $self=$_[0];
190 0 my $dir=$_[1];
191  
192 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
193 0 return undef;
194 }
195  
196 #make sure a directory has been specified
197 0 ▶ if (!defined($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
198 0 $self->{error}=1;
199 0 $self->{errorString}='No directory specified';
200 0 $self->warn;
201 0 return undef;
202 }
203  
204 0 my $returned=$self->{isatd}->isaToaderDir($dir);
205 0 ▶ if (! $returned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($returned) 0 0 0
206 0 $self->{error}=2;
207 0 $self->{errorString}='"'.$dir.'" is not a Toader directory';
208 0 $self->warn;
209 0 return undef;
210 }
211  
212 #cleanup the path
213 0 $dir=File::Spec->canonpath( $dir ) ;
214 0 $dir=abs_path( $dir );
215  
216 0 return $dir;
217 }
218  
219 =head2 relative
220  
221 This finds the relative path between two toader directories.
222  
223 Two arguments are accepted. Both are L<Toader> directories. The first
224 one is the directory to start in and the second is the directory
225 to end in.
226  
227 my $relativePath=$foo->relative($fromDir, $toDir);
228 if($foo->error){
229 warn('Error:'.$foo->error.': '.$foo->errorString);
230 }
231  
232 =cut
233  
234 0 ▶sub relative{
Subroutine relative: not called CC=8 SCAR=42.8
Pod documented
235 0 my $self=$_[0];
236 0 my $dir0=$_[1];
237 0 my $dir1=$_[2];
238  
239 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
240 0 return undef;
241 }
242  
243 #make sure a directory has been specified
244 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir0 and defined $dir1) 0 0 0
Condition0%0/3
defined $dir0 and defined $dir1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir0 and defined $dir1
defined $dir0 defined $dir1
Truth table0%0/3
A and B
A= defined $dir0B= defined $dir1
ABresult
0X0
100
111
245 (!defined($dir0)) ||
246 (!defined($dir1))
247 ) {
248 0 $self->{error}=1;
249 0 $self->{errorString}='No directory specified';
250 0 $self->warn;
251 0 return undef;
252 }
253  
254 #make sure both directories are Toader directories
255 0 ▶ if (! $self->{isatd}->isaToaderDir($dir0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"isatd"}->isaToaderDir($dir0)) 0 0 0
256 0 $self->{error}=2;
257 0 $self->{errorString}='"'.$dir0.'" is not a Toader directory';
258 0 $self->warn;
259 0 return undef;
260 }
261 0 ▶ if (! $self->{isatd}->isaToaderDir($dir1) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"isatd"}->isaToaderDir($dir1)) 0 0 0
262 0 $self->{error}=2;
263 0 $self->{errorString}='"'.$dir1.'" is not a Toader directory';
264 0 $self->warn;
265 0 return undef;
266 }
267  
268 #makes sure they are both under the root directory
269 0 ▶ if( ! $self->underRoot( $dir0 ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->underRoot($dir0)) 0 0 0
270 0 $self->{error}=5;
271 $self->{errorString}='"'.$dir0.'" is not under the root Toader directory "'.
272 0 $self->{root}.'",';
273 0 return undef;
274 }
275 0 ▶ if( ! $self->underRoot( $dir1 ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->underRoot($dir1)) 0 0 0
276 0 $self->{error}=5;
277 $self->{errorString}='"'.$dir1.'" is not under the root Toader directory "'.
278 0 $self->{root}.'",';
279 0 return undef;
280 }
281  
282 0 $dir0=$self->cleanup($dir0);
283 0 $dir1=$self->cleanup($dir1);
284  
285 0 return File::Spec->abs2rel( $dir1, $dir0 );
286 }
287  
288 =head2 relative2root
289  
290 This returns relative path from the root L<Toader> directory.
291  
292 One argument is taken and that is the L<Toader> directory
293 under the root L<Toader> directory.
294  
295 =cut
296  
297 0 ▶sub relative2root{
Subroutine relative2root: not called CC=5 SCAR=34.0
Pod documented
298 0 my $self=$_[0];
299 0 my $dir=$_[1];
300  
301 0 ▶ if( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
302 0 return undef;
303 }
304  
305 #make sure a directory has been specified
306 0 ▶ if ( ! defined( $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
307 0 $self->{error}=1;
308 0 $self->{errorString}='No directory specified';
309 0 $self->warn;
310 0 return undef;
311 }
312  
313 #make sure it is a Toader dir
314 0 ▶ if (! $self->{isatd}->isaToaderDir($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"isatd"}->isaToaderDir($dir)) 0 0 0
315 0 $self->{error}=2;
316 0 $self->{errorString}='"'.$dir.'" is not a Toader directory';
317 0 $self->warn;
318 0 return undef;
319 }
320  
321 #makes sure they are both under the root directory
322 0 ▶ if( ! $self->underRoot( $dir ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->underRoot($dir)) 0 0 0
323 0 $self->{error}=5;
324 $self->{errorString}='"'.$dir.'" is not under the root Toader directory "'.
325 0 $self->{root}.'",';
326 0 return undef;
327 }
328  
329 0 return $self->relative( $self->{root}, $dir );
330 }
331  
332 =head2 underRoot
333  
334 This checks if a specified L<Toader> directory is under the L<Toader> root
335 directory.
336  
337 One argument is taken and that is a directory. This directory must be a L<Toader>
338 directory.
339  
340 The returned value is a boolean value.
341  
342 my $return=$self->underRoot($dir);
343 if($foo->error){
344 warn('Error:'.$foo->error.': '.$foo->errorString);
345 }
346  
347 =cut
348  
349 0 ▶sub underRoot{
Subroutine underRoot: not called CC=4 SCAR=30.0
Pod documented
350 0 my $self=$_[0];
351 0 my $dir=$_[1];
352  
353 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
354 0 return undef;
355 }
356  
357 #make sure a directory has been specified
358 0 ▶ if ( !defined($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
359 0 $self->{error}=1;
360 0 $self->{errorString}='No directory specified';
361 0 $self->warn;
362 0 return undef;
363 }
364  
365 #make sure both directories are Toader directories
366 0 my $returned=$self->{isatd}->isaToaderDir($dir);
367 0 ▶ if (! $returned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($returned) 0 0 0
368 0 $self->{error}=2;
369 0 $self->{errorString}='"'.$dir.'" is not a Toader directory';
370 0 $self->warn;
371 0 return undef;
372 }
373  
374 0 return $self->underRootNT( $dir );
375 }
376  
377 =head2 underRootNT
378  
379 This checks if a specified directory is under the L<Toader> root
380 directory. Unlike underRoot, no check is done on if it is a
381 Toader directory or not.
382  
383 One argument is taken and that is a directory.
384  
385 The returned value is a boolean value.
386  
387 This does not check if it exists or not.
388  
389 my $return=$self->underRootNT($dir);
390 if($foo->error){
391 warn('Error:'.$foo->error.': '.$foo->errorString);
392 }
393  
394 =cut
395  
396 0 ▶sub underRootNT{
Subroutine underRootNT: not called CC=4 SCAR=30.0
Pod documented
397 0 my $self=$_[0];
398 0 my $dir=$_[1];
399  
400 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
401 0 return undef;
402 }
403  
404 #make sure a directory has been specified
405 0 ▶ if ( !defined($dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 0 0
406 0 $self->{error}=1;
407 0 $self->{errorString}='No directory specified';
408 0 $self->warn;
409 0 return undef;
410 }
411  
412 #cleans up the directory
413 0 $dir=$self->cleanup($dir);
414 0 $dir=$dir.'/'; #done to make sure other things are not matched... if it is the same as the root
415  
416 #add / to the root to make sure other items are not matched and make it into a regexp
417 0 my $root=$self->{root}.'/';
418 0 $root='^'.quotemeta($root);
419  
420 0 ▶ if( $dir =~ /$root/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /$root/) 0 0 0
421 0 return 1;
422 }
423
424 0 return 0;
425 }
426  
427 =head1 ERROR CODES
428  
429 =head2 1, noDirSpecified
430  
431 No directory specified.
432  
433 =head2 2, notAtoaderDir
434  
435 The directory is not a L<Toader> directory.
436  
437 =head2 3, otherToaderDir
438  
439 The L<Toader> directory in question is not under the L<Toader>
440 directory root it was initialized with.
441  
442 =head2 4, notAbleToFindRoot
443  
444 Unable to find the root L<Toader> directory.
445  
446 =head2 5, notUnderToaderRoot
447  
448 The directory is not under the root L<Toader> directory.
449  
450 =head1 AUTHOR
451  
452 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
453  
454 =head1 BUGS
455  
456 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
457 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
458 automatically be notified of progress on your bug as I make changes.
459  
460  
461  
462  
463 =head1 SUPPORT
464  
465 You can find documentation for this module with the perldoc command.
466  
467 perldoc Toader::Render
468  
469  
470 You can also look for information at:
471  
472 =over 4
473  
474 =item * RT: CPAN's request tracker
475  
476 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
477  
478 =item * AnnoCPAN: Annotated CPAN documentation
479  
480 L<http://annocpan.org/dist/Toader>
481  
482 =item * CPAN Ratings
483  
484 L<http://cpanratings.perl.org/d/Toader>
485  
486 =item * Search CPAN
487  
488 L<http://search.cpan.org/dist/Toader/>
489  
490 =back
491  
492  
493 =head1 ACKNOWLEDGEMENTS
494  
495  
496 =head1 LICENSE AND COPYRIGHT
497  
498 Copyright 2011. Zane C. Bowers-Hadley.
499  
500 This program is free software; you can redistribute it and/or modify it
501 under the terms of either: the GNU General Public License as published
502 by the Free Software Foundation; or the Artistic License.
503  
504 See http://dev.perl.org/licenses/ for more information.
505  
506  
507 =cut
508  
509 16 1; # End of Toader
« Toader/Render/CSS.pm ↑ Summary Toader/isaToaderDir.pm »