blib/lib/Resource/Loader.pm

filter: Statementstmt 96.3% 79 / 82 Branchbran 84.0% 42 / 50 Conditioncond 47.6% 10 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 92.8% 13 / 14 Podpod 100% 9 / 9 totaltotal 82.0% 155 / 189 CC 35 SCAR 23.3
CC
35
Cov
86%
resources
21.3
load
12.0
status
3.5
CRAP 10.3
↑ Summary
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 $VERSION = '0.03';
18  
19 # In: hash-style args. See docs.
20 # Out: object
21 9 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
22 9 my $proto = shift;
23 9 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
24 9 my $self = {};
25  
26 9 bless $self, $class;
27  
28 9 $self->_init( @_ );
29 }
30  
31 # In: hash-style args. See docs for new()
32 # Out: object
33 9 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
34 9 my $self = shift;
35 9 my %args = @_;
36  
37 9 ▶ while ( my ( $method, $args ) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 17 8 25
38 17 $self->$method( $args );
39 }
40  
41 8 return $self;
42 }
43  
44  
45 # In: array or arrayref. See docs.
46 # Out: array or arrayref of resources.
47 8 ▶sub resources {
Subroutine resources: called CC=14 SCAR=30.6
Pod documented
48 8 my $self = shift;
49  
50 8 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 0 8
51 8 undef $self->{resources};
52  
53 8 ▶ for ( ref $_[0] eq 'ARRAY' ? @{ $_[0] } : $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 8 0 8
54 croak "Malformed resource. Needs 'name', 'when', and 'what' args"
55 unless defined $_->{name}
56 && defined $_->{when}
57 15 ▶ && defined $_->{what};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_->{"name"} and defined $_->{"when"} and defined $_->{"what"} 0 15 15
Condition33%2/6
defined $_->{"name"} and defined $_->{"when"} and defined $_->{"what"}
!ll&&!rl&&r
0015
defined $_->{"name"} and defined $_->{"when"}
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $_->{"name"} and defined $_->{"when"} and defined $_->{"what"}
defined $_->{"name"} defined $_->{"when"} defined $_->{"what"}
Truth table25%1/4
A and B and C
A= defined $_->{"name"}B= defined $_->{"when"}C= defined $_->{"what"}
ABCresult
0XX0
10X0
1100
1111
58  
59 croak "Malformed resource. 'when' and 'what' need to be coderefs."
60 unless ref $_->{what} eq "CODE"
61 15 ▶ && ref $_->{when} eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_->{"what"} eq "CODE" and ref $_->{"when"} eq "CODE" 0 15 15
Condition33%1/3
ref $_->{"what"} eq "CODE" and ref $_->{"when"} eq "CODE"
!ll&&!rl&&r
0015
MC/DC0%0/2
ref $_->{"what"} eq "CODE" and ref $_->{"when"} eq "CODE"
ref $_->{"what"} eq "CODE" ref $_->{"when"} eq "CODE"
Truth table33%1/3
A and B
A= ref $_->{"what"} eq "CODE"B= ref $_->{"when"} eq "CODE"
ABresult
0X0
100
111
62  
63 croak "Malformed resource. 'whatargs' needs to be an arrayref."
64 if $_->{whatargs}
65 15 ▶ && ref $_->{whatargs} ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"whatargs"} and ref $_->{"whatargs"} ne "ARRAY" 0 15 15
Condition66%2/3
$_->{"whatargs"} and ref $_->{"whatargs"} ne "ARRAY"
!ll&&!rl&&r
1320
MC/DC0%0/2
$_->{"whatargs"} and ref $_->{"whatargs"} ne "ARRAY"
$_->{"whatargs"} ref $_->{"whatargs"} ne "ARRAY"
Truth table66%2/3
A and B
A= $_->{"whatargs"}B= ref $_->{"whatargs"} ne "ARRAY"
ABresult
0X0
100
111
66  
67 croak "Malformed resource. 'whenargs' needs to be an arrayref."
68 if $_->{whenargs}
69 15 ▶ && ref $_->{whenargs} ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"whenargs"} and ref $_->{"whenargs"} ne "ARRAY" 1 14 15
Condition100%3/3
$_->{"whenargs"} and ref $_->{"whenargs"} ne "ARRAY"
!ll&&!rl&&r
1221
MC/DC100%2/2
$_->{"whenargs"} and ref $_->{"whenargs"} ne "ARRAY"
$_->{"whenargs"} ref $_->{"whenargs"} ne "ARRAY"
Truth table100%3/3
A and B
A= $_->{"whenargs"}B= ref $_->{"whenargs"} ne "ARRAY"
ABresult
0X0
100
111
70  
71 14 push @{ $self->{resources} }, $_;
72 }
73 }
74  
75 0 ▶ return wantarray ? @{ $self->{resources} } : $self->{resources};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
76 }
77  
78 # In: optional new value
79 # Out: current value
80 15 ▶sub testing {
Subroutine testing: called CC=3 SCAR=11.0
Pod documented
81 15 my $self = shift;
82 15 ▶ $self->{testing} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 11 15
83 15 ▶ return defined $ENV{RMTESTING} ? $ENV{RMTESTING} : $self->{testing};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'RMTESTING'} ? : 8 7 15
84 }
85  
86 # In: optional new value
87 # Out: current value
88 19 ▶sub verbose {
Subroutine verbose: called CC=3 SCAR=11.0
Pod documented
89 19 my $self = shift;
90 19 ▶ $self->{verbose} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 16 19
91 19 ▶ return defined $ENV{RMVERBOSE} ? $ENV{RMVERBOSE} : $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'RMVERBOSE'} ? : 10 9 19
92 }
93  
94 # In: optional new value
95 # Out: current value
96 12 ▶sub cont {
Subroutine cont: called CC=3 SCAR=11.0
Pod documented
97 12 my $self = shift;
98 12 ▶ $self->{cont} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 10 12
99 12 ▶ return defined $ENV{RMCONT} ? $ENV{RMCONT} : $self->{cont};
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'RMCONT'} ? : 7 5 12
100 }
101  
102 # In: n/a
103 # Out: hashref of our environment variables
104 0 ▶sub env {
Subroutine env: not called CC=1 SCAR=6.9
Pod documented
105 0 my $self = shift;
106 return { RMTESTING => $ENV{RMTESTING},
107 RMVERBOSE => $ENV{RMVERBOSE},
108 RMSTATES => $ENV{RMSTATES},
109 RMCONT => $ENV{RMCONT},
110 0 };
111 }
112  
113 # In: n/a
114 # Out: hashref of loaded states and their returns values.
115 8 ▶sub loaded {
Subroutine loaded: called CC=1 SCAR=0.0
Pod documented
116 8 my $self = shift;
117 8 return $self->{loaded};
118 }
119  
120 # In: n/a
121 # Out: status report
122 #
123 # Runs the appropriate resources()
124 8 ▶sub load {
Subroutine load: called CC=12 SCAR=24.9
Pod documented
125 8 my $self = shift;
126  
127 # clear out loaded() and status() tables.
128 8 undef $self->{loaded};
129 8 undef $self->{status};
130 8 $self->{status} = { map { $_->{name} => 'inactive' } @{ $self->{resources} } };
131  
132 8 for( @{ $self->{resources} } ) {
133 18 my $name = $_->{name};
134  
135  
136 18 ▶ if ( defined $ENV{RMSTATES} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"RMSTATES"}) 12 6 18
137 12 ▶ if ( grep { $_ eq $name } split /:/, $ENV{RMSTATES} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $name;} split(/:/, $ENV{'RMSTATES'}, 0)) { } 4 8 12
138 4 print __PACKAGE__ . " state '$name' present in RMSTATES environment var\n";
139 } else {
140 8 print __PACKAGE__ . " state '$name' skipped due to RMSTATES environment var\n";
141 8 $self->{status}{$name} = 'skipped';
142 8 next;
143 }
144 }
145  
146 10 ▶ if ( $_->{when}->( ref $_->{whenargs} eq "ARRAY"
Branch: 2 branches
BranchTrueFalseTotal
if ($_->{'when'}->(ref $_->{'whenargs'} eq 'ARRAY' ? @{$$_{'whenargs'};} : ())) { } 8 2 10
ref $_->{'whenargs'} eq 'ARRAY' ? : 2 8 10
147 2 ? @{ $_->{whenargs} }
148 : () ) ) {
149 8 ▶ print __PACKAGE__ . " state '$name' active\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 2 6 8
150  
151 8 ▶ if ( $self->testing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testing) { } 4 4 8
152 4 ▶ print __PACKAGE__ . " in testing: won't run code for state '$name'\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 2 2 4
153 4 $self->{status}{$name} = 'notrun';
154 } else {
155 $self->{loaded}{$name} = $_->{what}->( ref $_->{whatargs} eq "ARRAY"
156 4 ▶ ? @{ $_->{whatargs} }
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'whatargs'} eq 'ARRAY' ? : 1 3 4
157 : () );
158 4 $self->{status}{$name} = 'loaded';
159 }
160  
161 8 ▶ last unless $self->cont;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cont 4 4 8
162 } else {
163 2 ▶ print __PACKAGE__ . " state '$name' inactive\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
164 2 $self->{status}{$name} = 'inactive';
165 }
166 }
167  
168 8 return $self->loaded;
169 }
170  
171 # In: n/a
172 # Out: status report, e.g. { name => status, ... }
173 6 ▶sub status {
Subroutine status: called CC=3 SCAR=12.5
Pod documented
174 6 my $self = shift;
175  
176 return unless $self->{status}
177 6 ▶ && ref $self->{status} eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"status"} and ref $self->{"status"} eq "HASH" 0 6 6
Condition33%1/3
$self->{"status"} and ref $self->{"status"} eq "HASH"
!ll&&!rl&&r
006
MC/DC0%0/2
$self->{"status"} and ref $self->{"status"} eq "HASH"
$self->{"status"} ref $self->{"status"} eq "HASH"
Truth table33%1/3
A and B
A= $self->{"status"}B= ref $self->{"status"} eq "HASH"
ABresult
0X0
100
111
178  
179 6 return $self->{status};
180 }
181  
182 6 1;
183  
184 __END__
↑ Summary