blib/lib/App/CELL/Config.pm

filter: Statementstmt 75.0% 63 / 84 Branchbran 45.1% 28 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 100% 5 / 5 totaltotal 65.0% 108 / 166 CC 32 SCAR 31.3
CC
32
Cov
62%
get_param_metadata
90.0
exists
11.3
set
9.4
CRAP 22.8
↑ Summary blib/lib/App/CELL/Load.pm »
1 # *************************************************************************
2 # Copyright (c) 2014-2020, SUSE LLC
3 #
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 # *************************************************************************
32  
33 package App::CELL::Config;
34  
35 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 15 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 15 ▶use App::CELL::Log qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 15 ▶use App::CELL::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 #use Data::Dumper;
42 15 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 =head1 NAME
45  
46 App::CELL::Config -- load, store, and dispense meta parameters, core
47 parameters, and site parameters
48  
49  
50  
51 =head1 SYNOPSIS
52
53 use App::CELL::Config qw( $meta $core $site );
54  
55 # get a parameter value (returns value or undef)
56 my $value;
57 $value = $meta->MY_PARAM;
58 $value = $core->MY_PARAM;
59 $value = $site->MY_PARAM;
60  
61 # set a meta parameter
62 $meta->set( 'MY_PARAM', 42 );
63  
64 # set an as-yet undefined core/site parameter
65 $core->set( 'MY_PARAM', 42 );
66 $site->set( 'MY_PARAM', 42 );
67  
68  
69  
70 =head1 DESCRIPTION
71  
72 The purpose of the L<App::CELL::Config> module is to maintain and provide
73 access to three package variables, C<$meta>, C<$core>, and C<$site>, which
74 are actually singleton objects, containing configuration parameters loaded
75 by L<App::CELL::Load> from files in the distro sharedir and the site
76 configuration directory, if any.
77  
78 For details, read L<App::CELL::Guilde>.
79  
80  
81  
82 =head1 EXPORTS
83  
84 This module exports three scalars: the 'singleton' objects C<$meta>,
85 C<$core>, and C<$site>.
86  
87 =cut
88  
89 15 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
90 15 our @EXPORT_OK = qw( $meta $core $site );
91  
92 15 our $meta = bless { CELL_CONFTYPE => 'meta' }, __PACKAGE__;
93 15 our $core = bless { CELL_CONFTYPE => 'core' }, __PACKAGE__;
94 15 our $site = bless { CELL_CONFTYPE => 'site' }, __PACKAGE__;
95  
96  
97  
98 =head1 SUBROUTINES
99  
100  
101 =head2 AUTOLOAD
102  
103 The C<AUTOLOAD> routine handles calls that look like this:
104 $meta->MY_PARAM
105 $core->MY_PARAM
106 $site->MY_PARAM
107  
108 =cut
109  
110 15 our $AUTOLOAD;
111 519 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
112 519 my $self = shift;
113 519 ( my $param ) = $AUTOLOAD =~ m/.*::(.*)$/;
114 519 ▶ return SUPER->DESTROY if $param eq 'DESTROY'; # for Perl <= 5.012
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "DESTROY" 0 519 519
115 519 my ( undef, $file, $line ) = caller;
116 519 ▶ die "Bad call to Config.pm \$$param at $file line $line!" if not blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 519 519
117 519 return _retrieve_param( $self->{'CELL_CONFTYPE'}, $param );
118 }
119  
120 521 ▶sub _retrieve_param {
Subroutine _retrieve_param: called CC=8 SCAR=20.9
121 521 my ( $type, $param ) = @_;
122 521 ▶ if ( $type eq 'meta' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'meta') { } 496 25 521
elsif ($type eq 'core') { } 4 21 25
elsif ($type eq 'site') { } 21 0 21
123 return (exists $meta->{$param})
124 ? $meta->{$param}->{Value}
125 496 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $App::CELL::Config::meta->{$param} ? : 471 25 496
126 } elsif ( $type eq 'core' ) {
127 return (exists $core->{$param})
128 ? $core->{$param}->{Value}
129 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $App::CELL::Config::core->{$param} ? : 4 0 4
130 } elsif ( $type eq 'site' ) {
131 21 ▶ if (exists $site->{$param}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $App::CELL::Config::site->{$param}) { } 11 10 21
elsif (exists $App::CELL::Config::core->{$param}) { } 9 1 10
132 11 return $site->{$param}->{Value};
133 } elsif (exists $core->{$param}) {
134 9 return $core->{$param}->{Value};
135 }
136 }
137 1 return;
138 }
139  
140  
141 =head2 DESTROY
142  
143 For some reason, Perl 5.012 seems to want a DESTROY method
144  
145 =cut
146  
147 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
148 0 my $self = shift;
149 0 ▶ $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 0 0
150 }
151  
152  
153 =head2 exists
154  
155 Determine parameter existence.
156  
157 =cut
158  
159 2 ▶sub exists {
Subroutine exists: called CC=5 SCAR=24.2
Pod documented
160 2 my ( $self, $param ) = @_;
161 2 my $type = $self->{'CELL_CONFTYPE'};
162  
163 2 my $bool;
164 2 ▶ if ( $type eq 'meta' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'meta') { } 2 0 2
elsif ($type eq 'core') { } 0 0 0
elsif ($type eq 'site') { } 0 0 0
165 2 $bool = exists $meta->{ $param };
166 } elsif ( $type eq 'core' ) {
167 0 $bool = exists $core->{ $param };
168 } elsif ( $type eq 'site' ) {
169 0 $bool = exists $site->{ $param };
170 0 ▶ if ( ! $bool ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bool) 0 0 0
171 0 $bool = exists $core->{ $param };
172 }
173 } else {
174 0 die "AAAAAAAAAAGGAHHAGHHG! improper param type in exists routine";
175 }
176 2 return $bool;
177 }
178  
179  
180 =head2 get
181  
182 Wrapper for get_param
183  
184 =cut
185  
186 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
187 0 my ( $self, $param ) = @_;
188 0 return $self->get_param( $param );
189 }
190  
191  
192 =head2 get_param
193  
194 Get value of config param provided in the argument.
195  
196 =cut
197  
198 2 ▶sub get_param {
Subroutine get_param: called CC=2 SCAR=7.0
Pod documented
199 2 my ( $self, $param ) = @_;
200 2 my ( undef, $file, $line ) = caller;
201 2 ▶ die "Bad call to Config.pm \$$param at $file line $line!" if not blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 2 2
202 2 return _retrieve_param( $self->{'CELL_CONFTYPE'}, $param );
203 }
204  
205  
206 =head2 get_param_metadata
207  
208 Routine to provide access not only to the value, but also to the metadata
209 (file and line number where parameter was defined) associated with a
210 given parameter.
211  
212 Takes: parameter name. Returns: reference to the hash associated with the
213 given parameter, or undef if no parameter found.
214  
215 =cut
216  
217 0 ▶sub get_param_metadata {
Subroutine get_param_metadata: not called CC=9 SCAR=45.0
Pod documented
218 0 my ( $self, $param ) = @_;
219 0 my ( undef, $file, $line ) = caller;
220 0 ▶ die "Bad call to Config.pm \$$param at $file line $line!" if not blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 0 0
221 0 my $type = $self->{'CELL_CONFTYPE'};
222 0 ▶ if ( $type eq 'meta' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'meta') { } 0 0 0
elsif ($type eq 'core') { } 0 0 0
elsif ($type eq 'site') { } 0 0 0
223 return (exists $meta->{$param})
224 0 ▶ ? $meta->{$param}
Branch: 1 branch
BranchTrueFalseTotal
exists $App::CELL::Config::meta->{$param} ? : 0 0 0
225 : undef;
226 } elsif ( $type eq 'core' ) {
227 return (exists $core->{$param})
228 0 ▶ ? $core->{$param}
Branch: 1 branch
BranchTrueFalseTotal
exists $App::CELL::Config::core->{$param} ? : 0 0 0
229 : undef;
230 } elsif ( $type eq 'site' ) {
231 0 ▶ if (exists $site->{$param}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $App::CELL::Config::site->{$param}) { } 0 0 0
elsif (exists $App::CELL::Config::core->{$param}) { } 0 0 0
232 0 return $site->{$param};
233 } elsif (exists $core->{$param}) {
234 0 return $core->{$param};
235 }
236 }
237 0 return;
238 }
239  
240  
241 =head2 set
242  
243 Use this function to set new params (meta/core/site) or change existing
244 ones (meta only). Takes two arguments: parameter name and new value.
245 Returns a status object.
246  
247 =cut
248  
249 42 ▶sub set {
Subroutine set: called CC=9 SCAR=22.4
Pod documented
250 42 my ( $self, $param, $value ) = @_;
251 42 ▶ return App::CELL::Status->not_ok if not blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 42 42
252 42 my %ARGS = (
253 level => 'OK',
254 caller => [ CORE::caller() ],
255 );
256 42 ▶ if ( $self->{'CELL_CONFTYPE'} eq 'meta' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'CELL_CONFTYPE'} eq 'meta') { } 22 20 42
elsif ($self->{'CELL_CONFTYPE'} eq 'core') { } 1 19 20
elsif ($self->{'CELL_CONFTYPE'} eq 'site') { } 19 0 19
257 22 ▶ if ( exists $meta->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $App::CELL::Config::meta->{$param}) { } 11 11 22
258 11 ▶ %ARGS = (
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 11 0 11
259 %ARGS,
260 code => 'CELL_OVERWRITE_META_PARAM',
261 args => [ $param, ( defined( $value ) ? $value : 'undef' ) ],
262 );
263 #$log->debug( "Overwriting \$meta->$param with ->$value<-", cell => 1 );
264 } else {
265 #$log->debug( "Setting new \$meta->$param to ->$value<-", cell => 1 );
266 }
267 22 $meta->{$param} = {
268 'File' => (caller)[1],
269 'Line' => (caller)[2],
270 'Value' => $value,
271 };
272 } elsif ( $self->{'CELL_CONFTYPE'} eq 'core' ) {
273 1 ▶ if ( exists $core->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $App::CELL::Config::core->{$param}) { } 1 0 1
274 1 %ARGS = (
275 %ARGS,
276 level => 'ERR',
277 code => 'CELL_PARAM_EXISTS_IMMUTABLE',
278 args => [ 'Core', $param ],
279 );
280 } else {
281 0 $core->{$param} = {
282 'File' => (caller)[1],
283 'Line' => (caller)[2],
284 'Value' => $value,
285 };
286 }
287 } elsif ( $self->{'CELL_CONFTYPE'} eq 'site' ) {
288 19 ▶ if ( exists $site->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $App::CELL::Config::site->{$param}) { } 1 18 19
289 1 %ARGS = (
290 %ARGS,
291 level => 'ERR',
292 code => 'CELL_PARAM_EXISTS_IMMUTABLE',
293 args => [ 'Site', $param ],
294 );
295 } else {
296 18 $site->{$param} = {
297 'File' => (caller)[1],
298 'Line' => (caller)[2],
299 'Value' => $value,
300 };
301 }
302 }
303 42 return App::CELL::Status->new( %ARGS );
304 }
305  
306 # END OF App::CELL::Config MODULE
307 15 1;
↑ Summary blib/lib/App/CELL/Load.pm »