| 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")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 28 | ▶ | next if ( /^\s*\#/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | if ( $current_section and $current_rule ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$current_section and $current_rule
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$existing and ref $existing eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $rule eq "HASH" and keys %{$rule;}
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;}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$i == 0 and $num_keys > 1 and not $item->{$key}
$i == 0 and $num_keys > 1
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # Run the last key action | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 18 | ▶ | if ( $i == $num_keys - 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | if ( $params->{autovifify} eq 'yes' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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'} || ''
MC/DC100%1/1
$rule->{'type'} || ''
$rule->{'type'}
Truth table100%2/2
A
A= $rule->{'type'}
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 5 | ▶ | unless ( $type ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | ▶ | $type = 'list' if ( ref $item->{ $key } eq 'ARRAY' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | $type = 'hash' if ( ref $item->{ $key } eq 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | ▶ | unless ( $item->{ $key } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | ▶ | $item->{ $key } = [] if ( $type eq 'list' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 2 | ▶ | $item->{ $key } = {} if ( $type eq 'hash' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | ▶ | $item->{ $key } = '' if ( $type eq '' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | ? "'$item->{ $key }'" : 'not defined' ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 5 | my $sv = join( ', ', @{ $rule->{value} } ); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 5 | ▶ | if ( $type eq 'list' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 3 | ▶ | unless ( ref $item->{ $key } eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | $item->{ $key } = ( defined $item->{ $key } ) | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | ▶ | ? [ $item->{ $key } ] : []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 3 | ▶ | my $queue = $rule->{queue} || 'back'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$rule->{'queue'} || 'back'
MC/DC100%1/1
$rule->{'queue'} || 'back'
$rule->{'queue'}
Truth table100%2/2
A
A= $rule->{'queue'}
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | if ( $queue eq 'front' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | delete $item->{ $key }; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | my $type = $rule->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | ▶ | unless ( $type ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | ▶ | $type = 'list' if ( ref $item->{ $key } eq 'ARRAY' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | ▶ | $type = 'hash' if ( ref $item->{ $key } eq 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | ▶ | $type ||= 'scalar'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$type ||= "scalar"
MC/DC0%0/1
$type ||= "scalar"
$type
Truth table50%1/2
A
A= $type
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # If there are no values, just delete the key entirely | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | ▶ | unless ( $rule->{value}[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||