File Coverage

blib/lib/Mojito/Role/Config.pm
Criterion Covered Total %
statement 47 49 95.9
branch 7 16 43.7
condition 5 8 62.5
subroutine 9 9 100.0
pod 2 2 100.0
total 70 84 83.3


line stmt bran cond sub pod time code
1 1     1   636 use strictures 1;
  1         6  
  1         26  
2             package Mojito::Role::Config;
3             {
4             $Mojito::Role::Config::VERSION = '0.24';
5             }
6 1     1   100 use Moo::Role;
  1         2  
  1         6  
7 1     1   1443 use MooX::Types::MooseLike::Base qw(HashRef);
  1         8033  
  1         90  
8 1     1   889 use Dir::Self;
  1         422  
  1         6  
9 1     1   776 use Path::Class qw(file);
  1         104421  
  1         74  
10 1     1   10 use Data::Dumper::Concise;
  1         2  
  1         545  
11              
12             has 'config' => (
13             is => 'rw',
14             isa => HashRef,
15             lazy => 1,
16             builder => '_build_config',
17             );
18              
19             =head2 _build_config
20              
21             Construct the configuration file.
22             Config file is looked for in three locations:
23              
24             ENV
25             lib/Mojito/conf/mojito_local.conf
26             lib/Mojito/conf/mojito.conf
27              
28             The values will be merged with the precedent order being:
29             ENV over
30             mojito_local.conf over
31             mojito.conf
32              
33             =cut
34              
35             sub _build_config {
36 1     1   1873 my ($self) = @_;
37              
38 1 50       6 warn "BUILD CONFIG" if $ENV{MOJITO_DEBUG};
39 1         8 my $conf_file = file(__DIR__ . '/../conf/mojito.conf');
40 1         402 $conf_file->cleanup;
41 1 50       177 $conf_file->resolve if (-e $conf_file);
42              
43 1         527 my $local_conf_file = file(__DIR__ . '/../conf/mojito_local.conf');
44 1         104 $local_conf_file->cleanup;
45 1 50       125 $local_conf_file->resolve if (-e $local_conf_file);
46              
47 1         64 my $env_conf_file = $ENV{MOJITO_CONFIG};
48 1 50 33     6 warn "ENV CONFIG: $ENV{MOJITO_CONFIG}" if ($ENV{MOJITO_DEBUG} and $ENV{MOJITO_CONFIG});
49              
50 1         5 my $conf = $self->read_config($conf_file);
51 1         4 my $local_conf = $self->read_config($local_conf_file);
52 1         5 my $env_conf = $self->read_config($env_conf_file);
53              
54             # The merge happens in pairs
55 1         6 my $merged_conf = $self->merge_hash($local_conf, $conf);
56 1         4 $merged_conf = $self->merge_hash($env_conf, $merged_conf);
57 1         37 return $merged_conf;
58             }
59              
60             =head2 read_config
61              
62             Args: a configuration file name
63             Returns: a HashRef of configuration values
64              
65             =cut
66              
67             sub read_config {
68 3     3 1 7 my ($self, $conf_file) = @_;
69              
70 3         5 my $config = {};
71 3 100 100     11 if ( $conf_file && -r $conf_file ) {
72 1 50       91 if ( not $config = do $conf_file ) {
73 0 0       0 die qq/Can't do config file "$conf_file" EXCEPTION: $@/ if $@;
74 0 0       0 die qq/Can't do config file "$conf_file" UNDEFINED: $!/ if not defined $config;
75             }
76             }
77              
78             # Let's add in the version number.
79 3   50     94 $config->{VERSION} = $Mojito::Role::Config::VERSION || 'development version';
80              
81 3         8 return $config;
82             }
83              
84             =head2 merge_hash
85              
86             Args: ($hash_ref_dominant, $hash_ref_subordinate)
87             Returns: HashRef of the two merged with the dominant values
88             chosen when they exist otherwise the subordinate values are used.
89              
90             =cut
91              
92             sub merge_hash {
93 2     2 1 4 my ($self, $precedent, $subordinate) = @_;
94 2         3 my @not = grep !exists $precedent->{$_}, keys %{$subordinate};
  2         16  
95 2         23 @{$precedent}{@not} = @{$subordinate}{@not};
  2         7  
  2         7  
96 2         6 return $precedent;
97             }
98              
99              
100             1;