File Coverage

File:lib/Code/Statistics/Config.pm
Coverage:100.0%

linestmtbrancondsubpodtimecode
1
2
2
2
0
0
0
use strict;
2
2
2
2
0
0
0
use warnings;
3
4package Code::Statistics::Config;
5
6# ABSTRACT: merges configuration options from various sources
7
8
2
2
2
0
0
0
use Moose;
9
2
2
2
0
0
0
use MooseX::HasDefaults::RO;
10
11
2
2
2
0
0
0
use Hash::Merge qw( merge );
12
2
2
2
0
0
0
use Config::INI::Reader;
13
2
2
2
0
0
0
use Path::Class qw(file);
14
2
2
2
0
0
0
use File::HomeDir;
15
16has args => ( isa => 'HashRef', default => sub {{}} );
17
18has command => ( isa => 'Str', );
19
20has conf_file => ( isa => 'Str', default => '.codestatrc' );
21
22has global_conf_file => ( isa => 'Str', default => sub { file( File::HomeDir->my_home, '.codestatrc' )->stringify } );
23
24has profile => ( isa => 'Str', );
25
26 - 29
=head2 assemble
    Builds the command-related configuration hash. The hash contains all config
    options from the global config file, local file and command line arguments.
=cut
30
31sub assemble {
32
12
0
    my ( $self ) = @_;
33
34
12
0
    my $config = {};
35
36
12
0
    $config = merge( $self->_global_config, $config );
37
12
0
    $config = merge( $self->_local_config, $config );
38
12
0
    $config = merge( $self->args, $config );
39
40
12
0
    return $config;
41}
42
43sub _local_config {
44
12
0
    my ( $self ) = @_;
45
46
12
0
    return $self->_merged_conf_from( $self->conf_file );
47}
48
49sub _global_config {
50
12
0
    my ( $self ) = @_;
51
52
12
0
    return $self->_merged_conf_from( $self->global_conf_file );
53}
54
55sub _merged_conf_from {
56
24
0
    my ( $self, $file ) = @_;
57
58
24
0
    return {} if !$file or !-e $file;
59
60
14
0
    my $conf = Config::INI::Reader->read_file( $file );
61
62
14
0
    my $merge;
63
14
42
0
0
    my @sections = grep { defined } ( '_', $self->command, $self->_profile_section );
64
14
0
    for ( @sections ) {
65
42
0
        next if !$conf->{$_};
66
30
0
        $merge = merge( $conf->{$_}, $merge );
67    }
68
69
14
0
    return $merge;
70}
71
72sub _profile_section {
73
14
0
    my ( $self ) = @_;
74
75
14
0
    my $section = $self->command;
76
14
0
    $section .= '::' . $self->profile if $self->profile;
77
78
14
0
    return $section;
79}
80
811;