Config.pm

filter: Statementstmt 37.3% 43 / 115 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 33.3% 10 / 30 Podpod 100% 10 / 10 totaltotal 33.8% 63 / 186 CC 14 SCAR 31.1
CC
14
Cov
31%
guess_config_file_path
90.0
grab_cfg
20.0
_build_cfg_context
12.0
CRAP 22.5
« Config/General.pm ↑ Summary Config/General/Extended.pm »
1 package Banal::Config;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.11';
9  
10 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Banal::Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Banal::Config::General::Extended;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Banal::Utils::Data qw(banal_get_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 has 'verbose' => (is => 'rw', lazy_build=>1);
20 2 has 'debug' => (is => 'rw', lazy_build=>1);
21 0 ▶has 'switches' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # Typically contains command line switches as produced by Getopt::Long or Getopt::Descriptive, or something that resembles it.
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 0 ▶has 'options' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # The hash that gets passed to "new" for the actual configuration object (xcfg). The 'ConfigFile' option will default to the value of 'source' property.
Subroutine __ANON__: not called CC=1 SCAR=1.2
23 2 has 'source' => (is => 'rw', isa => 'Str', lazy_build=>1); # The path to the configuration file. If it's not set, it will be guessed based on the name of the running process ($0). Note that this may be overriden by the "-ConfigFile" option.
24 2 has 'xcfg_class' => (is => 'rw', isa => 'Str', default=>'Banal::Config::General::Extended' );
25 2 has 'xcfg' => (
26 is => 'rw',
27 isa => 'Banal::Config::General::Extended',
28 lazy_build => 1,
29 handles => [qw (obj value hash array is_hash is_array is_scalar exists keys delete configfile find)],
30 );
31
32
33 2 has 'cfg_hash' => (is => 'rw', lazy_build=>1);
34  
35 2 has 'cfg_context' => (is => 'rw', lazy_build=>1); # If you do not set this, you can also provide it with the 'cfg_context' switch or the '-Banal_ConfigContext' option. Otherwise, it will take upon the value given by 'cfg_context_default'
36 2 has 'cfg_context_default' => (is => 'rw', lazy_build=>1); # You may wish to override this, if needed.
37  
38 0 ▶has 'default_options_for_banal_get_data' => (is => 'rw', default=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 {
40 0 search_upwards_while_not_defined => 1,
41 use_path_semantics => 1,
42 path_separator => '/',
43 remove_extra_separators => 1,
44 remove_leading_separator => 0,
45 remove_trailing_separator => 1,
46 remove_empty_segments => 1,
47 try_avoiding_repeated_segments => 1,
48 lower_case => 1,
49 trim => 1,
50 }
51 }
52 2 );
53  
54  
55  
56 #-----------------------------------------------
57 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
58 0 my $self = shift;
59
60 0 return $self->reload();
61 }
62  
63 #-----------------------------------------------
64 0 ▶sub reload {
Subroutine reload: not called CC=1 SCAR=6.9
Pod documented
65 0 my $self = shift;
66 0 my $cc = $self->xcfg_class;
67 0 my $opts = $self->options;
68
69 0 eval {
70 0 require $cc;
71 };
72 0 my $c = $cc->new(-ConfigFile=>$self->source, %$opts); # source can be overriden with the options.
73
74 0 return $self->xcfg($c);
75 }
76
77
78 #
79 #-----------------------------------------------
80 0 ▶sub get_cfg {
Subroutine get_cfg: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82 0 return $self->grab_cfg(key=>[@_]);
83 }
84  
85  
86 #-----------------------------------------------
87 0 ▶sub grab_cfg {
Subroutine grab_cfg: not called CC=4 SCAR=30.0
Pod documented
88 0 my $self = shift;
89 0 my $args = {@_};
90
91 0 ▶ $args->{data} ||= $self->cfg_hash();
Condition0%0/3
$args->{"data"} ||= $self->cfg_hash
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"data"} ||= $self->cfg_hash
$args->{"data"} $self->cfg_hash
Truth table0%0/3
A ||= B
A= $args->{"data"}B= $self->cfg_hash
ABresult
1X1
011
000
92
93 0 ▶ unless (defined($args->{context})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"context"}) 0 0 0
94 0 $args->{context} = $self->cfg_context();
95 }
96
97 0 ▶ unless (defined($args->{options})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"options"}) 0 0 0
98 # get a copy.
99 0 my $opts = $self->default_options_for_banal_get_data();
100 0 $args->{options} = {%$opts};
101 }
102
103  
104 0 return banal_get_data(%$args);
105 }
106  
107  
108 #***************************************************
109 # Possible Overrides
110 #***************************************************
111 #-----------------------------------------------
112 0 ▶sub get_default_config_term {
Subroutine get_default_config_term: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114
115 0 my ($prg_volume, $prg_dirs, $prg_name) = File::Spec->splitpath( $0 );
116
117 0 return $prg_name;
118 }
119  
120 #-----------------------------------------------
121 0 ▶sub get_default_config_file_base_name {
Subroutine get_default_config_file_base_name: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123
124 0 return $self->get_default_config_term();
125 }
126  
127  
128 #***************************************************
129 # Less likely overrides
130 #***************************************************
131 #-----------------------------------------------
132 0 ▶sub guess_config_file_path {
Subroutine guess_config_file_path: not called CC=9 SCAR=45.0
Pod documented
133 0 my $self = shift;
134 0 my $args = $self->switches;
135 0 $args = {%$args, @_}; # swicth overrides are possible by passing arguments to the function.
136
137 # If we have an explicit argument for the config file path, return that.
138 0 foreach my $opt ($self->get_possible_option_names_for_config_file_path(@_)) {
139 0 my $p = $args->{$opt};
140 0 ▶ return $p if ($p);
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
141 }
142
143 # Or else, if we have a defined ENVIRONMENT variable that contains a value, return that.
144 0 foreach my $v ($self->get_possible_environment_variable_names_for_config_file_path(@_)) {
145 0 my $p = $ENV{$v};
146 0 ▶ return $p if ($p);
Branch: 1 branch
BranchTrueFalseTotal
if $p 0 0 0
147 }
148
149 # Otherwise, return the first config file that exists in a list of possible file paths (normally based on the program name).
150 0 foreach my $p ($self->get_possible_config_file_paths(@_)) {
151 0 ▶ return $p if ($p and (-e $p));
Branch: 1 branch
BranchTrueFalseTotal
if $p and -e $p 0 0 0
Condition0%0/3
$p and -e $p
!ll&&!rl&&r
000
MC/DC0%0/2
$p and -e $p
$p -e $p
Truth table0%0/3
A and B
A= $pB= -e $p
ABresult
0X0
100
111
152 }
153
154
155 # Too bad. We've got nothing.
156
157 0 ▶ warn "No config file can be accessed. Does it exist?!\n" if $self->verbose > 7; # DEBUG
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 7 0 0 0
158 0 return;
159 }
160  
161
162 #-----------------------------------------------
163 0 ▶sub get_possible_option_names_for_config_file_path {
Subroutine get_possible_option_names_for_config_file_path: not called CC=1 SCAR=6.9
Pod documented
164 0 my $self = shift;
165 0 my @possibilities;
166
167 0 my $term = $self->get_default_config_term();
168
169 0 @possibilities = (
170 $term . "_cfg",
171 "cfg_" . $term,
172 "cfg",
173 );
174 0 return @possibilities;
175 }
176  
177 #-----------------------------------------------
178 0 ▶sub get_possible_environment_variable_names_for_config_file_path {
Subroutine get_possible_environment_variable_names_for_config_file_path: not called CC=1 SCAR=6.9
Pod documented
179 0 my $self = shift;
180 0 my @possibilities;
181
182 0 my $term = $self->get_default_config_term();
183
184 0 @possibilities = (
185 uc($term . "_CFG"),
186 uc("CFG_" . $term),
187 );
188 0 return @possibilities;
189 }
190  
191 #-----------------------------------------------
192 0 ▶sub get_possible_config_file_paths {
Subroutine get_possible_config_file_paths: not called CC=1 SCAR=6.9
Pod documented
193 0 my $self = shift;
194 0 my @possibilities;
195  
196 0 my $base_name = $self->get_default_config_file_base_name();
197
198 0 @possibilities = (
199 "./test/etc/" . $base_name . ".conf", # this one is for testing purposes during "make test"
200 "~/." . $base_name . ".conf",
201 "/etc/" . $base_name . ".conf",
202 "." . $base_name . ".conf",
203 );
204 0 return @possibilities;
205 }
206  
207  
208  
209  
210  
211 #**********************************************
212 # BUILDERS
213 #**********************************************
214 #--------------------------------------
215 0 ▶sub _build_verbose {
Subroutine _build_verbose: not called CC=1 SCAR=6.9
216 0 my $self = shift;
217
218 0 return $self->switches->verbose;
219 }
220  
221 #--------------------------------------
222 0 ▶sub _build_debug {
Subroutine _build_debug: not called CC=1 SCAR=6.9
223 0 my $self = shift;
224
225 0 return ($self->verbose >= 7);
226 }
227  
228 #--------------------------------------
229 0 ▶sub _build_source {
Subroutine _build_source: not called CC=1 SCAR=6.9
230 0 my $self = shift;
231 0 return $self->guess_config_file_path();
232 }
233  
234 #--------------------------------------
235 0 ▶sub _build_xcfg {
Subroutine _build_xcfg: not called CC=1 SCAR=6.9
236 0 my $self = shift;
237 0 return $self->load();
238 }
239  
240 #--------------------------------------
241 0 ▶sub _build_cfg_hash {
Subroutine _build_cfg_hash: not called CC=1 SCAR=6.9
242 0 my $self = shift;
243 0 return $self->xcfg()->config;
244 }
245  
246  
247 #--------------------------------------
248 0 ▶sub _build_cfg_context {
Subroutine _build_cfg_context: not called CC=3 SCAR=24.8
249 0 my $self = shift;
250 0 ▶ my $ctx = $self->switches->{cfg_context} || $self->options->{-Banal_ConfigContext} || $self->cfg_context_default;
Condition0%0/6
$self->switches->{'cfg_context'} || $self->options->{'-Banal_ConfigContext'} || $self->cfg_context_default
l!l&&r!l&&!r
000
$self->switches->{"cfg_context"} or $self->options->{"-Banal_ConfigContext"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->switches->{'cfg_context'} || $self->options->{'-Banal_ConfigContext'} || $self->cfg_context_default
$self->switches->{"cfg_context"} $self->options->{"-Banal_ConfigContext"} $self->cfg_context_default
Truth table0%0/4
A or B || C
A= $self->switches->{"cfg_context"}B= $self->options->{"-Banal_ConfigContext"}C= $self->cfg_context_default
ABCresult
1XX1
01X1
0011
0000
251 0 return $ctx;
252 }
253  
254 #--------------------------------------
255 0 ▶sub _build_cfg_context_default {
Subroutine _build_cfg_context_default: not called CC=1 SCAR=6.9
256 0 my $self = shift;
257 0 my $ctx = $self->get_default_config_term();
258 0 return $ctx;
259 }
260  
261  
262  
263 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 2 __PACKAGE__->meta->make_immutable;
265  
266 2 1;
267  
268  
269  
270  
271  
272 __END__
« Config/General.pm ↑ Summary Config/General/Extended.pm »