Toader/Render/CSS.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 119 SCAR 52.0
CC
13
Cov
0%
render
56.0
new
20.0
renderCSS
20.0
CRAP 182.0
1 package Toader::Render::CSS;
2  
3 use warnings;
4 use strict;
5 use Toader::Templates;
6 use base 'Error::Helper';
7  
8 =head1 NAME
9  
10 Toader::Render::CSS - This renders the CSS file for Toader.
11  
12 =head1 VERSION
13  
14 Version 0.1.0
15  
16 =cut
17  
18 our $VERSION = '0.1.0';
19  
20 =head1 SYNOPSIS
21  
22 use Toader::Render::CSS;
23  
24 my $foo = Toader::Render::CSS->new($toader);
25  
26 #renders it to a string
27 my $css=$foo->renderCSS;
28 if ( $foo->error ){
29 warn('error: '.$foo->error.":".$foo->errorString);
30 }
31
32 #this renders it to a file... this unlike renderCSS
33 #requires a output directory set
34 $toader->setOutputDir('/foo/bar');
35 if ( $toader->error ){
36 #do something
37 }
38 $foo->render;
39 if ( $foo->error ){
40 warn('error: '.$foo->error.":".$foo->errorString);
41 }
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 This initiates the object.
48  
49 One argument is taken. That is the a L<Toader> object.
50  
51 my $foo=Toader::Render::CSS->new($toader);
52 if($foo->error){
53 warn('error: '.$foo->error.":".$foo->errorString);
54 }
55  
56 =cut
57  
58 sub new{
59 my $toader=$_[1];
60  
61 my $self={
62 error=>undef,
63 errorString=>'',
64 perror=>undef,
65 errorExtra=>{
66 flags=>{
67 1=>'noObj',
68 2=>'noToaderObj',
69 3=>'toaderObjPerror',
70 4=>'noOutputDirSet',
71 5=>'templateFillFailed',
72 6=>'outputDirDoesNotExist',
73 7=>'outputWriteFailed',
74 },
75 },
76 };
77 bless $self;
78  
79 #make sure a Toader object is given
80 if ( ! defined( $toader ) ){
81 $self->{perror}=1;
82 $self->{error}=1;
83 $self->{errorString}='Nothing defined for the Toader object';
84 $self->warn;
85 return $self;
86 }
87 if ( ref( $toader ) ne 'Toader' ){
88 $self->{perror}=1;
89 $self->{error}=2;
90 $self->{errorString}='The specified object is not a Toader object, but a "'.
91 ref( $toader ).'"';
92 $self->warn;
93 return $self;
94 }
95 $self->{toader}=$toader;
96  
97 #make sure the object does not have a permanent error set
98 if( ! $self->{toader}->errorblank ){
99 $self->{perror}=1;
100 $self->{error}=3;
101 $self->{errorString}='The Toader object has a permanent error set';
102 $self->warn;
103 return $self;
104 }
105  
106 $self->{templates}=Toader::Templates->new( {
107 dir=>$self->{toader}->getRootDir,
108 toader=>$toader,
109 } );
110  
111 return $self;
112 }
113  
114 =head2 renderCSS
115  
116 This processes the CSS template.
117  
118 No arguments are accepted.
119  
120 my $css=$foo->renderCSS;
121  
122 =cut
123  
124 sub renderCSS{
125 my $self=$_[0];
126  
127 if ( ! $self->errorblank ){
128 return undef;
129 }
130  
131 my $config=$self->{toader}->getConfig;
132 my $css=$self->{templates}->fill_in( 'css',
133 {
134 c=>\$config,
135 toader=>\$self->{toader},
136 });
137
138 if ( $self->{templates}->error ){
139 $self->{error}=5;
140 $self->{errorString}='Failed to render the template. error="'.
141 $self->{templates}->error.'" errorString="'.
142 $self->{templates}->errorString.'"';
143 $self->warn;
144 return undef;
145 }
146  
147 my $cssInclude=$self->{templates}->fill_in( 'cssInclude',
148 {
149 c=>\$config,
150 toader=>\$self->{toader},
151 });
152  
153 if ( $self->{templates}->error ){
154 $self->{error}=5;
155 $self->{errorString}='Failed to render the template. error="'.
156 $self->{templates}->error.'" errorString="'.
157 $self->{templates}->errorString.'"';
158 $self->warn;
159 return undef;
160 }
161  
162  
163 return $css.$cssInclude;
164 }
165  
166 =head2 render
167  
168 This renders it to the output directory.
169  
170 No arguments are taken.
171  
172 $foo->render;
173  
174 =cut
175  
176 sub render{
177 my $self=$_[0];
178  
179 if ( ! $self->errorblank ){
180 return undef;
181 }
182  
183 my $dir=$self->{toader}->getOutputDir;
184 if ( ! defined( $dir ) ){
185 $self->{error}=4;
186 $self->{errorString}='The Toader object does not have a output directory set';
187 $self->warn;
188 return undef;
189 }
190  
191 my $css=$self->renderCSS;
192 if ( $self->error ){
193 $self->warnString( 'renderCSS errored' );
194 return undef;
195 }
196  
197 if ( ! -d $dir ){
198 if ( ! mkdir( $dir )){
199 $self->{error}=6;
200 $self->{errorString}='The output directory,"'.$dir
201 .'", does not exist and it could be created';
202 $self->warn;
203 return undef;
204 }
205 }
206  
207 my $file=$dir.'/toader.css';
208 my $fh;
209 if ( ! open( $fh, '>', $file ) ){
210 $self->{error}=7;
211 $self->{errorString}='Failed to open "'.$file.'" for writing';
212 $self->warn;
213 return undef;
214 }
215 print $fh $css;
216 close( $fh );
217  
218 return 1;
219 }
220  
221 =head1 ERROR CODES
222  
223 =head2 1, noObj
224  
225 Nothing defined for the L<Toader> object.
226  
227 =head2 2, noToaderObj
228  
229 The specified object is not a L<Toader> object.
230  
231 =head2 3, toaderObjPerror
232  
233 The L<Toader> object has a permanent error set.
234  
235 =head2 4, noOutputDirSet
236  
237 The L<Toader> object does not have a output directory set.
238  
239 =head2 5, templateFillFailed
240  
241 Failed to fill in the CSS template.
242  
243 =head2 6, outputDirDoesNotExist
244  
245 The output directory did not exist and could not be created.
246  
247 =head2 7, outputWriteFailed
248  
249 Failed to write the file out to the output directory.
250  
251 =head1 AUTHOR
252  
253 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
254  
255 =head1 BUGS
256  
257 Please report any bugs or feature requests to C<bug-toader at rt.cpan.org>, or through
258 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Toader>. I will
259 be notified, and then you'll automatically be notified of progress on your bug as I
260 make changes.
261  
262 =head1 SUPPORT
263  
264 You can find documentation for this module with the perldoc command.
265  
266 perldoc Toader::Render:CSS
267  
268 You can also look for information at:
269  
270 =over 4
271  
272 =item * RT: CPAN's request tracker
273  
274 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Toader>
275  
276 =item * AnnoCPAN: Annotated CPAN documentation
277  
278 L<http://annocpan.org/dist/Toader>
279  
280 =item * CPAN Ratings
281  
282 L<http://cpanratings.perl.org/d/Toader>
283  
284 =item * Search CPAN
285  
286 L<http://search.cpan.org/dist/Toader/>
287  
288 =back
289  
290 =head1 ACKNOWLEDGEMENTS
291  
292 =head1 LICENSE AND COPYRIGHT
293  
294 Copyright 2011. Zane C. Bowers-Hadley.
295  
296 This program is free software; you can redistribute it and/or modify it
297 under the terms of either: the GNU General Public License as published
298 by the Free Software Foundation; or the Artistic License.
299  
300 See http://dev.perl.org/licenses/ for more information.
301  
302  
303 =cut
304  
305 1; # End of Toader::Render::CSS