File Coverage

lib/Ubic/ServiceLoader/Ext/ini.pm
Criterion Covered Total %
statement 32 33 96.9
branch 9 12 75.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 51 55 92.7


line stmt bran cond sub pod time code
1             package Ubic::ServiceLoader::Ext::ini;
2             $Ubic::ServiceLoader::Ext::ini::VERSION = '1.59';
3             # ABSTRACT: loader for ini-style configs
4              
5              
6 3     3   24802 use strict;
  3         4  
  3         68  
7 3     3   9 use warnings;
  3         4  
  3         59  
8              
9 3     3   9 use parent qw( Ubic::ServiceLoader::Base );
  3         3  
  3         11  
10              
11 3     3   1177 use Config::Tiny;
  3         2084  
  3         1278  
12              
13             sub new {
14 2     2 1 16 my $class = shift;
15 2         7 return bless {} => $class;
16             }
17              
18             sub load {
19 6     6 1 6 my $self = shift;
20 6         7 my ($file) = @_;
21              
22 6         28 my $config = Config::Tiny->read($file);
23 6 100       669 unless ($config) {
24 1         6 die Config::Tiny->errstr;
25             }
26              
27 5         8 my $root_section = delete $config->{_};
28 5   100     19 my $module = delete $root_section->{module} || 'Ubic::Service::SimpleDaemon';
29 5 100       14 if (keys %$root_section) {
30 1         16 die "Unknown option ".join(', ', keys %$root_section)." in file $file";
31             }
32              
33 4         6 my $options = delete $config->{options};
34 4 100       12 if (keys %$config) {
35 1         15 die "Unknown section ".join(', ', keys %$config)." in file $file";
36             }
37              
38 3 50       14 $module =~ /^[\w:]+$/ or die "Invalid module name '$module'";
39 3         128 eval "require $module"; # TODO - Class::Load?
40 3 50       13 if ($@) {
41 0         0 die $@;
42             }
43              
44 3         6 my @options = ();
45 3 50       8 @options = ($options) if $options; # some modules can have zero options, I guess
46 3         15 return $module->new(@options);
47             }
48              
49             1;
50              
51             __END__