File Coverage

blib/lib/Config/KingKong/File.pm
Criterion Covered Total %
statement 9 58 15.5
branch 0 16 0.0
condition 0 6 0.0
subroutine 3 7 42.8
pod 1 4 25.0
total 13 91 14.2


line stmt bran cond sub pod time code
1             # Copyright (c) 2011 RaphaĆ«l Pinson.
2             #
3             # This library is free software; you can redistribute it and/or
4             # modify it under the terms of the GNU Lesser Public License as
5             # published by the Free Software Foundation; either version 2.1 of
6             # the License, or (at your option) any later version.
7             #
8             # Config-Model is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11             # Lesser Public License for more details.
12             #
13             # You should have received a copy of the GNU Lesser Public License
14             # along with Config-Model; if not, write to the Free Software
15             # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16             # 02110-1301 USA
17              
18             package Config::KingKong::File;
19              
20 1     1   841 use strict;
  1         3  
  1         40  
21 1     1   7 use warnings;
  1         2  
  1         34  
22 1     1   16 use base qw(Class::Accessor);
  1         2  
  1         930  
23              
24             sub new {
25 0     0 1   my $class = shift;
26 0           my %options = @_;
27              
28 0           my $self = __PACKAGE__->SUPER::new();
29              
30 0           $self->{engine} = $options{engine};
31 0           $self->{directory} = $options{directory};
32 0           $self->{target} = $options{target};
33 0           $self->{template} = $options{template};
34 0           $self->{template_extension} = $options{template_extension};
35 0           $self->{verbose} = $options{verbose};
36 0           $self->{environment} = $options{environment};
37              
38 0           return $self;
39             }
40              
41              
42             sub process {
43 0     0 0   my ($self) = @_;
44              
45 0           my @matches = $self->{target} =~ /\${(\w+)}/g;
46 0           $self->{file_vars} = \@matches;
47 0 0         if ($#matches >= 0) {
48 0           $self->recurse_file(0);
49             } else {
50 0           $self->generate();
51             }
52             }
53              
54             sub recurse_file {
55 0     0 0   my ($self, $level) = @_;
56 0           my @file_vars = @{$self->{file_vars}};
  0            
57 0 0         if ($level <= $#file_vars) {
58 0           my $newmatch = $file_vars[$level];
59 0           my $newkey = $newmatch . "s";
60 0 0         die "E: No such key $newkey" unless $self->{environment}{$newkey};
61 0           my @items;
62 0 0         if (ref($self->{environment}{$newkey}) eq "HASH") {
    0          
63 0           @items = keys %{$self->{environment}{$newkey}};
  0            
64             } elsif (ref($self->{environment}{$newkey}) eq "ARRAY") {
65 0           @items = @{$self->{environment}{$newkey}};
  0            
66             } else {
67 0           @items = ( $self->{environment}{$newkey} );
68             }
69 0           foreach my $item (@items) {
70 0           $self->{environment}{local_values}{$newmatch} = $item;
71 0           $self->recurse_file($level+1);
72             }
73             } else {
74 0           my $filename = $self->{target};
75 0           foreach my $key (keys %{$self->{environment}{local_values}}) {
  0            
76 0           my $value = $self->{environment}{local_values}{$key};
77 0           $filename =~ s|\${$key}|$value|;
78             }
79 0 0         print "V: Filename: $filename\n" if $self->{verbose};
80 0           $self->generate($filename);
81             }
82             }
83              
84             sub generate {
85 0     0 0   my ($self, $file) = @_;
86              
87 0           my $tmpl = $self->{template};
88 0           my $tmpl_ext = $self->{template_extension};
89 0   0       $file ||= $self->{target};
90 0   0       $tmpl ||= "$file.$tmpl_ext";
91 0           my $dirname = $self->{directory};
92 0 0         print "V: Generating $dirname/$file from $dirname/$tmpl\n" if $self->{verbose};
93 0 0         $self->{engine}->process($tmpl, $self->{environment}, $file)
94             || die $self->{engine}->error();
95             }
96              
97             1;