File Coverage

blib/lib/Log/Dispatch/Configurator/Any.pm
Criterion Covered Total %
statement 39 42 92.8
branch 9 12 75.0
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 5 0.0
total 58 72 80.5


line stmt bran cond sub pod time code
1             package Log::Dispatch::Configurator::Any;
2             {
3             $Log::Dispatch::Configurator::Any::VERSION = '1.122640';
4             }
5              
6 5     5   277488 use strict;
  5         16  
  5         221  
7 5     5   30 use warnings FATAL => 'all';
  5         13  
  5         262  
8              
9 5     5   29 use base 'Log::Dispatch::Configurator';
  5         13  
  5         4727  
10 5     5   6973 use Config::Any;
  5         59645  
  5         194  
11 5     5   52 use Carp;
  5         12  
  5         2430  
12              
13             sub new {
14 3     3 0 3272 my($class, $file) = @_;
15 3         8 my $self = {};
16              
17             # allow passing of hashref or filename
18 3 100       15 if (ref $file) {
19 2 100       250 croak "Config must be hashref or filename"
20             if ref $file ne 'HASH';
21              
22 1         6 $self = bless { file => undef, _config => $file }, $class;
23             }
24             else {
25 1         6 $self = bless { file => $file }, $class;
26 1         5 $self->parse_file;
27             }
28              
29 2         13 return bless $self, $class;
30             }
31              
32             sub parse_file {
33 1     1 0 2 my $self = shift;
34 1         9 my $file = $self->{'file'};
35              
36 1         3 my $config = eval{ Config::Any->load_files({
  1         13  
37             files => [$file],
38             use_ext => 1,
39             flatten_to_hash => 1,
40             })->{$file} };
41              
42 1 50 33     15384 croak "Config '$file' does not build a Hash"
43             if $@ or (ref $config ne 'HASH');
44 1         5 $self->{'_config'} = $config;
45             }
46              
47             sub reload {
48 0     0 0 0 my $self = shift;
49 0 0       0 return if !defined $self->{file};
50 0         0 $self->parse_file;
51             }
52              
53             sub get_attrs_global {
54 2     2 0 1648 my $self = shift;
55 2         5 my $attrs;
56              
57 2         3 foreach (keys %{$self->{_config}}) {
  2         11  
58 4 100       21 next if ref $self->{'_config'}->{$_} eq 'HASH';
59 2         8 $attrs->{$_} = $self->{'_config'}->{$_};
60             }
61              
62             # fix for Config::General squashing single item list to a scalar
63 2 100       13 $attrs->{dispatchers} = [$attrs->{dispatchers}]
64             if ! ref $attrs->{dispatchers};
65              
66 2         6 return $attrs;
67             }
68              
69             sub get_attrs {
70 2     2 0 39 my($self, $name) = @_;
71 2         11 return $self->{'_config'}->{$name};
72             }
73              
74             1;
75              
76              
77             # ABSTRACT: Configurator implementation with Config::Any
78              
79              
80             __END__