aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Open311/Endpoint/Role/ConfigFile.pm
blob: 1c4b83355dc0385524335bf41e3e7163ebcf1966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Open311::Endpoint::Role::ConfigFile;
use Moo::Role;
use Path::Tiny 'path';
use Carp 'croak';
use YAML ();
use Types::Standard qw( Maybe Str );

has config_file => (
    is => 'ro',
    isa => Maybe[Str],
);

around BUILDARGS => sub {
    my $next = shift;
    my $class = shift;

    my %args = @_;
    if (my $config_file = $args{config_file}) {
        my $cfg = path($config_file);
        croak "$config_file is not a file" unless $cfg->is_file;

        my $config = YAML::LoadFile($cfg) or croak "Couldn't load config from $config_file";
        return $class->$next(%$config, %args);
    }
    else {
        return $class->$next(%args);
    }
};

1;