Toader/Config.pm

filter: Statementstmt 8.7% 17 / 194 Branchbran 0.0% 0 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.7% 5 / 14 Podpod 100% 9 / 9 totaltotal 10.5% 31 / 295 SCAR 72.1
CC
40
Cov
6%
new
156.0
valueSet
56.0
write
56.0
CRAP 1358.4
« Toader/Render/Directory.pm ↑ Summary Toader/Render/AutoDoc.pm »
1 package Toader::Config;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Toader::Config - Represents the Toader config.
12  
13 =head1 VERSION
14  
15 Version 1.0.0
16  
17 =cut
18  
19 1 our $VERSION = '1.0.0';
20  
21 =head1 METHODS
22  
23 =head2 new
24  
25 This initiates the object.
26  
27 One argument is required and it is a Toader object.
28  
29 my $foo = Toader::Config->new( $toader );
30 if ( $foo->error ){
31 warn('error:'.$foo->error.': '.$foo->errorString);
32 }
33  
34 =cut
35  
36 sub new{
37 0 ▶ my $toader=$_[1];
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
38  
39 0 my $self={
40 error=>undef,
41 errorString=>'',
42 perror=>undef,
43 errorExtra=>{
44 flags=>{
45 1=>'noToaderObj',
46 2=>'notAtoaderObj',
47 3=>'noDirSet',
48 4=>'configReadFailed',
49 5=>'sectionDoesNotExist',
50 6=>'noVariableSpecified',
51 7=>'invalidVariableOrSection',
52 8=>'configWriteFailed',
53 9=>'getVCSerrored',
54 10=>'VCSusableErrored',
55 11=>'underVCSerrored',
56 12=>'VCSaddErrored',
57 },
58 },
59 VCSusable=>0,
60 };
61 0 bless $self;
62  
63 #make sure a Toader object is specified
64 0 ▶ if ( ! defined( $toader ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $toader) 0 0 0
65 0 $self->{perror}=1;
66 0 $self->{error}=1;
67 0 $self->{errorString}='No toader object specified';
68 0 $self->warn;
69 0 return $self;
70 }
71  
72 #make sure it is a Toader object
73 0 ▶ if ( ref( $toader ) ne 'Toader' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $toader ne "Toader") 0 0 0
74 0 $self->{perror}=1;
75 0 $self->{error}=2;
76 0 $self->{errorString}='The passed object is "'.ref( $toader ).'" and not a Toader object';
77 0 $self->warn;
78 0 return $self;
79 }
80  
81 #saves the Toader object
82 0 $self->{toader}=$toader;
83  
84 #gets the directory
85 0 $self->{dir}=$self->{toader}->getRootDir;
86 0 ▶ if ( ! defined( $self->{dir} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"dir"}) 0 0 0
87 0 $self->{perror}=1;
88 0 $self->{error}=3;
89 0 $self->{errorString}='The Toader object did not return a directory';
90 0 $self->warn;
91 0 return $self;
92 }
93  
94 #this handles the toader config file
95 0 $self->{configFile}=$self->{dir}.'/.toader/config.ini';
96 0 ▶ if ( -f $self->{configFile} ){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{'configFile'}) { } 0 0 0
97 0 $self->{config}=Config::Tiny->read( $self->{configFile} );
98 0 ▶ if ( ! defined( $self->{config} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}) 0 0 0
99 0 $self->{perror}='1';
100 0 $self->{error}=4;
101 $self->{errorString}='Unable to read the config file, "'.
102 0 $self->{configFile}.'",';
103 0 $self->warn;
104 0 return $self;
105 }
106 }else{
107 0 $self->{config}=Config::Tiny->new;
108 }
109  
110 #sets some defaults for the config if they are not set
111 #default to the site name being the hostname
112 0 ▶ if ( ! defined( $self->{config}->{_}->{site} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"_"}{"site"}) 0 0 0
113 0 $self->{config}->{_}->{site}=hostname;
114 }
115 #sets the owner if one is not specified
116 0 ▶ if ( ! defined( $self->{config}->{_}->{'owner'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"_"}{"owner"}) 0 0 0
117 0 $self->{config}->{_}->{'owner'}=getlogin.'@'.hostname;
118 }
119 #sets the default last
120 0 ▶ if ( ! defined( $self->{config}->{_}->{'last'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"_"}{"last"}) 0 0 0
121 0 $self->{config}->{_}->{'last'}=25;
122 }
123 #disable vcs by default
124 0 ▶ if ( ! defined( $self->{config}->{_}->{'vcs'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"_"}{"vcs"}) 0 0 0
125 0 $self->{config}->{_}->{'vcs'}=0;
126 }
127  
128 #gets the Toader::VCS object
129 0 $self->{vcs}=$self->{toader}->getVCS;
130 0 ▶ if ( $toader->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($toader->error) 0 0 0
131 0 $self->{perror}=1;
132 0 $self->{error}=9;
133 $self->{errorString}='Toader->getVCS errored. error="'.
134 0 $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"';
135 0 $self->warn;
136 0 return $self;
137 }
138  
139 #checks if VCS is usable
140 0 $self->{VCSusable}=$self->{vcs}->usable;
141 0 ▶ if ( $self->{vcs}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"vcs"}->error) 0 0 0
142 0 $self->{perror}=1;
143 0 $self->{error}=10;
144 $self->{errorString}='Toader::VCS->usable errored. error="'.
145 0 $self->{toader}->error.'" errorString="'.$self->{toader}->errorString.'"';
146 0 $self->warn;
147 0 return $self;
148 }
149  
150 0 return $self;
151 }
152  
153 =head2 getConfig
154  
155 This returns the L<Config::Tiny> object storing the Toader
156 config.
157  
158 There is no need to do any error checking as long as
159 Toader new suceeded with out issue.
160  
161 my $config=$foo->getConfig;
162  
163 =cut
164  
165 sub getConfig{
166 0 ▶ my $self=$_[0];
Subroutine getConfig: not called CC=2 SCAR=17.9
Pod documented
167
168 #blank any previous errors
169 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
170 0 return undef;
171 }
172  
173 0 return $self->{config};
174 }
175  
176 =head2 getConfigFile
177  
178 This returns the config file for Toader.
179  
180 my $configFile=$foo->getConfigFile;
181  
182 =cut
183  
184 sub getConfigFile{
185 0 ▶ my $self=$_[0];
Subroutine getConfigFile: not called CC=2 SCAR=17.9
Pod documented
186
187 #blank any previous errors
188 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
189 0 return undef;
190 }
191  
192 0 return $self->{configFile};
193 }
194  
195 =head2 listSections
196  
197 This returns a list of sections.
198  
199 my @sections=$foo->listSections;
200  
201 =cut
202  
203 sub listSections{
204 0 ▶ my $self=$_[0];
Subroutine listSections: not called CC=2 SCAR=17.9
Pod documented
205
206 #blank any previous errors
207 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
208 0 return undef;
209 }
210  
211 0 return keys( %{ $self->{config} } );
212 }
213  
214 =head2 listVariables
215  
216 This returns a list of variables for a section.
217  
218 my @variables=$foo->listVariables( $section );
219 if ( $foo->error ){
220 warn( 'error:'.$foo->error.': '.$foo->errorString );
221 }
222  
223 =cut
224  
225 sub listVariables{
226 0 ▶ my $self=$_[0];
Subroutine listVariables: not called CC=5 SCAR=34.0
Pod documented
227 0 my $section=$_[1];
228  
229 #blank any previous errors
230 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
231 0 return undef;
232 }
233  
234 # make sure a variable is specifed
235 0 ▶ if ( ! defined( $section ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
236 0 $self->{error}='6';
237 0 $self->{erroRstring}='No variable name specified';
238 0 $self->warn;
239 0 return undef;
240 }
241  
242 #default to _
243 0 ▶ if ( ! defined( $section ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
244 0 $section='_';
245 }
246  
247 # make sure the section exists
248 0 ▶ if ( ! defined( $self->{config}->{$section} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{$section}) 0 0 0
249 0 $self->{error}='5';
250 0 $self->{erroRstring}='The section "'.$section.'" does not exist';
251 0 $self->warn;
252 0 return undef;
253 }
254  
255 0 return keys( %{ $self->{config}->{$section} } );
256 }
257  
258 =head2 valueDel
259  
260 This deletes a specified value.
261  
262 Two arguments are taken. The first is the section. If not
263 specified, "_" is used. The second and required one is the
264 variable name.
265  
266 As long as the section exists, which it always will for '_',
267 and a variable name is specified, this won't error.
268  
269 $foo->valueDel( $section, $variable );
270  
271 =cut
272  
273 sub valueDel{
274 0 ▶ my $self=$_[0];
Subroutine valueDel: not called CC=6 SCAR=37.4
Pod documented
275 0 my $section=$_[1];
276 0 my $variable=$_[2];
277  
278 #blank any previous errors
279 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
280 0 return undef;
281 }
282  
283 # make sure a variable is specifed
284 0 ▶ if ( ! defined( $variable ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $variable) 0 0 0
285 0 $self->{error}='6';
286 0 $self->{erroRstring}='No variable name specified';
287 0 $self->warn;
288 0 return undef;
289 }
290  
291 #default to _
292 0 ▶ if ( ! defined( $section ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
293 0 $section='_';
294 }
295  
296 # make sure the section exists
297 0 ▶ if ( ! defined( $self->{config}->{$section} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{$section}) 0 0 0
298 0 $self->{error}='5';
299 0 $self->{erroRstring}='The section "'.$section.'" does not exist';
300 0 $self->warn;
301 0 return undef;
302 }
303  
304 0 ▶ if ( ! defined( $self->{config}->{$section}->{$variable} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{$section}{$variable}) 0 0 0
305 0 return 1;
306 }
307
308 0 delete( $self->{config}->{$section}->{$variable} );
309
310 0 return 1;
311 }
312  
313 =head2 valueGet
314  
315 This returns a value that has been set for a variable.
316  
317 Two arguments are taken. The first is the section. If not
318 specified, "_" is used. The second and required one is the
319 variable name.
320  
321 As long as the section exists, which it always will for '_',
322 and a variable name is specified, this won't error.
323  
324 If a value does not exist, undef is returned.
325  
326 my $value=$foo->valueGet( $section, $variable );
327  
328 =cut
329  
330 sub valueGet{
331 0 ▶ my $self=$_[0];
Subroutine valueGet: not called CC=5 SCAR=34.0
Pod documented
332 0 my $section=$_[1];
333 0 my $variable=$_[2];
334
335 #blank any previous errors
336 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
337 0 return undef;
338 }
339
340 # make sure a variable is specifed
341 0 ▶ if ( ! defined( $variable ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $variable) 0 0 0
342 0 $self->{error}='6';
343 0 $self->{erroRstring}='No variable name specified';
344 0 $self->warn;
345 0 return undef;
346 }
347
348 #default to _
349 0 ▶ if ( ! defined( $section ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
350 0 $section='_';
351 }
352
353 # make sure the section exists
354 0 ▶ if ( ! defined( $self->{config}->{$section} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{$section}) 0 0 0
355 0 $self->{error}='5';
356 0 $self->{erroRstring}='The section "'.$section.'" does not exist';
357 0 $self->warn;
358 0 return undef;
359 }
360
361 0 return $self->{config}->{$section}->{$variable};
362 }
363  
364 =head2 valueSet
365  
366 This sets a new value for the config.
367  
368 Third arguments are taken. The first is the section. If not
369 specified, "_" is used. The second and required one is the
370 variable name. The third and required is the the value.
371  
372 If the specified section does not exist, a new one will be created.
373  
374 Neither the section or variable name can match /[\t \n\=\#\;]/.
375  
376 my $value=$foo->valueSet( $section, $variable, $value );
377  
378 =cut
379  
380 sub valueSet{
381 0 ▶ my $self=$_[0];
Subroutine valueSet: not called CC=7 SCAR=40.3
Pod documented
382 0 my $section=$_[1];
383 0 my $variable=$_[2];
384 0 my $value=$_[2];
385
386 #blank any previous errors
387 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
388 0 return undef;
389 }
390  
391 # make sure a variable is specifed
392 0 ▶ if ( ! defined( $variable ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $variable) 0 0 0
393 0 $self->{error}=6;
394 0 $self->{erroRstring}='No variable name specified';
395 0 $self->warn;
396 0 return undef;
397 }
398  
399 #default to _
400 0 ▶ if ( ! defined( $section ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
401 0 $section='_';
402 }
403  
404 #makes sure a valid section and variable is specified
405 0 ▶ if ( $variable =~ /[\t \n\=\#\;]/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($variable =~ /[\t \n\=\#\;]/) 0 0 0
406 0 $self->{error}=7;
407 0 $self->{errorString}='The variable,"'.$variable.'", matched /[\t \n\=\#\;]/';
408 0 $self->warn;
409 0 return undef;
410 }
411 0 ▶ if ( $section =~ /[\t \n\=\#\;]/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /[\t \n\=\#\;]/) 0 0 0
412 0 $self->{error}=7;
413 0 $self->{errorString}='The section,"'.$section.'", matched /[\t \n\=\#\;]/';
414 0 $self->warn;
415 0 return undef;
416 }
417  
418 #set it if no section has been created yet
419 0 ▶ if ( ! defined( $self->{config}->{$section} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{$section}) 0 0 0
420 0 $self->{config}->{$section}={ $variable=>$value };
421 }
422  
423 0 return 1;
424 }
425  
426 =head2 write
427  
428 Writes the config out to the Toader config file.
429  
430 $foo->write;
431 if ( $foo->error ){
432 warn('error:'.$foo->error.': '.$foo->errorString);
433 }
434  
435 =cut
436  
437 sub write{
438 0 ▶ my $self=$_[0];
Subroutine write: not called CC=7 SCAR=40.3
Pod documented
439
440 #blank any previous errors
441 0 ▶ if(!$self->errorblank){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
442 0 return undef;
443 }
444  
445 #try to write the config out and error if it does not
446 0 ▶ if ( ! $self->{config}->write( $self->{configFile} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}->write($self->{"configFile"})) 0 0 0
447 0 $self->{error}=8;
448 0 $self->{errorString}='Failed to write the config out';
449 0 $self->warn;
450 0 return undef;
451 }
452  
453 #if VCS is not usable, stop here
454 0 ▶ if ( ! $self->{VCSusable} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"VCSusable"}) 0 0 0
455 0 return 1;
456 }
457  
458 #if it is under VCS, we have nothing to do
459 0 my $underVCS=$self->{vcs}->underVCS( $self->{configFile} );
460 0 ▶ if ( $self->{vcs}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"vcs"}->error) 0 0 0
461 0 $self->{error}=11;
462 $self->{errorString}='Toader::VCS->underVCS errored. error="'.
463 0 $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"';
464 0 $self->warn;
465 0 return undef;
466 }
467 0 ▶ if ( $underVCS ){
Branch: 1 branch
BranchTrueFalseTotal
if ($underVCS) 0 0 0
468 0 return 1;
469 }
470  
471 #add it as if we reach here it is not under VCS and VCS is being used
472 0 $self->{vcs}->add( $self->{configFile} );
473 0 ▶ if ( $self->{vcs}->error ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"vcs"}->error) 0 0 0
474 0 $self->{error}=12;
475 $self->{errorString}='Toader::VCS->add errored. error="'.
476 0 $self->{vcs}->error.'" errorString="'.$self->{vcs}->errorString.'"';
477 0 $self->warn;
478 0 return undef;
479 }
480  
481 0 return 1;
482 }
483  
484 =head1 ERROR CODES/FLAGS
485  
486 =head2 1, noToaderObj
487  
488 No L<Toader> object specified.
489  
490 =head2 2, notAtoaderObj
491  
492 The specified object is not a L<Toader> object.
493  
494 =head2 3, noDirSet
495  
496 The L<Toader> object did not return a directory.
497  
498 =head2 4, configReadFailed
499  
500 Failed to read the config file.
501  
502 =head2 5, sectionDoesNotExist
503  
504 The section does not exist.
505  
506 =head2 6, noVariableSpecified
507  
508 No variable name specified.
509  
510 =head2 7, invalidVariableOrSection
511  
512 Variable or section matched /[\t \n\=\#\;]/.
513  
514 =head2 8, configWriteFailed
515  
516 Failed to write the config out.
517  
518 =head2 9, getVCSerrored
519  
520 L<Toader>->getVCS errored.
521  
522 =head2 10, VCSusableErrored
523  
524 L<Toader::VCS>->usable errored.
525  
526 =head2 11, underVCSerrored
527  
528 L<Toader::VCS>->underVCS errored.
529  
530 =head2 12, VCSaddErrored
531  
532 L<Toader::VCS>->add errored.
533  
534 =head1 AUTHOR
535  
536 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
537  
538 =head1 BUGS
539  
540 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
541 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will be notified, and then you'll
542 automatically be notified of progress on your bug as I make changes.
543  
544  
545  
546  
547 =head1 SUPPORT
548  
549 You can find documentation for this module with the perldoc command.
550  
551 perldoc Toader::Config
552  
553  
554 You can also look for information at:
555  
556 =over 4
557  
558 =item * RT: CPAN's request tracker
559  
560 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
561  
562 =item * AnnoCPAN: Annotated CPAN documentation
563  
564 L<http://annocpan.org/dist/Toader>
565  
566 =item * CPAN Ratings
567  
568 L<http://cpanratings.perl.org/d/Toader>
569  
570 =item * Search CPAN
571  
572 L<http://search.cpan.org/dist/Toader/>
573  
574 =back
575  
576  
577 =head1 ACKNOWLEDGEMENTS
578  
579  
580 =head1 LICENSE AND COPYRIGHT
581  
582 Copyright 2011 Zane C. Bowers-Hadley.
583  
584 This program is free software; you can redistribute it and/or modify it
585 under the terms of either: the GNU General Public License as published
586 by the Free Software Foundation; or the Artistic License.
587  
588 See http://dev.perl.org/licenses/ for more information.
589  
590  
591 =cut
592  
593 1 1; # End of Toader::Config
« Toader/Render/Directory.pm ↑ Summary Toader/Render/AutoDoc.pm »