OpenInteract/Config/GlobalOverride.pm

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