File Coverage

blib/lib/App/Environ/Config.pm
Criterion Covered Total %
statement 47 47 100.0
branch 7 12 58.3
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 68 73 93.1


line stmt bran cond sub pod time code
1             package App::Environ::Config;
2              
3 3     3   4070 use 5.008000;
  3         7  
4 3     3   11 use strict;
  3         4  
  3         60  
5 3     3   11 use warnings;
  3         3  
  3         127  
6              
7             our $VERSION = '0.18';
8              
9 3     3   11 use App::Environ;
  3         11  
  3         57  
10 3     3   1498 use Config::Processor;
  3         39886  
  3         112  
11 3     3   24 use Carp qw( croak );
  3         6  
  3         1227  
12              
13             my @REGD_CONFIG_SECTIONS;
14             my %CONFIG_SECTIONS_IDX;
15             my $CONFIG;
16             my $NEED_CONFIG_INIT = 1;
17              
18             App::Environ->register( __PACKAGE__,
19             initialize => sub { __PACKAGE__->_initialize(@_) },
20             reload => sub { __PACKAGE__->_reload(@_) },
21             'finalize:r' => sub { __PACKAGE__->_finalize(@_) },
22             );
23              
24              
25             sub register {
26 2     2 1 991 my $class = shift;
27 2         5 my @config_sections = @_;
28              
29 2         4 foreach my $config_section (@config_sections) {
30 2 50       5 next if exists $CONFIG_SECTIONS_IDX{$config_section};
31              
32 2         4 $CONFIG_SECTIONS_IDX{$config_section} = 1;
33 2         3 push( @REGD_CONFIG_SECTIONS, $config_section );
34              
35 2         3 $NEED_CONFIG_INIT = 1;
36             }
37              
38 2         4 return;
39             }
40              
41             sub instance {
42 5 100   5 1 400 unless ( defined $CONFIG ) {
43 1         83 croak __PACKAGE__ . ' must be initialized first';
44             }
45              
46 4         8 return $CONFIG;
47             }
48              
49             sub _initialize {
50 1     1   2 my $class = shift;
51              
52 1 50       2 return unless $NEED_CONFIG_INIT;
53              
54 1         4 $class->_load;
55 1         2 undef $NEED_CONFIG_INIT;
56              
57 1         3 return;
58             }
59              
60             sub _reload {
61 2     2   3 my $class = shift;
62              
63 2         3 $class->_load;
64              
65 2         5 return;
66             }
67              
68             sub _finalize {
69 1     1   3 undef $CONFIG;
70 1         2 $NEED_CONFIG_INIT = 1;
71              
72 1         2 return;
73             }
74              
75             sub _load {
76 3     3   3 my @config_dirs;
77 3 50       8 if ( defined $ENV{APPCONF_DIRS} ) {
78 3         11 @config_dirs = split /:/, $ENV{APPCONF_DIRS};
79             }
80 3         5 my $interpolate_vars = $ENV{APPCONF_INTERPOLATE_VARIABLES};
81 3         3 my $process_directives = $ENV{APPCONF_PROCESS_DIRECTIVES};
82              
83 3 50       24 my $config_processor = Config::Processor->new(
    50          
84             dirs => \@config_dirs,
85             $interpolate_vars ? ( interpolate_variables => $interpolate_vars ) : (),
86             $process_directives ? ( process_directives => $process_directives ) : (),
87             );
88              
89 3         103 $CONFIG = $config_processor->load(@REGD_CONFIG_SECTIONS);
90              
91 3         1559 return;
92             }
93              
94             1;
95             __END__