root/trunk/plagger/lib/Plagger/ConfigLoader.pm

Revision 1327 (checked in by miyagawa, 14 years ago)

refactored load_include and load_recipes to Plagger::ConfigLoader?

Line 
1 package Plagger::ConfigLoader;
2 use strict;
3 use Carp;
4
5 sub new {
6     my $class = shift;
7     bless {}, $class;
8 }
9
10 sub load {
11     my($self, $stuff) = @_;
12
13     my $config;
14     if (-e $stuff && -r _) {
15         $config = YAML::LoadFile($stuff);
16         $self->{config_path} = $stuff;
17     } elsif (ref($stuff) && ref($stuff) eq 'SCALAR') {
18         $config = YAML::Load(${$stuff});
19     } elsif (ref($stuff) && ref($stuff) eq 'HASH') {
20         $config = Storable::dclone($stuff);
21     } else {
22         croak "Plagger::ConfigLoader->load: $stuff: $!";
23     }
24
25     return $config;
26 }
27
28 sub load_include {
29     my($self, $config) = @_;
30
31     return unless $config->{include};
32     for (@{ $config->{include} }) {
33         my $include = YAML::LoadFile($_);
34
35         for my $key (keys %{ $include }) {
36             my $add = $include->{$key};
37             unless ($config->{$key}) {
38                 $config->{$key} = $add;
39                 next;
40             }
41             if (ref($config->{$key}) eq 'HASH') {
42                 next unless ref($add) eq 'HASH';
43                 for (keys %{ $include->{$key} }) {
44                     $config->{$key}->{$_} = $include->{$key}->{$_};
45                 }
46             } elsif (ref($include->{$key}) eq 'ARRAY') {
47                 $add = [ $add ] unless ref($add) eq 'ARRAY';
48                 push(@{ $config->{$key} }, @{ $include->{$key} });
49             } elsif ($add) {
50                 $config->{$key} = $add;
51             }
52         }
53     }
54 }
55
56 sub load_recipes {
57     my($self, $config) = @_;
58
59     for (@{ $config->{recipes} }) {
60         $self->error("no such recipe to $_") unless $config->{define_recipes}->{$_};
61         my $plugin = $config->{define_recipes}->{$_};
62         $plugin = [ $plugin ] unless ref($plugin) eq 'ARRAY';
63         push(@{ $config->{plugins} }, @{ $plugin });
64     }
65 }
66
67 1;
Note: See TracBrowser for help on using the browser.