blib/lib/XAS/Base.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 151 CC 13 SCAR 29.7
CC
13
Cov
0%
_auto_load
56.0
load_msgs
20.0
validate_params
12.0
CRAP 19.4
1 package XAS::Base;
2  
3 our $MESSAGES;
4 our $VERSION = '0.03';
5 our $EXCEPTION = 'XAS::Exception';
6 our ($SCRIPT) = ( $0 =~ m#([^\\/]+)$# );
7  
8 use XAS::Factory;
9 use XAS::Exception;
10 use Config::IniFiles;
11 use Params::Validate ':all';
12  
13 use XAS::Class
14 debug => 0,
15 version => $VERSION,
16 base => 'Badger::Base',
17 utils => 'dotid dir_walk',
18 auto_can => '_auto_load',
19 filesystem => 'Dir',
20 vars => {
21 PARAMS => {
22 -xdebug => { optional => 1, default => 0 }
23 }
24 }
25 ;
26  
27 #use Data::Dumper;
28  
29 # ----------------------------------------------------------------------
30 # Public Methods
31 # ----------------------------------------------------------------------
32  
33 sub load_msgs {
34 my $self = shift;
35  
36 my $messages = $self->class->any_var('MESSAGES');
37 return if (defined($messages->{messages_loaded});
38  
39 foreach my $path (@INC) {
40  
41 my $dir = Dir($path, 'XAS');
42  
43 if ($dir->exists) {
44  
45 dir_walk(
46 -directory => $dir,
47 -filter => $self->env->msgs,
48 -callback => sub {
49 my $file = shift;
50  
51 my $cfg = Config::IniFiles->new(-file => $file->path);
52 if (my @names = $cfg->Parameters('messages')) {
53
54 foreach my $name (@names) {
55
56 $messages->{$name} = $cfg->val('messages', $name);
57  
58 }
59  
60 }
61  
62 }
63 );
64  
65 }
66  
67 }
68  
69 $MESSAGES = $messages;
70  
71 }
72  
73 sub validation_exception {
74 my $param = shift;
75 my $class = shift;
76  
77 my $method = dotid($class) . '.invparams';
78 $param = lcfirst($param);
79  
80 __PACKAGE__->throw_msg($method, 'invparams', $param);
81  
82 }
83  
84 sub validate_params {
85 my $self = shift;
86 my $params = shift;
87 my $specs = shift;
88 my $class = shift;
89  
90 unless (defined($class)) {
91  
92 $class = (caller(1))[3];
93  
94 }
95  
96 my $results = validate_with(
97 params => $params,
98 called => $class,
99 spec => $specs,
100 normalize_keys => sub {
101 my $key = shift;
102 $key =~ s/^-//;
103 return lc $key;
104 },
105 on_fail => sub {
106 my $param = shift;
107 validation_exception($param, $class);
108 },
109 );
110  
111 return wantarray ? @$results : $results;
112  
113 }
114  
115 # ----------------------------------------------------------------------
116 # Private Methods
117 # ----------------------------------------------------------------------
118  
119 sub _auto_load {
120 my $self = shift;
121 my $name = shift;
122  
123 if ($name eq 'alert') {
124  
125 return sub { XAS::Factory->module('alert'); }
126  
127 }
128  
129 if ($name eq 'alerts') {
130  
131 return sub { XAS::Alerts->new(); }
132  
133 }
134  
135 if ($name eq 'env') {
136  
137 return sub { XAS::Factory->module('environment'); }
138  
139 }
140  
141 if ($name eq 'email') {
142  
143 return sub { XAS::Factory->module('email'); }
144  
145 }
146  
147 if ($name eq 'log') {
148  
149 return sub {
150  
151 XAS::Factory->module('logger', {
152 -type => $self->env->logtype,
153 -filename => $self->env->logfile,
154 -levels => {
155 debug => $self->debugging ? 1 : 0,
156 }
157 });
158  
159 }
160  
161 }
162  
163 $self->throw_msg(
164 dotid($self->class) . '.auto_load.invmethod',
165 'invmethod',
166 $name
167 );
168  
169 }
170  
171 sub init {
172 my $self = shift;
173  
174 # load the messages
175
176 $self->load_msgs();
177  
178 # process PARAMS
179  
180 my $class = $self->class;
181 my $params = $self->class->hash_vars('PARAMS');
182 my $p = $self->validate_params(\@_, $params, $class);
183  
184 # build our object
185  
186 $self->{config} = $p;
187  
188 no strict "refs"; # to register new methods in package
189 no warnings; # turn off warnings
190  
191 while (my ($key, $value) = each(%$p)) {
192  
193 $self->{$key} = $value;
194  
195 *$key = sub {
196 my $self = shift;
197 return $self->{$key};
198 };
199  
200 }
201  
202 $self->debugging($self->xdebug);
203  
204 return $self;
205  
206 }
207  
208 package # hide from PAUSE
209 XAS::Alerts;
210  
211 use XAS::Class
212 version => '0.01',
213 base => 'XAS::Singleton',
214 ;
215  
216 sub check {
217 my $self = shift;
218  
219 return $self->{enabled};
220  
221 }
222  
223 sub on {
224 my $self = shift;
225 my ($enable) = $self->validate_params(\@_, [
226 { optional => 1, default => undef, regex => qr/0|1/ }
227 ]);
228  
229 $self->{enabled} = $enable if (defined($enable));
230  
231 return $self->{enabled};
232  
233 }
234  
235 sub init {
236 my $class = shift;
237  
238 my $self = $class->SUPER::init(@_);
239  
240 $self->{enabled} = 0;
241  
242 return $self;
243  
244 }
245  
246 1;
247  
248 __END__
249  
250 =head1 NAME
251  
252 XAS::Base - The base class for the XAS environment
253  
254 =head1 SYNOPSIS
255  
256 use XAS::Class
257 version => '0.01',
258 base => 'XAS::Base',
259 vars => {
260 PARAMS => {}
261 }
262 ;
263  
264 =head1 DESCRIPTION
265  
266 This module defines a base class for the XAS Environment and inherits from
267 L<Badger::Base|http://http://badgerpower.com/docs/Badger/Base.html>. The package variable $PARAMS is used to hold
268 the parameters that this class uses for initialization. Due to the pseudo
269 inheritance of package variables provided by L<Badger::Class|http://badgerpower.com/docs/Badger/Class.html>, these
270 parameters can be changed or extended by inheriting classes. The parameters
271 are validated using L<Params::Validate|https://metacpan.org/pod/Params::Validate>. Any parameters defined in $PARAMS
272 auto-magically become accessors toward their values.
273  
274 =head1 METHODS
275  
276 =head2 new($parameters)
277  
278 This is used to initialized the class. These parameters are validated using
279 the validate_params() method.
280  
281 By default the parameter -xdebug is set to 0. This parameter is used to
282 turn on debugging output.
283  
284 =head2 load_msgs
285  
286 This method loads the message files. It searches @INC for the XAS
287 installation. When found, it loads any message files found into the package
288 variable MESSAGES. A message file has the following format:
289  
290 [messages]
291 exception = %s: %s
292  
293 Where "exception" is the name of the message and rest is the text that will
294 be used for the message.
295  
296 =head2 validate_params($params, $spec, $class)
297  
298 This method is used to validate parameters. Internally this uses
299 Params::Validate::validate_with() for the parameter validation.
300  
301 By convention, all named parameters have a leading dash. This method will
302 strip off that dash and lower case the parameters name.
303  
304 If an validation exception is thrown, the parameter name will have the dash
305 stripped.
306  
307 Based on the $spec, this can return an array or a hashref of validated
308 parameters and values.
309  
310 =over 4
311  
312 =item B<$params>
313  
314 An array ref to a set of parameters.
315  
316 =item B<$spec>
317  
318 A validation spec as defined by L<Params::Validate>.
319  
320 =item B<$class>
321  
322 An optional class that is calling this method. If one is not provided then
323 caller() is used to determine the calling method.
324  
325 =back
326  
327 =head2 validation_exception($param, $class)
328  
329 This is a package level sub routine. It exists to provide a uniform exception
330 error message. It takes these parameters:
331  
332 =over 4
333  
334 =item B<$param>
335  
336 The error message returned by L<Params::Validate>.
337  
338 =item B<$class>
339  
340 The routine that the error occurred in.
341  
342 =back
343  
344 =head1 AUTOLOADING
345  
346 Specific modules can be auto-loaded when a method name is invoked. The
347 following methods have been defined:
348  
349 =head2 alert
350  
351 This will auto-load L<XAS::Lib::Modules::Alerts|XAS::Lib::Modules::Alerts>.
352 Please see that module for more details.
353  
354 =head2 env
355  
356 This will auto-load L<XAS::Lib::Modules::Environment|XAS::Lib::Modules::Environment>.
357 Please see that module for more details.
358  
359 =head2 email
360  
361 This will auto load L<XAS::Lib::Modules::Email|XAS::Lib::Modules::Email>.
362 Please see that module for more details.
363  
364 =head2 log
365  
366 This will auto load L<XAS::Lib::Modules::Log|XAS::Lib::Modules::Log>.
367 Please see that module for more details.
368  
369 =head1 SEE ALSO
370  
371 =over 4
372  
373 =item L<XAS|XAS>
374  
375 =back
376  
377 =head1 AUTHOR
378  
379 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
380  
381 =head1 COPYRIGHT AND LICENSE
382  
383 Copyright (C) 2014 Kevin L. Esteb
384  
385 This library is free software; you can redistribute it and/or modify
386 it under the same terms as Perl itself, either Perl version 5.8.8 or,
387 at your option, any later version of Perl 5 you may have available.
388  
389 See L<http://dev.perl.org/licenses/> for more information.
390  
391 =cut