File Coverage

lib/Code/Statistics/Config.pm
Criterion Covered Total %
statement 48 48 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 13 13 100.0
pod 1 1 100.0
total 71 71 100.0


line stmt bran cond sub pod time code
1 1     1   6 use strict;
  1         1  
  1         35  
2 1     1   5 use warnings;
  1         2  
  1         35  
3              
4             package Code::Statistics::Config;
5             $Code::Statistics::Config::VERSION = '1.190680';
6             # ABSTRACT: merges configuration options from various sources
7              
8 1     1   504 use Moose;
  1         429402  
  1         6  
9 1     1   7135 use MooseX::HasDefaults::RO;
  1         11951  
  1         19  
10              
11 1     1   19041 use Hash::Merge qw( merge );
  1         7760  
  1         73  
12 1     1   572 use Config::INI::Reader;
  1         24114  
  1         41  
13 1     1   375 use Path::Class qw(file);
  1         13430  
  1         66  
14 1     1   541 use File::HomeDir;
  1         4656  
  1         437  
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              
27             sub assemble {
28 6     6 1 26946 my ( $self ) = @_;
29              
30 6         15 my $config = {};
31              
32 6         22 $config = merge( $self->_global_config, $config );
33 6         384 $config = merge( $self->_local_config, $config );
34 6         540 $config = merge( $self->args, $config );
35              
36 6         411 return $config;
37             }
38              
39             sub _local_config {
40 6     6   14 my ( $self ) = @_;
41              
42 6         144 return $self->_merged_conf_from( $self->conf_file );
43             }
44              
45             sub _global_config {
46 6     6   11 my ( $self ) = @_;
47              
48 6         172 return $self->_merged_conf_from( $self->global_conf_file );
49             }
50              
51             sub _merged_conf_from {
52 12     12   22 my ( $self, $file ) = @_;
53              
54 12 100 100     326 return {} if !$file or !-e $file;
55              
56 7         59 my $conf = Config::INI::Reader->read_file( $file );
57              
58 7         7977 my $merge;
59 7         165 my @sections = grep { defined } ( '_', $self->command, $self->_profile_section );
  21         39  
60 7         17 for ( @sections ) {
61 21 100       1089 next if !$conf->{$_};
62 15         30 $merge = merge( $conf->{$_}, $merge );
63             }
64              
65 7         690 return $merge;
66             }
67              
68             sub _profile_section {
69 7     7   14 my ( $self ) = @_;
70              
71 7         111 my $section = $self->command;
72 7 100       122 $section .= '::' . $self->profile if $self->profile;
73              
74 7         16 return $section;
75             }
76              
77             1;
78              
79             __END__
80              
81             =pod
82              
83             =encoding UTF-8
84              
85             =head1 NAME
86              
87             Code::Statistics::Config - merges configuration options from various sources
88              
89             =head1 VERSION
90              
91             version 1.190680
92              
93             =head2 assemble
94             Builds the command-related configuration hash. The hash contains all config
95             options from the global config file, local file and command line arguments.
96              
97             =head1 AUTHOR
98              
99             Christian Walde <mithaldu@yahoo.de>
100              
101             =head1 COPYRIGHT AND LICENSE
102              
103              
104             Christian Walde has dedicated the work to the Commons by waiving all of his
105             or her rights to the work worldwide under copyright law and all related or
106             neighboring legal rights he or she had in the work, to the extent allowable by
107             law.
108              
109             Works under CC0 do not require attribution. When citing the work, you should
110             not imply endorsement by the author.
111              
112             =cut