File Coverage

File:blib/lib/Database/BI/CacheDir.pm
Coverage:87.4%

linestmtbrancondsubtimecode
1package Database::BI::CacheDir;
2
3
23
23
23
158214
22
190
use strict;
4
23
23
23
30
24
437
use warnings;
5
6
23
23
23
116
480
102
use version; our $VERSION = version->declare('0.025.0');
7
8
23
23
23
944
17
406
use Carp        qw(carp);
9
23
23
23
30
54306
121
use Mojo::File  ();
10
23
23
23
142
1008
4678
use Readonly;
11
12# The configuration file the application reads when MOJO_CONFIG does not name
13# another (Mojolicious takes it from the moniker).
14Readonly my $CONFIG_FILE => 'database_bi.conf';
15
16# The environment variables read after Object::Configure, in order.
17Readonly my @ENV_KEYS => qw(CACHEDIR CACHE_DIR);
18
19 - 90
=head1 NAME

Database::BI::CacheDir - Where a cache keeps its files

=head1 VERSION

0.025.0

=head1 SYNOPSIS

    use Database::BI::CacheDir;

    sub new {
        my ($class, %args) = @_;
        my $dir = Database::BI::CacheDir::resolve(
            class   => $class,
            params  => \%args,
            default => '.bi/geocode-cache',
        );
        ...
    }

=head1 DESCRIPTION

Every object in this distribution that keeps a cache on disc asks C<resolve()>
for its directory when it is made, so one setting moves them all.

=head1 SUBROUTINES

=head2 resolve

Returns the first of these that is set:

=over 4

=item 1. C<cache_dir> from L<Object::Configure> for C<class>: its config files
(F<~/.conf/E<lt>classE<gt>.yml> and the rest), then the environment variable
C<E<lt>ClassE<gt>__cache_dir> (C<::> written C<__>, e.g.
C<Database__BI__Model__Geocode__cache_dir>).  A C<cache_dir> in C<params> is
what those fall back on;

=item 2. C<$ENV{CACHEDIR}>;

=item 3. C<$ENV{CACHE_DIR}>;

=item 4. C<cache_dir> in F<database_bi.conf>; a relative one is under the
application home;

=item 5. C<default>.

=back

The directory is not made here; the cache makes it when it first stores
something.

=head3 API SPECIFICATION

=head4 INPUT

    class   => string,   # optional; the class being made, for Object::Configure
    params  => hashref,  # optional; the arguments to its new(), read for cache_dir
    config  => hashref,  # optional; the application's config; when the key is absent, database_bi.conf is read (see bi_config)
    home    => string,   # optional; the folder a relative config cache_dir is under
    default => string,   # optional; the directory when nothing else is set

=head4 OUTPUT

A directory name, or C<undef> when nothing is set and there is no C<default>.
A config file Object::Configure cannot read is reported with C<carp> and
skipped.

=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.
109sub resolve {
110
320
80092
        local ($@, $!);
111
320
488
        my (%args) = @_;
112
113
320
440
        my $params = $args{params} // {};
114
115
320
399
        if (defined(my $class = $args{class})) {
116
318
170
                my $configured;
117
318
206
                my $ok = eval {
118
318
791
                        require Object::Configure;
119                        $configured = Object::Configure::configure($class, {
120
318
136082
                                (_set($params->{cache_dir}) ? (cache_dir => $params->{cache_dir}) : ()),
121                                logger => [],
122                        });
123
318
193672
                        1;
124                };
125
318
410
                carp("Object::Configure could not configure $class: " . ($@ || 'no reason given'))
126                        unless $ok;
127                return $configured->{cache_dir}
128
318
951
                        if $ok && ref($configured) eq 'HASH' && _set($configured->{cache_dir});
129        }
130
315
453
        return $params->{cache_dir} if _set($params->{cache_dir});
131
132
315
568
        for my $key (@ENV_KEYS) {
133
628
1277
                return $ENV{$key} if _set($ENV{$key});
134        }
135
136        my ($config, $home) = exists($args{config})
137                ? ($args{config}, $args{home})
138
310
735
                : bi_config();
139
310
519
        if (ref($config) eq 'HASH' && _set($config->{cache_dir})) {
140
11
25
                my $dir = Mojo::File->new($config->{cache_dir});
141                $dir = Mojo::File->new($home)->child($config->{cache_dir})
142
11
53
                        if !$dir->is_abs && _set($home);
143
11
92
                return $dir->to_string;
144        }
145
146
299
294
        return _set($args{default}) ? $args{default} : undef;
147}
148
149 - 168
=head2 bi_config

    my ($config, $home) = Database::BI::CacheDir::bi_config();

The application's configuration for a caller outside it: the file
C<$ENV{MOJO_CONFIG}> names, else F<database_bi.conf> in the current directory.

=head3 API SPECIFICATION

=head4 INPUT

None.

=head4 OUTPUT

C<($config_hashref, $home)>, where C<$home> is the file's folder; an empty
list when there is no file or it does not read (the second is reported with
C<carp>).

=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.
176sub bi_config {
177
219
567
        local ($@, $!);
178
219
207
        my $file = _set($ENV{MOJO_CONFIG}) ? $ENV{MOJO_CONFIG} : $CONFIG_FILE;
179
180
219
757
        return () unless -f $file && -r _;
181
182
1
0
        my $config;
183
1
1
        my $ok = eval {
184
1
131
                require Mojolicious::Plugin::Config;
185
1
576
                $config = Mojolicious::Plugin::Config->new->load($file, {}, undef);
186
1
171
                1;
187        };
188
1
4
        unless ($ok) {
189
0
0
                carp("Cannot read $file: " . ($@ || 'no reason given'));
190
0
0
                return ();
191        }
192
193
1
2
        return ($config, Mojo::File->new($file)->to_abs->dirname->to_string);
194}
195
196sub _set {
197
2190
5549
        my $value = shift;
198
2190
3496
        return defined($value) && !ref($value) && length($value);
199}
200
2011;
202