File Coverage

blib/lib/Mojolicious/Plugin/ConfigHashMerge.pm
Criterion Covered Total %
statement 23 24 95.8
branch 7 16 43.7
condition 4 17 23.5
subroutine 4 4 100.0
pod 1 1 100.0
total 39 62 62.9


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::ConfigHashMerge;
2 1     1   962 use Mojo::Base 'Mojolicious::Plugin::Config';
  1         2  
  1         7  
3 1     1   2854 use Hash::Merge::Simple qw( merge );
  1         569  
  1         70  
4 1     1   17 use File::Spec::Functions 'file_name_is_absolute';
  1         2  
  1         406  
5              
6             our $VERSION = '0.02';
7              
8             sub register {
9 1     1 1 49 my ($self, $app, $conf) = @_;
10              
11             # Config file
12 1   33     5 my $file = $conf->{file} || $ENV{MOJO_CONFIG};
13 1   0     3 $file ||= $app->moniker . '.' . ($conf->{ext} || 'conf');
      33        
14              
15             # Mode specific config file
16 1 50       13 my $mode = $file =~ /^(.*)\.([^.]+)$/ ? join('.', $1, $app->mode, $2) : '';
17              
18 1         18 my $home = $app->home;
19 1 50       11 $file = $home->rel_file($file) unless file_name_is_absolute $file;
20 1 50 33     19 $mode = $home->rel_file($mode) if $mode && !file_name_is_absolute $mode;
21 1 50 33     48 $mode = undef unless $mode && -e $mode;
22              
23             # Read config file
24 1         2 my $config = {};
25 1 50 0     24 if (-e $file) { $config = $self->load($file, $conf, $app) }
  1 0       8  
26              
27             # Check for default and mode specific config file
28             elsif (!$conf->{default} && !$mode) {
29 0         0 die qq{Configuration file "$file" missing, maybe you need to create it?\n};
30             }
31              
32             # Merge everything
33 1 50       876 $config = merge($config, $self->load($mode, $conf, $app)) if $mode;
34 1 50       6 $config = merge($conf->{default}, $config) if $conf->{default};
35 1         75 return $app->defaults(config => $app->config)->config($config)->config;
36             }
37             1;
38             __END__