Config/General.pm

filter: Statementstmt 26.2% 27 / 103 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 47.0% 8 / 17 Podpod 100% 7 / 7 totaltotal 25.0% 42 / 168 CC 14 SCAR 33.0
CC
14
Cov
20%
determine_options
56.0
_bless_into_appropriate_class
56.0
new
6.0
CRAP 27.2
↑ Summary Config.pm »
1 #===============================================
2 package Banal::Config::General;
3
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 2 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @ISA = qw(Config::General);
11
12 # Apparently, on debian with perl > 5.8.4 croak() doesn't work anymore without this.
13 # There seems to be some require statement which apparently dies 'cause it can't find Carp::Heavy,
14 2 ▶use Carp::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 2 ▶use Banal::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19
20 2 our $STASH;
21
22 #-----------------------------------------------
23 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
24 0 my $proto = shift;
25 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
26 0 my $opts = $class->determine_options(@_);
27
28
29 0 print STDERR "\n========= " . __PACKAGE__ . " : New ...\n" if 0; # For debugging.
30
31 0 my $self = $class->SUPER::new(%$opts);
32
33 0 $class->_bless_into_appropriate_class($self, $opts);
34
35 0 return $self;
36 }
37
38
39 #-----------------------------------------------
40 # Class method.
41 # -- may be overridden.
42 #-----------------------------------------------
43 0 ▶sub extended_config_class {
Subroutine extended_config_class: not called CC=1 SCAR=6.9
Pod documented
44 0 return __PACKAGE__ ."::Extended";
45 }
46
47
48 #-----------------------------------------------
49 # Class method.
50 # -- may be overridden.
51 #-----------------------------------------------
52 0 ▶sub default_options {
Subroutine default_options: not called CC=1 SCAR=6.9
Pod documented
53 return {
54 0 -InterPolateVars => 1,
55 -InterPolateEnv => 1,
56 -ExtendedAccess => 1,
57 -StrictObjects => 0,
58 -UseApacheInclude => 1,
59 -IncludeRelative => 1,
60 -IncludeDirectories => 1,
61 -IncludeGlob => 1,
62 -UTF8 => 1,
63 -SaveSorted => 1,
64 -AutoTrue => 1,
65 -MergeDuplicateBlocks => 1,
66 -MergeDuplicateOptions => 1,
67 -LowerCaseNames => 1,
68
69 # 'Banal' options
70 -Banal_UsePredefinedVars => 1,
71 -Banal_StashKind => 'normal', # Could have also been 'global'
72 };
73 }
74
75
76 #-----------------------------------------------
77 # Class method.
78 # -- may be overridden, but you better know what you are doing!
79 #-----------------------------------------------
80 0 ▶sub forced_default_options {
Subroutine forced_default_options: not called CC=1 SCAR=6.9
Pod documented
81 return {
82 0 -StrictObjects => 0,
83 # -Plug => { pre_read => *_debug_hook_pre_read }
84 };
85 }
86
87
88 #-----------------------------------------------
89 # Class method.
90 # -- may be overridden, but you should not need to do that normally.
91 #-----------------------------------------------
92 0 ▶sub determine_options {
Subroutine determine_options: not called CC=7 SCAR=40.3
Pod documented
93 0 my $proto = shift;
94 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
95 0 my $stash = $class->build_stash();
96
97 # Preserve a global copy of the very first stash we ever build, hopefully when the process first starts.
98 0 ▶ $STASH ||= $stash;
Condition0%0/3
$Banal::Config::General::STASH ||= $stash
l!l&&r!l&&!r
000
MC/DC0%0/2
$Banal::Config::General::STASH ||= $stash
$Banal::Config::General::STASH $stash
Truth table0%0/3
A ||= B
A= $Banal::Config::General::STASHB= $stash
ABresult
1X1
011
000
99
100 0 my $default_opts = $class->default_options(@_);
101 0 my $forced_default_opts = $class->forced_default_options(@_);
102
103 # Make sure the options passed by the user (i.e. caller) override the defaults.
104 # In between, force a few defaults on options. They can still be overriden by user passed options.
105 # In the end, make sure we have ExtendedAccess.
106 0 my $opts = {
107 %$default_opts,
108 %$forced_default_opts,
109 @_,
110 -ExtendedAccess => 1,
111 };
112
113 # Do the right thing, if we are told to use predefined variables (like NOW, TODAY, ...)
114 0 ▶ if ($opts->{-Banal_UsePredefinedVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"-Banal_UsePredefinedVars"}) 0 0 0
115 0 ▶ $stash = $STASH if ($opts->{-Banal_StashKind} =~ /global/i);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"-Banal_StashKind"} =~ /global/i 0 0 0
116
117 # Yes, a copy is needed. Don't just assign the reference. Otherwise, the global STASH will be polluted.
118 0 my $dc = {%$stash};
119
120 # If there is already a default config, merge it with the stash.
121 0 ▶ if ($opts->{-DefaultConfig}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"-DefaultConfig"}) 0 0 0
122 0 my $odc = $opts->{-DefaultConfig};
123
124 0 ▶ if (ref($odc) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $odc eq 'HASH') { } 0 0 0
125 0 $dc = {%$dc, %$odc};
126 } else {
127 0 croak "Banal::Config::General: Error: Currently, '-DefaultConfig' can only be a reference to a HASH when used in conjunction with '-Banal_UsePredefinedVars'. Let go of one or the other, or both.\n";
128 }
129 }
130
131 0 $opts->{-DefaultConfig} = $dc;
132 }
133
134 0 return $opts;
135 }
136
137
138 #================================
139 # class method
140 #================================
141 0 ▶sub build_stash {
Subroutine build_stash: not called CC=1 SCAR=6.9
Pod documented
142 0 my $class = shift;
143 0 my $stash = {};
144
145 0 my $now = $stash->{'BNL.NOW.OBJ'} = Banal::DateTime->now();
146 0 my $today = $stash->{'BNL.TODAY.OBJ'} = $now->clone()->truncate( to => 'day' );
147 0 my $yesterday = $stash->{'BNL.YESTERDAY.OBJ'} = $today->clone()->subtract( days => 1 );
148 0 my $day_before_yesterday = $stash->{'BNL.DAY_BEFORE_YESTERDAY.OBJ'} = $yesterday->clone()->subtract( days => 1 );
149
150 0 my $tomorrow = $stash->{'BNL.TOMORROW.OBJ'} = $today->clone()->add( days => 1 );
151 0 my $day_after_tomorrow = $stash->{'BNL.DAY_AFTER_TOMORROW.OBJ'} = $tomorrow->clone()->add( days => 1 );
152
153 0 $stash->{'BNL.NOW.STR.ISO'} = $now->iso8601();
154 0 $stash->{'BNL.TODAY.STR.ISO'} = $today->ymd('-');
155 0 $stash->{'BNL.YESTERDAY.STR.ISO'} = $yesterday->ymd('-');
156 0 $stash->{'BNL.DAY_BEFORE_YESTERDAY.STR.ISO'} = $day_before_yesterday->ymd('-');
157 0 $stash->{'BNL.TOMORROW.STR.ISO'} = $tomorrow->ymd('-');
158 0 $stash->{'BNL.DAY_AFTER_TOMORROW.STR.ISO'} = $day_after_tomorrow->ymd('-');
159
160 0 $stash->{'BNL.NOW.STR'} = $now->ymd('') . 'T' . $now->hms('');
161 0 $stash->{'BNL.TODAY.STR'} = $today->ymd('');
162 0 $stash->{'BNL.YESTERDAY.STR'} = $yesterday->ymd('');
163 0 $stash->{'BNL.DAY_BEFORE_YESTERDAY.STR'} = $day_before_yesterday->ymd('');
164 0 $stash->{'BNL.TOMORROW.STR'} = $tomorrow->ymd('');
165 0 $stash->{'BNL.DAY_AFTER_TOMORROW.STR'} = $day_after_tomorrow->ymd('');
166
167 0 $stash->{'BNL.THIS_YEAR'} = $now->year();
168 0 $stash->{'BNL.THIS_YEAR.STD'} = $now->year_std();
169
170 0 $stash->{'BNL.THIS_MONTH'} = $now->month();
171 0 $stash->{'BNL.THIS_MONTH.STD'} = $now->month_std();
172
173 0 $stash->{'BNL.THIS_DAY_OF_MONTH'} = $now->day();
174 0 $stash->{'BNL.THIS_DAY_OF_MONTH.STD'} = $now->day_std();
175
176 0 my $extra_vars = $class->extra_predefined_vars(@_);
177 0 $stash = {%$stash, %$extra_vars};
178
179 0 return $stash;
180 }
181
182
183 #================================
184 # class method
185 # -- Feel free to override
186 # Should return a hashable list.
187 #================================
188 0 ▶sub extra_predefined_vars {
Subroutine extra_predefined_vars: not called CC=1 SCAR=6.9
Pod documented
189 0 return;
190 }
191
192
193
194
195
196 #*************************************************************************
197 # Private stuff.
198 #*************************************************************************
199
200 # Note that this is a class method!
201 # Note also that "class" parameter comes before "object".
202 0 ▶sub _bless_into_appropriate_class {
Subroutine _bless_into_appropriate_class: not called CC=7 SCAR=40.3
203 #
204 # bless into ::Extended if necessary
205 0 my $class = shift;
206 0 my $object = shift;
207 0 my $opts = shift;
208
209 0 ▶ if ($object->{ExtendedAccess} || $opts->{-ExtendedAccess} || UNIVERSAL::isa($object, "Config::General::Extended")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{'ExtendedAccess'} or $opts->{'-ExtendedAccess'} or UNIVERSAL::isa($object, 'Config::General::Extended')) { } 0 0 0
Condition0%0/6
$object->{"ExtendedAccess"} or $opts->{"-ExtendedAccess"} or UNIVERSAL::isa($object, "Config::General::Extended")
l!l&&r!l&&!r
000
$object->{"ExtendedAccess"} or $opts->{"-ExtendedAccess"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$object->{"ExtendedAccess"} or $opts->{"-ExtendedAccess"} or UNIVERSAL::isa($object, "Config::General::Extended")
$object->{"ExtendedAccess"} $opts->{"-ExtendedAccess"} UNIVERSAL::isa($object, "Config::General::Extended")
Truth table0%0/4
A or B or C
A= $object->{"ExtendedAccess"}B= $opts->{"-ExtendedAccess"}C= UNIVERSAL::isa($object, "Config::General::Extended")
ABCresult
1XX1
01X1
0011
0000
210 # we are blessing here again, to get into the ::Extended namespace
211 # for inheriting the methods available over there, which we don't necessarily have.
212
213 0 ▶ my $extended_class = $class->extended_config_class(@_) || $class . "::Extended";
Condition0%0/2
$class->extended_config_class(@_) || $class . '::Extended'
l!l
00
MC/DC0%0/1
$class->extended_config_class(@_) || $class . '::Extended'
$class->extended_config_class(@_)
Truth table0%0/2
A
A= $class->extended_config_class(@_)
Aresult
11
00
214
215 0 ▶ unless (UNVERSAL::isa($object, $extended_class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNVERSAL::isa($object, $extended_class)) 0 0 0
216 0 bless $object, $extended_class;
217 0 eval { require $extended_class; };
218 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
219 0 croak "Banal::Config::General: " . $@;
220 }
221 }
222 }else {
223 0 bless $object, $class;
224 }
225
226 0 return $object;
227 }
228
229
230
231 #================================
232 # A pre-read hook for debugging purposes.
233 #================================
234 0 ▶ sub _debug_hook_pre_read {
Subroutine _debug_hook_pre_read: not called CC=1 SCAR=6.9
235 0 my $fh = shift;
236 0 my $text = join ('', @_);
237 0 my $retval = 1;
238
239 0 print STDERR "\n========= ". __PACKAGE__ . " : Reading a config file ...\n";
240
241 0 my @lines = split ("\n", $text);
242
243 0 return ($retval, $fh, @lines);
244
245 }
246
247 2 1;
248
249
250 __END__
↑ Summary Config.pm »