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

# File Coverage

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

line | stmt | bran | cond | sub | pod | time | code
--- | --- | --- | --- | --- | --- | --- | ---
1 | 2 2 2 |  |  | 2 |  | 0 0 0 | use strict;
2 | 2 2 2 |  |  | 2 |  | 0 0 0 | use warnings;
3 |
4 |  |  |  |  |  |  | package Code::Statistics::Config;
5 |
6 |  |  |  |  |  |  | # ABSTRACT: merges configuration options from various sources
7 |
8 | 2 2 2 |  |  | 2 |  | 0 0 0 | use Moose;
9 | 2 2 2 |  |  | 2 |  | 0 0 0 | use MooseX::HasDefaults::RO;
10 |
11 | 2 2 2 |  |  | 2 |  | 0 0 0 | use Hash::Merge qw( merge );
12 | 2 2 2 |  |  | 2 |  | 0 0 0 | use Config::INI::Reader;
13 | 2 2 2 |  |  | 2 |  | 0 0 0 | use Path::Class qw(file);
14 | 2 2 2 |  |  | 2 |  | 0 0 0 | use File::HomeDir;
15 |
16 |  |  |  |  |  |  | has args => ( isa => 'HashRef', default => sub {{}} );
17 |
18 |  |  |  |  |  |  | has command => ( isa => 'Str', );
19 |
20 |  |  |  |  |  |  | has conf_file => ( isa => 'Str', default => '.codestatrc' );
21 |
22 |  |  |  |  |  |  | has global_conf_file => ( isa => 'Str', default => sub { file( File::HomeDir->my_home, '.codestatrc' )->stringify } );
23 |
24 |  |  |  |  |  |  | has 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 |
31 |  |  |  |  |  |  | sub assemble {
32 | 12 |  |  | 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 |
43 |  |  |  |  |  |  | sub _local_config {
44 | 12 |  |  | 12 |  | 0 | my ( $self ) = @_;
45 |
46 | 12 |  |  |  |  | 0 | return $self->_merged_conf_from( $self->conf_file );
47 |  |  |  |  |  |  | }
48 |
49 |  |  |  |  |  |  | sub _global_config {
50 | 12 |  |  | 12 |  | 0 | my ( $self ) = @_;
51 |
52 | 12 |  |  |  |  | 0 | return $self->_merged_conf_from( $self->global_conf_file );
53 |  |  |  |  |  |  | }
54 |
55 |  |  |  |  |  |  | sub _merged_conf_from {
56 | 24 |  |  | 24 |  | 0 | my ( $self, $file ) = @_;
57 |
58 | 24 | 100 | 100 |  |  | 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 | 100 |  |  |  | 0 | next if !$conf->{$_};
66 | 30 |  |  |  |  | 0 | $merge = merge( $conf->{$_}, $merge );
67 |  |  |  |  |  |  | }
68 |
69 | 14 |  |  |  |  | 0 | return $merge;
70 |  |  |  |  |  |  | }
71 |
72 |  |  |  |  |  |  | sub _profile_section {
73 | 14 |  |  | 14 |  | 0 | my ( $self ) = @_;
74 |
75 | 14 |  |  |  |  | 0 | my $section = $self->command;
76 | 14 | 100 |  |  |  | 0 | $section .= '::' . $self->profile if $self->profile;
77 |
78 | 14 |  |  |  |  | 0 | return $section;
79 |  |  |  |  |  |  | }
80 |
81 |  |  |  |  |  |  | 1;