| 1 | package XAS::Base; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $MESSAGES; | |||||||||
| 4 | 1 | our $VERSION = '0.05'; | |||||||||
| 5 | 1 | our $EXCEPTION = 'XAS::Exception'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use XAS::Factory; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use XAS::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Params::Validate ':all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | use XAS::Class | ||||||||||
| 12 | 1 | debug => 0, | |||||||||
| 13 | version => $VERSION, | ||||||||||
| 14 | base => 'Badger::Base', | ||||||||||
| 15 | utils => 'dotid', | ||||||||||
| 16 | auto_can => '_auto_load', | ||||||||||
| 17 | accessors => 'env', | ||||||||||
| 18 | filesystem => 'Dir', | ||||||||||
| 19 | vars => { | ||||||||||
| 20 | PARAMS => { | ||||||||||
| 21 | -alerts => { optional => 1, default => 0 }, | ||||||||||
| 22 | -xdebug => { optional => 1, default => 0 }, | ||||||||||
| 23 | }, | ||||||||||
| 24 | } | ||||||||||
| 25 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | # ---------------------------------------------------------------------- | ||||||||||
| 30 | # Public Methods | ||||||||||
| 31 | # ---------------------------------------------------------------------- | ||||||||||
| 32 | |||||||||||
| 33 | sub validation_exception { | ||||||||||
| 34 | 0 | ▶ | my $param = shift; | ||||||||
|
Subroutine
validation_exception: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 0 | my $class = shift; | |||||||||
| 36 | |||||||||||
| 37 | 0 | my $method = dotid($class) . '.invparams'; | |||||||||
| 38 | 0 | $param = lcfirst($param); | |||||||||
| 39 | |||||||||||
| 40 | 0 | __PACKAGE__->throw_msg($method, 'invparams', $param); | |||||||||
| 41 | |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub validate_params { | ||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
validate_params: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 46 | 0 | my $params = shift; | |||||||||
| 47 | 0 | my $specs = shift; | |||||||||
| 48 | 0 | my $class = shift; | |||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | unless (defined($class)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 0 | $class = (caller(1))[3]; | |||||||||
| 53 | |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | my $results = validate_with( | ||||||||||
| 57 | params => $params, | ||||||||||
| 58 | called => $class, | ||||||||||
| 59 | spec => $specs, | ||||||||||
| 60 | normalize_keys => sub { | ||||||||||
| 61 | 0 | ▶ | my $key = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 62 | 0 | $key =~ s/^-//; | |||||||||
| 63 | 0 | return lc $key; | |||||||||
| 64 | }, | ||||||||||
| 65 | on_fail => sub { | ||||||||||
| 66 | 0 | ▶ | my $param = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | 0 | validation_exception($param, $class); | |||||||||
| 68 | }, | ||||||||||
| 69 | 0 | ); | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | return wantarray ? @$results : $results; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | # ---------------------------------------------------------------------- | ||||||||||
| 76 | # Private Methods | ||||||||||
| 77 | # ---------------------------------------------------------------------- | ||||||||||
| 78 | |||||||||||
| 79 | sub _auto_load { | ||||||||||
| 80 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_auto_load: not called CC=4 SCAR=30.0
| |||||||||||
| 81 | 0 | my $name = shift; | |||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | if ($name eq 'alert') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | return sub { XAS::Factory->module('alert'); } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | |||||||||||
| 87 | 0 | } | |||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | if ($name eq 'email') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | return sub { XAS::Factory->module('email'); } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 92 | |||||||||||
| 93 | 0 | } | |||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | if ($name eq 'log') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | return sub { | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | XAS::Factory->module('logger', { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 100 | -type => $self->env->log_type, | ||||||||||
| 101 | -filename => $self->env->logfile, | ||||||||||
| 102 | -process => $self->env->script, | ||||||||||
| 103 | -levels => { | ||||||||||
| 104 | debug => $self->env->xdebug, | ||||||||||
| 105 | } | ||||||||||
| 106 | }); | ||||||||||
| 107 | |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 0 | } | |||||||||
| 111 | |||||||||||
| 112 | 0 | my ($package, $filename, $line) = caller(2); | |||||||||
| 113 | 0 | $self->throw_msg( | |||||||||
| 114 | dotid($self->class) . '.auto_load.invmethod', | ||||||||||
| 115 | 'invmethod', | ||||||||||
| 116 | $name, $filename, $line | ||||||||||
| 117 | ); | ||||||||||
| 118 | |||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | sub _create_methods { | ||||||||||
| 122 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_create_methods: not called CC=2 SCAR=10.5
| |||||||||||
| 123 | 0 | my $p = shift; | |||||||||
| 124 | |||||||||||
| 125 | 1 | ▶ | no strict "refs"; # to register new methods in package | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 126 | 1 | ▶ | no warnings; # turn off warnings | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | while (my ($key, $value) = each(%$p)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | $self->{$key} = $value; | |||||||||
| 131 | |||||||||||
| 132 | *$key = sub { | ||||||||||
| 133 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 134 | 0 | return $self->{$key}; | |||||||||
| 135 | 0 | }; | |||||||||
| 136 | |||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | sub init { | ||||||||||
| 142 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 143 | |||||||||||
| 144 | # load the environment | ||||||||||
| 145 | |||||||||||
| 146 | 0 | $self->{env} = XAS::Factory->module('environment'); | |||||||||
| 147 | 0 | $XAS::Base::MESSAGES = $self->env->get_msgs(); | |||||||||
| 148 | |||||||||||
| 149 | # process PARAMS | ||||||||||
| 150 | |||||||||||
| 151 | 0 | my $class = $self->class; | |||||||||
| 152 | 0 | my $params = $self->class->hash_vars('PARAMS'); | |||||||||
| 153 | 0 | my $p = $self->validate_params(\@_, $params, $class); | |||||||||
| 154 | |||||||||||
| 155 | # build our object | ||||||||||
| 156 | |||||||||||
| 157 | 0 | $self->{config} = $p; | |||||||||
| 158 | 0 | $self->_create_methods($p); | |||||||||
| 159 | |||||||||||
| 160 | # set defaults | ||||||||||
| 161 | |||||||||||
| 162 | 0 | $self->env->alerts($self->alerts); | |||||||||
| 163 | 0 | $self->env->xdebug($self->xdebug); | |||||||||
| 164 | |||||||||||
| 165 | 0 | return $self; | |||||||||
| 166 | |||||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | 1 | 1; | |||||||||
| 170 | |||||||||||
| 171 | __END__ | ||||||||||
| 172 | |||||||||||
| 173 | =head1 NAME | ||||||||||
| 174 | |||||||||||
| 175 | XAS::Base - The base class for the XAS environment | ||||||||||
| 176 | |||||||||||
| 177 | =head1 SYNOPSIS | ||||||||||
| 178 | |||||||||||
| 179 | use XAS::Class | ||||||||||
| 180 | version => '0.01', | ||||||||||
| 181 | base => 'XAS::Base', | ||||||||||
| 182 | vars => { | ||||||||||
| 183 | PARAMS => {} | ||||||||||
| 184 | } | ||||||||||
| 185 | ; | ||||||||||
| 186 | |||||||||||
| 187 | =head1 DESCRIPTION | ||||||||||
| 188 | |||||||||||
| 189 | This module defines a base class for the XAS Environment and inherits from | ||||||||||
| 190 | L<Badger::Base|https://metacpan.org/pod/Badger::Base>. The package variable $PARAMS is used to hold | ||||||||||
| 191 | the parameters that this class uses for initialization. Due to the pseudo | ||||||||||
| 192 | inheritance of package variables provided by L<Badger::Class|https://metacpan.org/pod/Badger::Class>, these | ||||||||||
| 193 | parameters can be changed or extended by inheriting classes. The parameters | ||||||||||
| 194 | are validated using L<Params::Validate|https://metacpan.org/pod/Params::Validate>. Any parameters defined in $PARAMS | ||||||||||
| 195 | auto-magically become accessors toward their values. | ||||||||||
| 196 | |||||||||||
| 197 | =head1 METHODS | ||||||||||
| 198 | |||||||||||
| 199 | =head2 new($parameters) | ||||||||||
| 200 | |||||||||||
| 201 | This is used to initialized the class. These parameters are validated using | ||||||||||
| 202 | the validate_params() method. | ||||||||||
| 203 | |||||||||||
| 204 | By default the parameter -xdebug is set to 0. This parameter is used to | ||||||||||
| 205 | turn on debugging output. | ||||||||||
| 206 | |||||||||||
| 207 | =head2 validate_params($params, $spec, $class) | ||||||||||
| 208 | |||||||||||
| 209 | This method is used to validate parameters. Internally this uses | ||||||||||
| 210 | Params::Validate::validate_with() for the parameter validation. | ||||||||||
| 211 | |||||||||||
| 212 | By convention, all named parameters have a leading dash. This method will | ||||||||||
| 213 | strip off that dash and lower case the parameters name. | ||||||||||
| 214 | |||||||||||
| 215 | If an validation exception is thrown, the parameter name will have the dash | ||||||||||
| 216 | stripped. | ||||||||||
| 217 | |||||||||||
| 218 | Based on the $spec, this can return an array or a hashref of validated | ||||||||||
| 219 | parameters and values. | ||||||||||
| 220 | |||||||||||
| 221 | =over 4 | ||||||||||
| 222 | |||||||||||
| 223 | =item B<$params> | ||||||||||
| 224 | |||||||||||
| 225 | An array ref to a set of parameters. | ||||||||||
| 226 | |||||||||||
| 227 | =item B<$spec> | ||||||||||
| 228 | |||||||||||
| 229 | A validation spec as defined by L<Params::Validate|https://metacpan.org/pod/Params::Validate>. | ||||||||||
| 230 | |||||||||||
| 231 | =item B<$class> | ||||||||||
| 232 | |||||||||||
| 233 | An optional class that is calling this method. If one is not provided then | ||||||||||
| 234 | caller() is used to determine the calling method. | ||||||||||
| 235 | |||||||||||
| 236 | =back | ||||||||||
| 237 | |||||||||||
| 238 | =head2 validation_exception($param, $class) | ||||||||||
| 239 | |||||||||||
| 240 | This is a package level sub routine. It exists to provide a uniform exception | ||||||||||
| 241 | error message. It takes these parameters: | ||||||||||
| 242 | |||||||||||
| 243 | =over 4 | ||||||||||
| 244 | |||||||||||
| 245 | =item B<$param> | ||||||||||
| 246 | |||||||||||
| 247 | The error message returned by L<Params::Validate|https://metacpan.org/pod/Params::Validate>. | ||||||||||
| 248 | |||||||||||
| 249 | =item B<$class> | ||||||||||
| 250 | |||||||||||
| 251 | The routine that the error occurred in. | ||||||||||
| 252 | |||||||||||
| 253 | =back | ||||||||||
| 254 | |||||||||||
| 255 | =head1 AUTOLOADING | ||||||||||
| 256 | |||||||||||
| 257 | Specific modules can be auto-loaded when a method name is invoked. The | ||||||||||
| 258 | following methods have been defined: | ||||||||||
| 259 | |||||||||||
| 260 | =head2 alert | ||||||||||
| 261 | |||||||||||
| 262 | This will auto-load L<XAS::Lib::Modules::Alerts|XAS::Lib::Modules::Alerts>. | ||||||||||
| 263 | Please see that module for more details. | ||||||||||
| 264 | |||||||||||
| 265 | =head2 env | ||||||||||
| 266 | |||||||||||
| 267 | This will auto-load L<XAS::Lib::Modules::Environment|XAS::Lib::Modules::Environment>. | ||||||||||
| 268 | Please see that module for more details. | ||||||||||
| 269 | |||||||||||
| 270 | =head2 email | ||||||||||
| 271 | |||||||||||
| 272 | This will auto load L<XAS::Lib::Modules::Email|XAS::Lib::Modules::Email>. | ||||||||||
| 273 | Please see that module for more details. | ||||||||||
| 274 | |||||||||||
| 275 | =head2 log | ||||||||||
| 276 | |||||||||||
| 277 | This will auto load L<XAS::Lib::Modules::Log|XAS::Lib::Modules::Log>. | ||||||||||
| 278 | Please see that module for more details. | ||||||||||
| 279 | |||||||||||
| 280 | =head1 SEE ALSO | ||||||||||
| 281 | |||||||||||
| 282 | =over 4 | ||||||||||
| 283 | |||||||||||
| 284 | =item L<XAS|XAS> | ||||||||||
| 285 | |||||||||||
| 286 | =back | ||||||||||
| 287 | |||||||||||
| 288 | =head1 AUTHOR | ||||||||||
| 289 | |||||||||||
| 290 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 291 | |||||||||||
| 292 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 293 | |||||||||||
| 294 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 295 | |||||||||||
| 296 | TThis is free software; you can redistribute it and/or modify it under | ||||||||||
| 297 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 298 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 299 | |||||||||||
| 300 | =cut | ||||||||||