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