blib/lib/Database/BI/CacheDir.pm

filter: Statementstmt 78.5% 44 / 56 Branchbran 50.0% 13 / 26 Conditioncond 37.0% 10 / 27 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 58.5% 79 / 135 CC 25 SCAR 32.7
CC
25
Cov
61%
resolve
40.4
bi_config
20.5
_set
3.1
CRAP 26.3
« blib/lib/Database/BI/Password.pm ↑ Summary blib/lib/Database/BI/Command/password.pm »
1 package Database::BI::CacheDir;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use version; our $VERSION = version->declare('0.023.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Mojo::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # The configuration file the application reads when MOJO_CONFIG does not name
13 # another (Mojolicious takes it from the moniker).
14 3 Readonly my $CONFIG_FILE => 'database_bi.conf';
15  
16 # The environment variables read after Object::Configure, in order.
17 3 Readonly my @ENV_KEYS => qw(CACHEDIR CACHE_DIR);
18  
19 =head1 NAME
20  
21 Database::BI::CacheDir - Where a cache keeps its files
22  
23 =head1 VERSION
24  
25 0.023.1
26  
27 =head1 SYNOPSIS
28  
29 use Database::BI::CacheDir;
30  
31 sub new {
32 my ($class, %args) = @_;
33 my $dir = Database::BI::CacheDir::resolve(
34 class => $class,
35 params => \%args,
36 default => '.bi/geocode-cache',
37 );
38 ...
39 }
40  
41 =head1 DESCRIPTION
42  
43 Every object in this distribution that keeps a cache on disc asks C<resolve()>
44 for its directory when it is made, so one setting moves them all.
45  
46 =head1 SUBROUTINES
47  
48 =head2 resolve
49  
50 Returns the first of these that is set:
51  
52 =over 4
53  
54 =item 1. C<cache_dir> from L<Object::Configure> for C<class>: its config files
55 (F<~/.conf/E<lt>classE<gt>.yml> and the rest), then the environment variable
56 C<E<lt>ClassE<gt>__cache_dir> (C<::> written C<__>, e.g.
57 C<Database__BI__Model__Geocode__cache_dir>). A C<cache_dir> in C<params> is
58 what those fall back on;
59  
60 =item 2. C<$ENV{CACHEDIR}>;
61  
62 =item 3. C<$ENV{CACHE_DIR}>;
63  
64 =item 4. C<cache_dir> in F<database_bi.conf>; a relative one is under the
65 application home;
66  
67 =item 5. C<default>.
68  
69 =back
70  
71 The directory is not made here; the cache makes it when it first stores
72 something.
73  
74 =head3 API SPECIFICATION
75  
76 =head4 INPUT
77  
78 class => string, # optional; the class being made, for Object::Configure
79 params => hashref, # optional; the arguments to its new(), read for cache_dir
80 config => hashref, # optional; the application's config; when the key is absent, database_bi.conf is read (see bi_config)
81 home => string, # optional; the folder a relative config cache_dir is under
82 default => string, # optional; the directory when nothing else is set
83  
84 =head4 OUTPUT
85  
86 A directory name, or C<undef> when nothing is set and there is no C<default>.
87 A config file Object::Configure cannot read is reported with C<carp> and
88 skipped.
89  
90 =cut
91  
92 # resolve(%args) -> $directory | undef
93 #
94 # The one place the directory a cache keeps its files in is chosen, so every
95 # object that caches on disc reads the same settings in the same order:
96 #
97 # 1. Object::Configure, for the class being made (its config files, then the
98 # environment variable <Class__Name>__cache_dir; a cache_dir the caller
99 # passed is what they fall back on, as Object::Configure always does);
100 # 2. $ENV{CACHEDIR};
101 # 3. $ENV{CACHE_DIR};
102 # 4. cache_dir in database_bi.conf (relative: under the application home);
103 # 5. the caller's default (relative: as given).
104 #
105 # Object::Configure is given an array for its logger, which no config file can
106 # replace: a universal.yml that logs to syslog would otherwise be opened for
107 # every object made. A config file it cannot read is reported and skipped, so
108 # a broken ~/.conf costs the cache its setting, not the page.
109 126 ▶sub resolve {
Subroutine resolve: called CC=18 SCAR=37.0
Pod documented
110 126 local ($@, $!);
111 126 my (%args) = @_;
112  
113 126 ▶ my $params = $args{params} // {};
Condition50%1/2
$args{"params"} // {}
l!l
1260
MC/DC0%0/1
$args{"params"} // {}
$args{"params"}
Truth table50%1/2
A
A= $args{"params"}
Aresult
11
00
114  
115 126 ▶ if (defined(my $class = $args{class})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $class = $args{"class"})) 126 0 126
116 126 my $configured;
117 126 my $ok = eval {
118 126 require Object::Configure;
119 $configured = Object::Configure::configure($class, {
120 126 ▶ (_set($params->{cache_dir}) ? (cache_dir => $params->{cache_dir}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
_set($$params{'cache_dir'}) ? : 0 126 126
121 logger => [],
122 });
123 126 1;
124 };
125 126 ▶ carp("Object::Configure could not configure $class: " . ($@ || 'no reason given'))
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 126 126
Condition0%0/2
$@ || 'no reason given'
l!l
00
MC/DC0%0/1
$@ || 'no reason given'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
126 unless $ok;
127 return $configured->{cache_dir}
128 126 ▶ if $ok && ref($configured) eq 'HASH' && _set($configured->{cache_dir});
Branch: 1 branch
BranchTrueFalseTotal
if $ok and ref $configured eq "HASH" and _set($$configured{"cache_dir"}) 0 126 126
Condition33%2/6
$ok and ref $configured eq "HASH" and _set($$configured{"cache_dir"})
!ll&&!rl&&r
01260
$ok and ref $configured eq "HASH"
!ll&&!rl&&r
00126
MC/DC0%0/3
$ok and ref $configured eq "HASH" and _set($$configured{"cache_dir"})
$ok ref $configured eq "HASH" _set($$configured{"cache_dir"})
Truth table25%1/4
A and B and C
A= $okB= ref $configured eq "HASH"C= _set($$configured{"cache_dir"})
ABCresult
0XX0
10X0
1100
1111
129 }
130 126 ▶ return $params->{cache_dir} if _set($params->{cache_dir});
Branch: 1 branch
BranchTrueFalseTotal
if _set($$params{"cache_dir"}) 0 126 126
131  
132 126 for my $key (@ENV_KEYS) {
133 252 ▶ return $ENV{$key} if _set($ENV{$key});
Branch: 1 branch
BranchTrueFalseTotal
if _set($ENV{$key}) 0 252 252
134 }
135  
136 my ($config, $home) = exists($args{config})
137 ? ($args{config}, $args{home})
138 126 ▶ : bi_config();
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'config'} ? : 17 109 126
139 126 ▶ if (ref($config) eq 'HASH' && _set($config->{cache_dir})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq "HASH" and _set($$config{"cache_dir"})) 0 126 126
Condition66%2/3
ref $config eq "HASH" and _set($$config{"cache_dir"})
!ll&&!rl&&r
109170
MC/DC0%0/2
ref $config eq "HASH" and _set($$config{"cache_dir"})
ref $config eq "HASH" _set($$config{"cache_dir"})
Truth table66%2/3
A and B
A= ref $config eq "HASH"B= _set($$config{"cache_dir"})
ABresult
0X0
100
111
140 0 my $dir = Mojo::File->new($config->{cache_dir});
141 $dir = Mojo::File->new($home)->child($config->{cache_dir})
142 0 ▶ if !$dir->is_abs && _set($home);
Branch: 1 branch
BranchTrueFalseTotal
if not $dir->is_abs and _set($home) 0 0 0
Condition0%0/3
not $dir->is_abs and _set($home)
!ll&&!rl&&r
000
MC/DC0%0/2
not $dir->is_abs and _set($home)
not $dir->is_abs _set($home)
Truth table0%0/3
not(A) and B
A= not $dir->is_absB= _set($home)
ABresult
0X0
100
111
143 0 return $dir->to_string;
144 }
145  
146 126 ▶ return _set($args{default}) ? $args{default} : undef;
Branch: 1 branch
BranchTrueFalseTotal
_set($args{'default'}) ? : 13 113 126
147 }
148  
149 =head2 bi_config
150  
151 my ($config, $home) = Database::BI::CacheDir::bi_config();
152  
153 The application's configuration for a caller outside it: the file
154 C<$ENV{MOJO_CONFIG}> names, else F<database_bi.conf> in the current directory.
155  
156 =head3 API SPECIFICATION
157  
158 =head4 INPUT
159  
160 None.
161  
162 =head4 OUTPUT
163  
164 C<($config_hashref, $home)>, where C<$home> is the file's folder; an empty
165 list when there is no file or it does not read (the second is reported with
166 C<carp>).
167  
168 =cut
169  
170 # bi_config() -> ($config_hashref, $home) | ()
171 #
172 # database_bi.conf for a caller outside the web application (a script, or a
173 # library user): the file MOJO_CONFIG names, else database_bi.conf in the
174 # current directory. Its folder is the home a relative setting is under. No
175 # file, or one that does not read, gives an empty list.
176 109 ▶sub bi_config {
Subroutine bi_config: called CC=6 SCAR=30.2
Pod documented
177 109 local ($@, $!);
178 109 ▶ my $file = _set($ENV{MOJO_CONFIG}) ? $ENV{MOJO_CONFIG} : $CONFIG_FILE;
Branch: 1 branch
BranchTrueFalseTotal
_set($ENV{'MOJO_CONFIG'}) ? : 0 109 109
179  
180 109 ▶ return () unless -f $file && -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file and -r _ 109 0 109
Condition33%1/3
-f $file and -r _
!ll&&!rl&&r
10900
MC/DC0%0/2
-f $file and -r _
-f $file -r _
Truth table33%1/3
A and B
A= -f $fileB= -r _
ABresult
0X0
100
111
181  
182 0 my $config;
183 0 my $ok = eval {
184 0 require Mojolicious::Plugin::Config;
185 0 $config = Mojolicious::Plugin::Config->new->load($file, {}, undef);
186 0 1;
187 };
188 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
189 0 ▶ carp("Cannot read $file: " . ($@ || 'no reason given'));
Condition0%0/2
$@ || 'no reason given'
l!l
00
MC/DC0%0/1
$@ || 'no reason given'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
190 0 return ();
191 }
192  
193 0 return ($config, Mojo::File->new($file)->to_abs->dirname->to_string);
194 }
195  
196 882 ▶sub _set {
Subroutine _set: called CC=3 SCAR=11.4
197 882 my $value = shift;
198 882 ▶ return defined($value) && !ref($value) && length($value);
Condition66%4/6
defined $value && !ref($value) && length $value
!ll&&!rl&&r
869013
defined $value and not ref $value
!ll&&!rl&&r
869013
MC/DC33%1/3
defined $value && !ref($value) && length $value
defined $value not ref $value length $value
Truth table50%2/4
A and not(B) && C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
199 }
200  
201 3 1;
202  
203 __END__
« blib/lib/Database/BI/Password.pm ↑ Summary blib/lib/Database/BI/Command/password.pm »