File Coverage

blib/lib/Resource/Loader.pm
Criterion Covered Total %
statement 77 80 96.2
branch 40 48 83.3
condition 10 21 47.6
subroutine 13 14 92.8
pod 9 9 100.0
total 149 172 86.6


line stmt bran cond sub pod time code
1             # -*- perl -*- --------------------------------------------------
2             #
3             # Resource::Loader - load different resources depending...
4             #
5             # Joshua Keroes
6             #
7             # This number is *not* the $VERSION (see below):
8             # $Id: Loader.pm,v 1.10 2003/04/28 23:28:19 jkeroes Exp $
9              
10             package Resource::Loader;
11              
12 6     6   183276 use strict;
  6         15  
  6         229  
13 6     6   31 use warnings;
  6         10  
  6         153  
14 6     6   33 use Carp;
  6         13  
  6         589  
15 6     6   37 use vars qw/$VERSION/;
  6         8  
  6         6320  
16              
17             $VERSION = '0.03';
18              
19             # In: hash-style args. See docs.
20             # Out: object
21             sub new {
22 9     9 1 186 my $proto = shift;
23 9   33     57 my $class = ref $proto || $proto;
24 9         17 my $self = {};
25              
26 9         23 bless $self, $class;
27              
28 9         31 $self->_init( @_ );
29             }
30              
31             # In: hash-style args. See docs for new()
32             # Out: object
33             sub _init {
34 9     9   15 my $self = shift;
35 9         36 my %args = @_;
36              
37 9         44 while ( my ( $method, $args ) = each %args ) {
38 17         54 $self->$method( $args );
39             }
40              
41 8         595 return $self;
42             }
43              
44              
45             # In: array or arrayref. See docs.
46             # Out: array or arrayref of resources.
47             sub resources {
48 8     8 1 13 my $self = shift;
49              
50 8 50       37 if ( @_ ) {
51 8         29 undef $self->{resources};
52              
53 8 50       35 for ( ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0] ) {
  8         30  
54 15 50 33     121 croak "Malformed resource. Needs 'name', 'when', and 'what' args"
      33        
55             unless defined $_->{name}
56             && defined $_->{when}
57             && defined $_->{what};
58              
59 15 50 33     89 croak "Malformed resource. 'when' and 'what' need to be coderefs."
60             unless ref $_->{what} eq "CODE"
61             && ref $_->{when} eq "CODE";
62              
63 15 50 66     115 croak "Malformed resource. 'whatargs' needs to be an arrayref."
64             if $_->{whatargs}
65             && ref $_->{whatargs} ne "ARRAY";
66              
67 15 100 100     633 croak "Malformed resource. 'whenargs' needs to be an arrayref."
68             if $_->{whenargs}
69             && ref $_->{whenargs} ne "ARRAY";
70              
71 14         19 push @{ $self->{resources} }, $_;
  14         44  
72             }
73             }
74              
75 7 50       45 return wantarray ? @{ $self->{resources} } : $self->{resources};
  0         0  
76             }
77              
78             # In: optional new value
79             # Out: current value
80             sub testing {
81 15     15 1 23 my $self = shift;
82 15 100       639 $self->{testing} = shift if @_;
83 15 100       81 return defined $ENV{RMTESTING} ? $ENV{RMTESTING} : $self->{testing};
84             }
85              
86             # In: optional new value
87             # Out: current value
88             sub verbose {
89 19     19 1 50 my $self = shift;
90 19 100       53 $self->{verbose} = shift if @_;
91 19 100       613 return defined $ENV{RMVERBOSE} ? $ENV{RMVERBOSE} : $self->{verbose};
92             }
93              
94             # In: optional new value
95             # Out: current value
96             sub cont {
97 12     12 1 54 my $self = shift;
98 12 100       38 $self->{cont} = shift if @_;
99 12 100       93 return defined $ENV{RMCONT} ? $ENV{RMCONT} : $self->{cont};
100             }
101              
102             # In: n/a
103             # Out: hashref of our environment variables
104             sub env {
105 0     0 1 0 my $self = shift;
106 0         0 return { RMTESTING => $ENV{RMTESTING},
107             RMVERBOSE => $ENV{RMVERBOSE},
108             RMSTATES => $ENV{RMSTATES},
109             RMCONT => $ENV{RMCONT},
110             };
111             }
112              
113             # In: n/a
114             # Out: hashref of loaded states and their returns values.
115             sub loaded {
116 8     8 1 11 my $self = shift;
117 8         34 return $self->{loaded};
118             }
119              
120             # In: n/a
121             # Out: status report
122             #
123             # Runs the appropriate resources()
124             sub load {
125 8     8 1 13 my $self = shift;
126              
127             # clear out loaded() and status() tables.
128 8         18 undef $self->{loaded};
129 8         15 undef $self->{status};
130 8         13 $self->{status} = { map { $_->{name} => 'inactive' } @{ $self->{resources} } };
  18         59  
  8         25  
131              
132 8         16 for( @{ $self->{resources} } ) {
  8         22  
133 18         31 my $name = $_->{name};
134              
135              
136 18 100       47 if ( defined $ENV{RMSTATES} ) {
137 12 100       28 if ( grep { $_ eq $name } split /:/, $ENV{RMSTATES} ) {
  12         31  
138 4         385 print __PACKAGE__ . " state '$name' present in RMSTATES environment var\n";
139             } else {
140 8         893 print __PACKAGE__ . " state '$name' skipped due to RMSTATES environment var\n";
141 8         91 $self->{status}{$name} = 'skipped';
142 8         21 next;
143             }
144             }
145              
146 10 100       53 if ( $_->{when}->( ref $_->{whenargs} eq "ARRAY"
  2 100       9  
147             ? @{ $_->{whenargs} }
148             : () ) ) {
149 8 100       40 print __PACKAGE__ . " state '$name' active\n" if $self->verbose;
150              
151 8 100       26 if ( $self->testing ) {
152 4 100       24 print __PACKAGE__ . " in testing: won't run code for state '$name'\n" if $self->verbose;
153 4         12 $self->{status}{$name} = 'notrun';
154             } else {
155 1         6 $self->{loaded}{$name} = $_->{what}->( ref $_->{whatargs} eq "ARRAY"
156 4 100       44 ? @{ $_->{whatargs} }
157             : () );
158 4         41 $self->{status}{$name} = 'loaded';
159             }
160              
161 8 100       23 last unless $self->cont;
162             } else {
163 2 50       19 print __PACKAGE__ . " state '$name' inactive\n" if $self->verbose;
164 2         7 $self->{status}{$name} = 'inactive';
165             }
166             }
167              
168 8         25 return $self->loaded;
169             }
170              
171             # In: n/a
172             # Out: status report, e.g. { name => status, ... }
173             sub status {
174 6     6 1 1430 my $self = shift;
175              
176 6 50 33     47 return unless $self->{status}
177             && ref $self->{status} eq "HASH";
178              
179 6         26 return $self->{status};
180             }
181              
182             1;
183              
184             __END__