File Coverage

blib/lib/Amon2/Config/Simple.pm
Criterion Covered Total %
statement 12 23 52.1
branch 0 8 0.0
condition 0 2 0.0
subroutine 4 5 80.0
pod 0 1 0.0
total 16 39 41.0


line stmt bran cond sub pod time code
1             use strict;
2 25     25   178 use warnings;
  25         52  
  25         735  
3 25     25   127 use File::Spec;
  25         61  
  25         630  
4 25     25   142 use Carp ();
  25         57  
  25         481  
5 25     25   172  
  25         77  
  25         5521  
6             my ($class, $c) = (shift, shift);
7             my %conf = @_ == 1 ? %{$_[0]} : @_;
8 0     0 0    
9 0 0         my $env = $conf{environment} || $c->mode_name || 'development';
  0            
10             my $fname = File::Spec->catfile($c->base_dir, 'config', "${env}.pl");
11 0   0       my $config = do $fname;
12 0           Carp::croak("$fname: $@") if $@;
13 0           Carp::croak("$fname: $!") unless defined $config;
14 0 0         unless ( ref($config) eq 'HASH' ) {
15 0 0         Carp::croak("$fname does not return HashRef.");
16 0 0         }
17 0           return $config;
18             }
19 0            
20             1;
21              
22             =encoding utf-8
23              
24             =head1 NAME
25              
26             Amon2::Config::Simple - Default configuration file loader
27              
28             =head1 SYNOPSIS
29              
30             package MyApp2;
31             # do "config/@{{ $c->mode_name ]}.pl"
32             use Amon2::Config::Simple;
33             sub load_config { Amon2::Config::Simple->load(shift) }
34              
35             =head1 DESCRIPTION
36              
37             This is a default configuration file loader for L<Amon2>.
38              
39             This module loads the configuration by C<< do >> function. Yes, it's just plain perl code structure.
40              
41             Amon2 using configuration file in C<< "config/@{[ $c->mode_name ]}.pl" >>.
42              
43             =head1 HOW DO YOU USE YOUR OWN ENVIRONMENT VARIABLE FOR DETECTING CONFIGURATION FILE?
44              
45             If you want to use C<< config/$ENV{RUN_MODE}.pl >> for the configuration file, you can write code as following:
46              
47             package MyApp;
48             use Amon2::Config::Simple;
49             sub load_config { Amon2::Config::Simple->load(shift, +{ environment => $ENV{RUN_MODE} } ) }
50