File Coverage

blib/lib/Archer/ConfigLoader.pm
Criterion Covered Total %
statement 44 49 89.8
branch 5 14 35.7
condition 4 18 22.2
subroutine 12 12 100.0
pod 0 2 0.0
total 65 95 68.4


line stmt bran cond sub pod time code
1             package Archer::ConfigLoader;
2 6     6   34 use strict;
  6         10  
  6         431  
3 6     6   33 use warnings;
  6         12  
  6         165  
4 6     6   7719 use Storable;
  6         27023  
  6         455  
5 6     6   58 use Carp;
  6         14  
  6         424  
6 6     6   8005 use Kwalify qw(validate);
  6         74010  
  6         611  
7 6     6   6861 use Path::Class;
  6         394642  
  6         520  
8 6     6   6235 use FindBin;
  6         6755  
  6         304  
9 6     6   6112 use File::ShareDir qw/dist_dir/;
  6         49617  
  6         504  
10 6     6   66 use File::Spec;
  6         11  
  6         3040  
11              
12             my $yaml_class;
13             if (eval "require YAML::Syck; 1;") { ## no critic.
14             $yaml_class = "YAML::Syck";
15             } else {
16             require YAML;
17             $yaml_class = "YAML";
18             }
19              
20 7     7 0 30 sub new { bless {}, shift }
21              
22             sub load {
23 7     7 0 18 my ( $self, $stuff, $context ) = @_;
24              
25 7         46 $context->log('debug' => "yaml class: $yaml_class");
26              
27             # load
28 7         94 my $config;
29 7 50 33     299 if ( ( !ref($stuff) && $stuff eq '-' )
    0 33        
    0 33        
      0        
      0        
30             || ( -e $stuff && -r _ ) )
31             {
32 7         124 $config = $yaml_class->can('LoadFile')->($stuff);
33 7 50       189922 $context->{config_path} = $stuff if $context;
34             }
35             elsif ( ref($stuff) && ref($stuff) eq 'SCALAR' ) {
36 0         0 $config = $yaml_class->can('Load')->( ${$stuff} );
  0         0  
37             }
38             elsif ( ref($stuff) && ref($stuff) eq 'HASH' ) {
39 0         0 $config = Storable::dclone($stuff);
40             }
41             else {
42 0         0 croak "Archer::ConfigLoader->load: $stuff: $!";
43             }
44              
45             # setup default value
46             $config->{global}->{assets_path} ||= sub {
47 7     7   153 my $dir = File::Spec->catdir( $FindBin::Bin, 'assets');
48 7 50       322 return $dir if -d $dir;
49 7         64 $dir = File::Spec->catdir( $FindBin::Bin, 'share', 'assets');
50 7 50       255 return $dir if -d $dir;
51              
52 0         0 File::Spec->catdir(dist_dir('Archer'), 'assets');
53 7   33     128 }->();
54 7         129 $context->log('debug' => "assets path: $config->{global}->{assets_path}");
55              
56             # verify
57 7         184 my $schema_file = file( $config->{global}->{assets_path}, 'kwalify', 'schema.yaml' );
58 7         2000 my $res = validate( $yaml_class->can('LoadFile')->($schema_file), $config );
59 7 50       475080 $context->log( error => $res ) unless $res == 1;
60              
61 7         261 return $config;
62             }
63              
64             1;