OpenInteract2/Config/GlobalOverride.pm

filter: Statementstmt 90.0% 144 / 160 Branchbran 67.7% 61 / 90 Conditioncond 66.6% 16 / 24 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 5 totaltotal 77.6% 239 / 308 CC 59 SCAR 24.2
CC
59
Cov
81%
_add_action
17.7
_remove_action
13.7
_read_rules
13.2
CRAP 11.3
« OpenInteract2/Manage/Package/CreateCPAN.pm ↑ Summary OpenInteract2/SPOPS/LDAP.pm »
1 package OpenInteract2::Config::GlobalOverride;
2  
3 # $Id: GlobalOverride.pm,v 1.11 2005/03/17 14:58:00 sjn Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use OpenInteract2::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 87 $OpenInteract2::Config::GlobalOverride::VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
12  
13 87 my ( $log );
14  
15 ########################################
16 # CLASS METHODS
17  
18 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
19 2 my ( $class, $params ) = @_;
20 2 ▶ $log ||= get_logger( LOG_INIT );
Condition66%2/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
110
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
21 2 my $self = bless( {}, $class );
22 2 return $self->_read_rules( $params );
23 }
24  
25  
26 8 ▶sub break_key {
Subroutine break_key: called CC=2 SCAR=7.1
Pod undocumented
27 8 my ( $class, $key ) = @_;
28 8 ▶ return () unless ( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 8 8
29 8 return split /\./, $key;
30 }
31  
32  
33 ########################################
34 # OBJECT METHODS
35  
36 # \%params should have either 'filename' or 'content' defined.
37 # We're not using our INI reader because a) this is simpler and b) we
38 # need to allow multiple actions per key
39  
40 2 ▶sub _read_rules {
Subroutine _read_rules: called CC=12 SCAR=25.8
41 2 my ( $self, $params ) = @_;
42  
43 2 my ( $lines );
44 2 ▶ if ( $params->{filename} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'filename'}) { } 0 2 2
elsif ($params->{'content'}) { } 2 0 2
45 0 $log->info( "Reading global override lines from file ",
46 "'$params->{filename}'" );
47 0 $lines = OpenInteract2::Config->read_file( $params->{filename} );
48 }
49 elsif ( $params->{content} ) {
50 2 $log->info( "Reading global override data from given content" );
51 2 $lines = [ split /\n/, $params->{content} ];
52 }
53 else {
54 0 oi_error "Cannot read override rules without 'filename' or ",
55 "'content' being defined";
56 }
57  
58 2 my @rules = ();
59 2 my ( $current_section, $current_rule );
60 2 for ( @{ $lines } ) {
61 34 chomp;
62 34 s/\r//g;
63 34 ▶ next if ( /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 6 28 34
64 28 ▶ next if ( /^\s*\#/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*\#/ 0 28 28
65 28 s/\s+$//;
66 28 s/^\s+//;
67  
68 # Encountered a key -- if we have a section/rule saved, stick
69 # that into our rule list and reset the section.
70  
71 28 ▶ if ( /^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) 8 20 28
72 8 ▶ if ( $current_section and $current_rule ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_section and $current_rule) 6 2 8
Condition66%2/3
$current_section and $current_rule
!ll&&!rl&&r
206
MC/DC50%1/2
$current_section and $current_rule
$current_section $current_rule
Truth table66%2/3
A and B
A= $current_sectionB= $current_rule
ABresult
0X0
100
111
73 6 push @rules, $current_rule;
74 }
75 8 $current_section = $1;
76 8 $log->debug( "New section found: $current_section" );
77 8 $current_rule = { key => $current_section };
78 8 next;
79 }
80  
81 # Otherwise, we should have a key/value pair. If there's
82 # already a value assigned to the key, we make it an
83 # arrayref. (This should only happen for the 'value' key.)
84  
85 20 my ( $param, $value ) = /^\s*([^=]+?)\s*=\s*(.*)\s*$/;
86 20 my $existing = $current_rule->{ $param };
87 20 ▶ if ( $existing and ref $existing eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($existing and ref $existing eq 'ARRAY') { } 0 20 20
elsif ($existing) { } 1 19 20
Condition66%2/3
$existing and ref $existing eq "ARRAY"
!ll&&!rl&&r
1910
MC/DC0%0/2
$existing and ref $existing eq "ARRAY"
$existing ref $existing eq "ARRAY"
Truth table66%2/3
A and B
A= $existingB= ref $existing eq "ARRAY"
ABresult
0X0
100
111
88 0 $log->debug( "Adding to existing rule for '$param': $value" );
89 0 push @{ $current_rule->{ $param } }, $value;
90 }
91 elsif ( $existing ) {
92 1 $log->debug( "Adding to existing rule for '$param': $value" );
93 1 $current_rule->{ $param } = [ $existing, $value ];
94 }
95 else {
96 19 $log->debug( "Adding new rule for '$param': $value" );
97 19 $current_rule->{ $param } = $value;
98 }
99 }
100  
101 # Stick the last rule into our rule list and set into the object
102  
103 2 push @rules, $current_rule;
104 2 $self->rules( \@rules );
105  
106 2 return $self;
107 }
108  
109  
110 # Get/set for the override rules
111  
112 4 ▶sub rules {
Subroutine rules: called CC=2 SCAR=6.9
Pod undocumented
113 4 my ( $self, $rules ) = @_;
114 4 ▶ if ( $rules ) { $self->{_rules} = $rules; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rules) 2 2 4
115 4 return $self->{_rules};
116 }
117  
118  
119 # Get an arrayref of override keys.
120  
121 2 ▶sub override_keys {
Subroutine override_keys: called CC=1 SCAR=0.0
Pod undocumented
122 2 my ( $self ) = @_;
123 2 return [ map { $_->{key} } @{ $self->{_rules} } ];
124 }
125  
126 # Main method: apply the set of override rules to a passed-in
127 # configuration
128  
129 2 ▶sub apply_rules {
Subroutine apply_rules: called CC=9 SCAR=22.8
Pod undocumented
130 2 my ( $self, $config ) = @_;
131 2 foreach my $rule ( @{ $self->rules } ) {
132 8 ▶ next unless ( ref $rule eq 'HASH' and keys %{ $rule } );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rule eq "HASH" and keys %{$rule;} 0 8 8
Condition33%1/3
ref $rule eq "HASH" and keys %{$rule;}
!ll&&!rl&&r
008
MC/DC0%0/2
ref $rule eq "HASH" and keys %{$rule;}
ref $rule eq "HASH" keys %{$rule;}
Truth table33%1/3
A and B
A= ref $rule eq "HASH"B= keys %{$rule;}
ABresult
0X0
100
111
133  
134 # For the processors: put the key inside the rule and ensure
135 # that 'value' is always an arrayref
136  
137 $rule->{value} = ( ref $rule->{value} eq 'ARRAY' )
138 ? $rule->{value}
139 8 ▶ : [ $rule->{value} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rule->{'value'} eq 'ARRAY' ? : 1 7 8
140  
141 # Process this rule
142  
143 8 $log->info( "Processing rule '$rule->{action}' for key ",
144 "'$rule->{key}'" );
145  
146 8 ▶ if ( $rule->{action} eq 'add' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule->{'action'} eq 'add') { } 5 3 8
elsif ($rule->{'action'} eq 'remove') { } 2 1 3
elsif ($rule->{'action'} eq 'replace') { } 1 0 1
147 5 $self->_key_iterate( $rule, $config,
148 { last_key => \&_add_action,
149 autovifify => 'yes' } );
150 }
151 elsif ( $rule->{action} eq 'remove' ) {
152 2 $self->_key_iterate( $rule, $config,
153 { last_key => \&_remove_action,
154 autovivify => 'no' } );
155 }
156 elsif ( $rule->{action} eq 'replace' ) {
157 1 ▶ unless ( $rule->{replace} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule->{"replace"}) 0 1 1
158 0 oi_error "Rule 'replace' for the key '$rule->{key}' ",
159 "must have a value for the 'replace' key.";
160 }
161 1 $self->_key_iterate( $rule, $config,
162 { last_key => \&_replace_action,
163 autovifify => 'no' } );
164 }
165 }
166 }
167  
168  
169 # Split apart the key in $rule->{key} and traverse $config; once we've
170 # reached the last key (where we should do something), execute the
171 # callback passed in $params->{last_key}. Caller should also specify
172 # whether we should autovifify keys as we traverse $config if a key
173 # isn't found -- 'yes' we should, 'no' we die.
174  
175 8 ▶sub _key_iterate {
Subroutine _key_iterate: called CC=9 SCAR=24.4
176 8 my ( $self, $rule, $config, $params ) = @_;
177  
178 8 my @keys = $self->break_key( $rule->{key} );
179 8 ▶ unless ( scalar @keys ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @keys) 0 8 8
180 0 oi_error "No keys found from '$rule->{key}'";
181 }
182  
183 8 my $item = $config;
184 8 my $num_keys = scalar @keys;
185  
186 8 ▶ for ( my $i = 0; $i < $num_keys; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_keys) 18 8 26
187 18 my $key = $keys[ $i ];
188  
189 # If the top-level key doesn't exist and there's more than one
190 # key then we don't do anything. This means we shouldn't
191 # autovivify top-level configuration items.
192  
193 18 ▶ last if ( $i == 0 and $num_keys > 1 and ! $item->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $num_keys > 1 and not $item->{$key} 0 18 18
Condition66%4/6
$i == 0 and $num_keys > 1 and not $item->{$key}
!ll&&!rl&&r
1080
$i == 0 and $num_keys > 1
!ll&&!rl&&r
1008
MC/DC0%0/3
$i == 0 and $num_keys > 1 and not $item->{$key}
$i == 0 $num_keys > 1 not $item->{$key}
Truth table50%2/4
A and B and not(C)
A= $i == 0B= $num_keys > 1C= not $item->{$key}
ABCresult
0XX0
10X0
1100
1111
194  
195 # Run the last key action
196  
197 18 ▶ if ( $i == $num_keys - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $num_keys - 1) 8 10 18
198 8 $params->{last_key}->( $rule, $item, $key );
199 8 next;
200 }
201  
202 # Otherwise climb down...
203  
204 # if we're supposed to autovivify, create the key to climb
205 # down, otherwise die
206  
207 10 ▶ unless ( $item->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{$key}) 0 10 10
208 0 ▶ if ( $params->{autovifify} eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'autovifify'} eq 'yes') { } 0 0 0
209 0 $item->{ $key } = {};
210 }
211 else {
212 0 oi_error "The key specified in '$rule->{action}' for ",
213 "'$rule->{key}' must already exist. (Nothing ",
214 "for '$key')";
215 }
216 }
217 10 $item = $item->{ $key };
218 }
219 }
220  
221  
222 # Action to execute when we find the last key for an 'add'
223  
224 5 ▶sub _add_action {
Subroutine _add_action: called CC=17 SCAR=28.7
225 5 my ( $rule, $item, $key ) = @_;
226 5 ▶ my $type = $rule->{type} || '';
Condition100%2/2
$rule->{'type'} || ''
l!l
23
MC/DC100%1/1
$rule->{'type'} || ''
$rule->{'type'}
Truth table100%2/2
A
A= $rule->{'type'}
Aresult
11
00
227 5 ▶ unless ( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 3 2 5
228 3 ▶ $type = 'list' if ( ref $item->{ $key } eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $item->{$key} eq "ARRAY" 1 2 3
229 3 ▶ $type = 'hash' if ( ref $item->{ $key } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $item->{$key} eq "HASH" 0 3 3
230 }
231  
232 5 ▶ unless ( $item->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{$key}) 2 3 5
233 2 ▶ $item->{ $key } = [] if ( $type eq 'list' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "list" 0 2 2
234 2 ▶ $item->{ $key } = {} if ( $type eq 'hash' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hash" 0 2 2
235 2 ▶ $item->{ $key } = '' if ( $type eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "" 2 0 2
236 }
237  
238 $log->is_info &&
239 $log->info( "Adding to '$key' with type '$type'; ",
240 "existing value is ",
241 5 ▶ ( defined $item->{ $key }
Branch: 2 branches
BranchTrueFalseTotal
$log->is_info and $log->info("Adding to '${key}' with type '${type}'; ", "existing value is ", defined $item->{$key} ? "'$item->{$key}'" : "not defined") 0 5 5
defined $item->{$key} ? : 0 0 0
242 ? "'$item->{ $key }'" : 'not defined' ) );
243 5 my $sv = join( ', ', @{ $rule->{value} } );
244 5 ▶ if ( $type eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'list') { } 3 2 5
245 3 ▶ unless ( ref $item->{ $key } eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item->{$key} eq "ARRAY") 2 1 3
246 $item->{ $key } = ( defined $item->{ $key } )
247 2 ▶ ? [ $item->{ $key } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{$key} ? : 2 0 2
248 }
249 3 ▶ my $queue = $rule->{queue} || 'back';
Condition100%2/2
$rule->{'queue'} || 'back'
l!l
12
MC/DC100%1/1
$rule->{'queue'} || 'back'
$rule->{'queue'}
Truth table100%2/2
A
A= $rule->{'queue'}
Aresult
11
00
250 3 ▶ if ( $queue eq 'front' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue eq 'front') { } 1 2 3
251 1 $log->info( "Adding values to front of list: $sv" );
252 1 unshift @{ $item->{ $key } }, @{ $rule->{value} };
253 }
254 else {
255 2 $log->info( "Adding values to back of list: $sv" );
256 2 push @{ $item->{ $key } }, @{ $rule->{value} };
257 }
258 }
259 else {
260 2 $item->{ $key } = $rule->{value}[0];
261 }
262 $log->info( "Resulting value of '$key': ",
263 ( ref $item->{ $key } )
264 3 ? join( ', ', @{ $item->{ $key } } )
265 5 ▶ : $item->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
ref $item->{$key} ? : 3 2 5
266 }
267  
268  
269 # Action to execute when we find the last key for a 'remove'
270  
271 2 ▶sub _remove_action {
Subroutine _remove_action: called CC=10 SCAR=26.2
272 2 my ( $rule, $item, $key ) = @_;
273 2 ▶ unless ( $item->{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{$key}) 0 2 2
274 0 delete $item->{ $key };
275 0 return;
276 }
277 2 my $type = $rule->{type};
278 2 ▶ unless ( $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 2 0 2
279 2 ▶ $type = 'list' if ( ref $item->{ $key } eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $item->{$key} eq "ARRAY" 2 0 2
280 2 ▶ $type = 'hash' if ( ref $item->{ $key } eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $item->{$key} eq "HASH" 0 2 2
281 2 ▶ $type ||= 'scalar';
Condition50%1/2
$type ||= "scalar"
l!l
20
MC/DC0%0/1
$type ||= "scalar"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
282 }
283 # If there are no values, just delete the key entirely
284  
285 2 ▶ unless ( $rule->{value}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule->{"value"}[0]) 1 1 2
286 1 delete $item->{ $key };
287 1 return;
288 }
289  
290 # Otherwise cycle through the values and do the right thing
291  
292 1 foreach my $value ( @{ $rule->{value} } ) {
293 1 ▶ if ( $type eq 'list' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'list') { } 1 0 1
elsif ($type eq 'hash') { } 0 0 0
294 3 $item->{ $key } = [ grep { $_ ne $value }
295 1 @{ $item->{ $key } } ];
296 }
297 elsif ( $type eq 'hash' ) {
298 0 delete $item->{ $key }{ $value };
299 }
300 else {
301 0 delete $item->{ $key };
302 }
303 }
304 }
305  
306 # Action to execute when we find the last key for a 'replace'
307  
308 1 ▶sub _replace_action {
Subroutine _replace_action: called CC=4 SCAR=14.0
309 1 my ( $rule, $item, $key ) = @_;
310 1 ▶ unless ( ref $item->{ $key } eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $item->{$key} eq "ARRAY") 0 1 1
311 0 oi_error "The rule 'replace' can only be applied to lists. ",
312 "The value in the key [$rule->{key}] is not a list.";
313 }
314 1 my @new_list = ();
315 1 foreach my $existing ( @{ $item->{ $key } } ) {
316 2 ▶ if ( $existing eq $rule->{replace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing eq $rule->{'replace'}) { } 1 1 2
317 1 push @new_list, @{ $rule->{value} };
318 }
319 else {
320 1 push @new_list, $existing;
321 }
322 }
323 1 $item->{ $key } = \@new_list;
324 }
325  
326 87 1;
327  
328 __END__
« OpenInteract2/Manage/Package/CreateCPAN.pm ↑ Summary OpenInteract2/SPOPS/LDAP.pm »