File Coverage

blib/lib/Config/ZOMG.pm
Criterion Covered Total %
statement 51 54 94.4
branch 20 22 90.9
condition 5 6 83.3
subroutine 11 12 91.6
pod 4 5 80.0
total 91 99 91.9


line stmt bran cond sub pod time code
1             package Config::ZOMG;
2             {
3             $Config::ZOMG::VERSION = '1.000000';
4             }
5              
6             # ABSTRACT: Yet Another Catalyst::Plugin::ConfigLoader-style layer over Config::Any
7              
8 11     11   366013 use Moo;
  11         211296  
  11         75  
9 11     11   34472 use Sub::Quote 'quote_sub';
  11         52808  
  11         941  
10              
11 11     11   8419 use Config::ZOMG::Source::Loader;
  11         39  
  11         448  
12              
13 11     11   100 use Config::Any;
  11         21  
  11         314  
14 11     11   13213 use Hash::Merge::Simple;
  11         6138  
  11         8178  
15              
16             has package => (
17             is => 'ro',
18             );
19              
20             has source => (
21             is => 'rw',
22             handles => [qw/ driver local_suffix no_env env_lookup path found find /],
23             );
24              
25             has load_once => (
26             is => 'ro',
27             default => quote_sub q{ 1 },
28             );
29              
30             has loaded => (
31             is => 'rw',
32             default => quote_sub q{ 0 },
33             );
34              
35             has default => (
36             is => 'ro',
37             default => quote_sub q[ {} ],
38             );
39              
40             has path_to => (
41             is => 'ro',
42             reader => '_path_to',
43             builder => '_build_path_to',
44             lazy => 1,
45             );
46              
47             sub _build_path_to {
48 1     1   532 my $self = shift;
49 1 50       5 return $self->load->{home} if $self->load->{home};
50 1 50       30 return $self->source->path unless $self->source->path_is_file;
51 0         0 return '.';
52             }
53              
54             has _config => (
55             is => 'rw',
56             );
57              
58             sub BUILD {
59 19     19 0 53681 my $self = shift;
60 19         45 my $given = shift;
61              
62 19         64 my ($source, %source);
63 19 100       106 if ($given->{file}) {
64              
65 8         27 $given->{path} = $given->{file};
66 8         68 $source{path_is_file} = 1;
67             }
68              
69             {
70 19         42 for (qw/
  19         202  
71             name
72             path
73             driver
74              
75             no_local
76             local_suffix
77              
78             no_env
79             env_lookup
80              
81             /) {
82 133 100       397 $source{$_} = $given->{$_} if exists $given->{$_};
83             }
84              
85 19 100 100     139 warn "Warning, 'local_suffix' will be ignored if 'file' is given, use 'path' instead" if
86             exists $source{local_suffix} && exists $given->{file};
87              
88 19         478 $source = Config::ZOMG::Source::Loader->new( %source );
89             }
90              
91 19         609 $self->source($source);
92             }
93              
94             sub open {
95 9 100   9 1 9782 unless ( ref $_[0] ) {
96 4         11 my $class = shift;
97 4 100       130 return $class->new( @_ == 1 ? (file => $_[0]) : @_ )->open;
98             }
99 5         11 my $self = shift;
100 5 100       30 warn "You called ->open on an instantiated object with arguments" if @_;
101 5         85 my $config_hash = $self->load;
102 5 100       132 return unless $self->found;
103 4 100       37 return wantarray ? ($config_hash, $self) : $config_hash;
104             }
105              
106             sub load {
107 59     59 1 3241 my $self = shift;
108              
109 59 100 66     638 return $self->_config if $self->loaded && $self->load_once;
110              
111 18         98 $self->_config($self->default);
112              
113 18         105 $self->_load($_) for $self->source->read;
114              
115 18         995 $self->loaded(1);
116              
117 18         143 return $self->_config;
118             }
119              
120             sub clone {
121 0     0 1 0 require Clone;
122 0         0 Clone::clone($_[0]->config)
123             }
124              
125             sub reload {
126 1     1 1 2 my $self = shift;
127 1         5 $self->loaded(0);
128 1         3 return $self->load;
129             }
130              
131             sub _load {
132 20     20   1372 my $self = shift;
133 20         36 my $cfg = shift;
134              
135 20         53 my ($file, $hash) = %$cfg;
136              
137 20         210 $self->_config(Hash::Merge::Simple->merge($self->_config, $hash));
138             }
139              
140             1;
141              
142             __END__