File Coverage

blib/lib/Arepa/Config.pm
Criterion Covered Total %
statement 60 62 96.7
branch 8 12 66.6
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 84 90 93.3


line stmt bran cond sub pod time code
1             package Arepa::Config;
2              
3 5     5   582660 use Carp qw(croak);
  5         10  
  5         1397  
4 5     5   30 use File::Basename;
  5         9  
  5         409  
5 5     5   37 use File::Spec;
  5         7  
  5         102  
6 5     5   3786 use YAML::Syck;
  5         9040  
  5         4172  
7              
8             sub new {
9 2     2 1 798 my ($class, $path, %user_opts) = @_;
10 2         202 my %opts = (builder_config_dir => File::Spec->catfile(dirname($path),
11             'builders'),
12             %user_opts);
13              
14 2         18 my $self = bless {
15             config => LoadFile($path),
16             }, $class;
17              
18             # Now, load the builder configuration
19 2 50       684 opendir D, $opts{builder_config_dir} or
20             croak "Can't read directory $opts{builder_config_dir}";
21 2         61 my @builder_config_files = grep /\.yml$/, readdir D;
22 2         30 closedir D;
23 2         16 $self->{config}->{builders} = [];
24 2         7 foreach my $file (@builder_config_files) {
25 8         14 my $name = $file;
26 8         37 $name =~ s/\.yml$//;
27 8         96 my $path = File::Spec->catfile($opts{builder_config_dir}, $file);
28 8         31 my $builder_conf = LoadFile($path);
29 8         1400 push @{$self->{config}->{builders}}, {%$builder_conf,
  8         85  
30             name => $name};
31             }
32              
33 2         13 return $self;
34             }
35              
36             sub key_exists {
37 2     2 1 469 my ($self, $key) = @_;
38 2         4 my $exists = 0;
39              
40             # If get_key dies, the key doesn't exist
41 2         3 eval {
42 2         6 $self->get_key($key);
43 1         2 $exists = 1;
44             };
45              
46 2         44 return $exists;
47             }
48              
49             sub get_key {
50 6     6 1 20 my ($self, $key) = @_;
51              
52 6         30 my @keys = split(':', $key);
53 6         12 my $value = $self->{config};
54 6         14 foreach my $k (@keys) {
55 10 100       26 if (defined $value->{$k}) {
56 8         24 $value = $value->{$k};
57             }
58             else {
59 2         313 croak "Can't find configuration key $key (no $k)";
60             }
61             }
62 4         57 return $value;
63             }
64              
65             sub get_builders {
66 1     1 1 8 my ($self, %user_opts) = @_;
67 1         3 my %opts = (%user_opts);
68              
69 1         4 my @builders = @{$self->{config}->{builders}};
  1         6  
70 1 50       6 if (exists $opts{type}) {
71 0         0 @builders = grep { $_->{type} eq $opts{type} }
  0         0  
72             @builders;
73             }
74 1         3 return map { $_->{name} } @builders;
  4         21  
75             }
76              
77             sub get_builder_config {
78 5     5 1 8150 my ($self, $builder_name) = @_;
79              
80 5         17 my $builder_config = $self->{config}->{builders};
81 5         9 my @matching_builders = grep { $_->{name} eq $builder_name }
  20         62  
82             @$builder_config;
83 5 50       25 scalar(@matching_builders) == 0 and
84             croak "Don't know builder '$builder_name'";
85 5 50       16 scalar(@matching_builders) > 1 and
86             croak "There is more than one builder called '$builder_name'";
87 5         9 return %{$matching_builders[0]};
  5         49  
88             }
89              
90             sub get_builder_config_key {
91 2     2 1 6 my ($self, $builder_name, $config_key) = @_;
92              
93 2         8 my %builder_config = $self->get_builder_config($builder_name);
94 2 100       192 defined($builder_config{$config_key}) or
95             croak "'$builder_name' doesn't have a configuration key $config_key";
96 1         11 return $builder_config{$config_key};
97             }
98              
99             1;
100              
101             __END__