File Coverage

blib/lib/OpenInteract/Config.pm
Criterion Covered Total %
statement 9 86 10.4
branch 0 22 0.0
condition 0 11 0.0
subroutine 3 14 21.4
pod 0 10 0.0
total 12 143 8.3


line stmt bran cond sub pod time code
1             package OpenInteract::Config;
2              
3             # $Id: Config.pm,v 1.7 2002/04/23 13:01:33 lachoy Exp $
4              
5 1     1   7 use strict;
  1         2  
  1         37  
6 1     1   413 use OpenInteract::Error;
  1         3  
  1         102  
7             require Exporter;
8              
9             # AUTOLOAD not being used any longer... see below
10             #use vars qw( $AUTOLOAD );
11             #$AUTOLOAD = '';
12              
13             @OpenInteract::Config::ISA = qw( Exporter );
14             $OpenInteract::Config::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
15             @OpenInteract::Config::EXPORT_OK = qw( _w DEBUG );
16             my %CONFIG_TYPES = (
17             'perl' => 'OpenInteract::Config::PerlFile',
18             'ini' => 'OpenInteract::Config::IniFile',
19             );
20              
21 1     1   5 use constant DEBUG => 0;
  1         2  
  1         1133  
22              
23             # Interface: subclasses should override these
24              
25 0     0 0   sub read_config { return $_[0]; }
26 0     0 0   sub save_config { return undef; }
27              
28              
29             # Create a new config object. This is a factory method: rather than
30             # creating new objects of the class OpenInteract::Config, we use the
31             # variable $type and create an object based on it.
32              
33             sub instance {
34 0     0 0   my ( $pkg, $type, @params ) = @_;
35              
36             # Backwards compatibility fix -- this probably won't be here forever.
37              
38 0   0       $type ||= 'perl';
39 0           my $class = $CONFIG_TYPES{ $type };
40 0 0         die "No configuration class corresponding to type ($type)" unless ( $class );
41 0           eval "require $class";
42 0 0         if ( $@ ) {
43 0           die "Configuration class ($class) cannot be used. Error: $@";
44             }
45 0           my $data = $class->read_config( @params );
46 0           return bless( $data, $class );
47             }
48              
49              
50             sub read_file {
51 0     0 0   my ( $class, $filename ) = @_;
52 0           DEBUG && _w( 1, "Config trying to read file [$filename]" );
53 0 0         open( CONF, $filename )
54             || die "Cannot open [$filename] for reading: $!\n";
55 0           my @lines = ;
56 0           close( CONF );
57 0           return \@lines;
58             }
59              
60             # Copy items from the default action into all the other actions --
61             # this method doesn't quite belong here but since we don't have
62             # anything dealing strictly with action information... One possibility
63             # is to create a ActionTable module which does stuff like this and
64             # operates on $R
65              
66             sub flatten_action_config {
67 0     0 0   my ( $self ) = @_;
68 0   0       my $default_action = $self->{action_info}{default} || $self->{action}{_default_action_info_};
69 0           my @names = ();
70 0           foreach my $action_key ( keys %{ $self->{action} } ) {
  0            
71 0 0 0       next if ( $action_key eq 'default' or $action_key =~ /^_/ );
72 0           foreach my $def ( keys %{ $default_action } ) {
  0            
73 0   0       $self->{action}{ $action_key }{ $def } ||= $default_action->{ $def };
74             }
75              
76             # Also ensure that the action information knows its own key
77              
78 0           $self->{action}{ $action_key }{name} = $action_key;
79 0           push @names, $action_key;
80             }
81 0           return \@names;
82             }
83              
84              
85             # Set a parameter
86              
87             sub param_set {
88 0     0 0   my ( $self, $config, $value ) = @_;
89 0           $config = lc $config;
90 0 0         $self->{ $config } = $value if ( $value );
91 0           return $self->{ $config };
92             }
93              
94              
95             # Get the value of a key
96              
97             sub get {
98 0     0 0   my ( $self, @p ) = @_;
99 0           my @configs = ();
100 0           foreach my $conf ( @p ) {
101 0           push @configs, $self->param_set( $conf );
102             }
103 0 0         if ( scalar @configs == 1 ) {
104 0           return $configs[0];
105             }
106 0           return @configs;
107             }
108              
109              
110             # Allow you to set multiple values at once
111              
112             sub set {
113 0     0 0   my ( $self, $p ) = @_;
114 0           my %configs = ();
115 0           my $count = 0;
116 0           my $last_conf = ''; # hack to return one value if only one passed in
117 0           foreach my $conf ( keys %{ $p } ) {
  0            
118 0           $configs{ $conf } = $self->param_set( $conf, $p->{ $conf } );
119 0           $last_conf = $conf;
120 0           $count++;
121             }
122 0 0         if ( $count == 1 ) {
123 0           return $configs{ $last_conf };
124             }
125 0           return %configs;
126             }
127              
128              
129             # Do a macro expansion on the directory names -- this SHOULD be done
130             # only once (on read, or by request) and then the information in the
131             # config object would be stable
132              
133             sub get_dir {
134 0     0 0   my ( $self, $dir_tag ) = @_;
135 0           my $dir_hash = $self->{dir};
136 0           $dir_tag =~ s/_dir$//;
137 0           my $dir = $dir_hash->{ lc $dir_tag };
138 0           DEBUG && _w( 1, "get_dir(): start out with <<$dir>>" );
139 0 0         return undef if ( ! $dir );
140 0           while ( $dir =~ m|^\$([\w\_]+)/| ) {
141 0           my $orig_lookup = $1;
142 0           my $lookup_dir = lc $orig_lookup;
143 0           DEBUG && _w( 1, " get_dir(): found lookup dir of <<$lookup_dir>>" );
144 0 0         return undef if ( ! $dir_hash->{ $lookup_dir } );
145 0           $dir =~ s/^\$$orig_lookup/$dir_hash->{ $lookup_dir }/;
146 0           DEBUG && _w( 1, " get_dir(): new directory: <<$dir>>" );
147             }
148 0           return $dir;
149             }
150              
151              
152              
153             sub is_file_valid {
154 0     0 0   my ( $self, $filename ) = @_;
155              
156 0 0         unless ( -f $filename ) {
157 0           my $msg = 'Cannot read configuration file!';
158 0           my $system_msg = "No valid filename ($filename) for reading configuration information!";
159 0           OpenInteract::Error->set({ user_msg => $msg,
160             type => 'config',
161             system_msg => $system_msg,
162             method => 'read_config',
163             extra => { filename => $filename } });
164 0           die $msg;
165             }
166 0           return 1;
167             }
168              
169             # Allow you to call config keys as methods -- we should probably get
170             # rid of this and force you to use it as a hashref...
171              
172             # AUTOLOAD no longer supported -- please email Chris if you have an
173             # issue with this.
174              
175             #sub AUTOLOAD {
176             # my ( $self, @params ) = @_;
177             # my $request = $AUTOLOAD;
178             # $request =~ s/.*://;
179             # DEBUG && _w( 1, "Trying to fulfill request ($request) from AUTOLOAD" );
180             # return $self->param_set( $request, @params);
181             #}
182              
183              
184             sub _w {
185 0 0   0     return unless ( DEBUG >= shift );
186 0           my ( $pkg, $file, $line ) = caller;
187 0           my @ci = caller(1);
188 0           warn "$ci[3] ($line) >> ", join( ' ', @_ ), "\n";
189             }
190              
191             1;
192              
193             __END__