File Coverage

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

linestmtbrancondsubpodtimecode
1
1
1
1
0
0
0
use strict;
2
1
1
1
0
0
0
use warnings;
3
4package Code::Statistics::Config;
5
6# ABSTRACT: merges configuration options from various sources
7
8
1
1
1
0
0
0
use Moose;
9
1
1
1
0
0
0
use MooseX::HasDefaults::RO;
10
11
1
1
1
0
0
0
use Hash::Merge qw( merge );
12
1
1
1
0
0
0
use Config::INI::Reader;
13
1
1
1
0
0
0
use Path::Class qw(file);
14
1
1
1
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
6
0
    my ( $self ) = @_;
33
34
6
0
    my $config = {};
35
36
6
0
    $config = merge( $self->_global_config, $config );
37
6
0
    $config = merge( $self->_local_config, $config );
38
6
0
    $config = merge( $self->args, $config );
39
40
6
15625
    return $config;
41}
42
43sub _local_config {
44
6
0
    my ( $self ) = @_;
45
46
6
0
    return $self->_merged_conf_from( $self->conf_file );
47}
48
49sub _global_config {
50
6
0
    my ( $self ) = @_;
51
52
6
0
    return $self->_merged_conf_from( $self->global_conf_file );
53}
54
55sub _merged_conf_from {
56
12
0
    my ( $self, $file ) = @_;
57
58
12
0
    return {} if !$file or !-e $file;
59
60
7
0
    my $conf = Config::INI::Reader->read_file( $file );
61
62
7
0
    my $merge;
63
7
21
0
0
    my @sections = grep { defined } ( '_', $self->command, $self->_profile_section );
64
7
0
    for ( @sections ) {
65
21
0
        next if !$conf->{$_};
66
15
0
        $merge = merge( $conf->{$_}, $merge );
67    }
68
69
7
0
    return $merge;
70}
71
72sub _profile_section {
73
7
0
    my ( $self ) = @_;
74
75
7
0
    my $section = $self->command;
76
7
0
    $section .= '::' . $self->profile if $self->profile;
77
78
7
0
    return $section;
79}
80
811;