| 1 | package Aspect::Pointcut::Or; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 21 | ▶ | use Aspect::Pointcut::Logic (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 21 | our $VERSION = '0.97_06'; | |||||||||||||
| 8 | 21 | our @ISA = 'Aspect::Pointcut::Logic'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | ###################################################################### | ||||||||||||||
| 15 | # Constructor | ||||||||||||||
| 16 | |||||||||||||||
| 17 | sub new { | ||||||||||||||
| 18 | 18 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 19 | 18 | my @parts = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | # Validate the pointcut subexpressions | ||||||||||||||
| 22 | 18 | foreach my $part ( @parts ) { | |||||||||||||
| 23 | 37 | ▶ | next if Params::Util::_INSTANCE($part, 'Aspect::Pointcut'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 0 | Carp::croak("Attempted to apply pointcut logic to non-pointcut '$part'"); | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | # Collapse nested or statements at constructor time so we don't have | ||||||||||||||
| 28 | # to do so multiple times later on during currying. | ||||||||||||||
| 29 | 18 | ▶ | while ( scalar grep { $_->isa('Aspect::Pointcut::Or') } @parts ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | @parts = map { | ||||||||||||||
| 31 | 6 | ▶ | $_->isa('Aspect::Pointcut::Or') ? @$_ : $_ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } @parts; | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 18 | $class->SUPER::new(@parts); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | |||||||||||||||
| 42 | ###################################################################### | ||||||||||||||
| 43 | # Weaving Methods | ||||||||||||||
| 44 | |||||||||||||||
| 45 | sub compile_weave { | ||||||||||||||
| 46 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
compile_weave: called CC=7 SCAR=25.7
Pod
documented
| |||||||||||||||
| 47 | 3 | my @children = map { $_->compile_weave } @$self; | |||||||||||||
| 48 | |||||||||||||||
| 49 | # Collapse string conditions together, | ||||||||||||||
| 50 | # and further collapse code conditions together. | ||||||||||||||
| 51 | 3 | my @string = (); | |||||||||||||
| 52 | 3 | my @code = (); | |||||||||||||
| 53 | 3 | foreach my $child ( @children ) { | |||||||||||||
| 54 | # Short-cut if we contain any purely runtime pointcuts | ||||||||||||||
| 55 | 6 | ▶ | unless ( ref $child ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 6 | ▶ | return 1 if $child eq 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 6 | push @string, $child; | |||||||||||||
| 58 | 6 | next; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 0 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | my $group = join ' or ', map { "( $_ )" } @string; | |||||||||||||
| 62 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 63 | 0 | @string = (); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 0 | push @code, $child; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 3 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 3 | my $group = join ' or ', map { "( $_ )" } @string; | |||||||||||||
| 70 | 3 | ▶ | unless ( @code ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | # This is the only thing we have | ||||||||||||||
| 72 | 3 | return $group; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | # Join the groups | ||||||||||||||
| 78 | return sub { | ||||||||||||||
| 79 | 0 | ▶ | foreach my $child ( @code ) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 80 | 0 | ▶ | return 0 unless $child->(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | } | ||||||||||||||
| 82 | 0 | return 1; | |||||||||||||
| 83 | 0 | }; | |||||||||||||
| 84 | |||||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | sub compile_runtime { | ||||||||||||||
| 88 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
compile_runtime: called CC=7 SCAR=25.7
Pod
documented
| |||||||||||||||
| 89 | 1 | my @children = map { $_->compile_runtime } @$self; | |||||||||||||
| 90 | |||||||||||||||
| 91 | # Collapse string conditions together, | ||||||||||||||
| 92 | # and further collapse code conditions together. | ||||||||||||||
| 93 | 1 | my @string = (); | |||||||||||||
| 94 | 1 | my @code = (); | |||||||||||||
| 95 | 1 | foreach my $child ( @children ) { | |||||||||||||
| 96 | # Short-cut if we contain any purely runtime pointcuts | ||||||||||||||
| 97 | 2 | ▶ | unless ( ref $child ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 2 | ▶ | return 1 if $child eq 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 2 | push @string, $child; | |||||||||||||
| 100 | 2 | next; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | 0 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | my $group = join ' or ', map { "( $_ )" } @string; | |||||||||||||
| 104 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 105 | 0 | @string = (); | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | 0 | push @code, $child; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 1 | ▶ | if ( @string ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 1 | my $group = join ' or ', map { "( $_ )" } @string; | |||||||||||||
| 112 | 1 | ▶ | unless ( @code ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | # This is the only thing we have | ||||||||||||||
| 114 | 1 | return $group; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | 0 | push @code, eval "sub () { $group }"; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | # Join the groups | ||||||||||||||
| 120 | return sub { | ||||||||||||||
| 121 | 0 | ▶ | foreach my $child ( @code ) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 122 | 0 | ▶ | return 0 unless $child->(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | } | ||||||||||||||
| 124 | 0 | return 1; | |||||||||||||
| 125 | 0 | }; | |||||||||||||
| 126 | |||||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | sub match_contains { | ||||||||||||||
| 130 | 9 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
match_contains: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 131 | 9 | ▶ | my $count = $self->isa($_[0]) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 9 | foreach my $child ( @$self ) { | |||||||||||||
| 133 | 18 | $count += $child->match_contains($_[0]); | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | 9 | return $count; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | sub match_runtime { | ||||||||||||||
| 139 | 7 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
match_runtime: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 140 | 7 | foreach my $child ( @$self ) { | |||||||||||||
| 141 | 15 | ▶ | return 1 if $child->match_runtime; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | } | ||||||||||||||
| 143 | 2 | return 0; | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | sub curry_weave { | ||||||||||||||
| 147 | 4 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
curry_weave: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||
| 148 | 4 | my @list = @$self; | |||||||||||||
| 149 | |||||||||||||||
| 150 | # Curry down our children. Any null element always matches, and | ||||||||||||||
| 151 | # therefore in an OR scenario the entire expression always matches. | ||||||||||||||
| 152 | 4 | my @or = (); | |||||||||||||
| 153 | 4 | foreach my $child ( @list ) { | |||||||||||||
| 154 | 8 | ▶ | my $curried = $child->curry_weave or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | 7 | push @or, $curried; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | # If none are left, curry us away to nothing | ||||||||||||||
| 159 | 3 | ▶ | return unless @or; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | |||||||||||||||
| 161 | # If only one remains, curry us away to just that child | ||||||||||||||
| 162 | 3 | ▶ | return $list[0] if @or == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | |||||||||||||||
| 164 | # Create our clone to hold the curried subset | ||||||||||||||
| 165 | 3 | return ref($self)->new(@or); | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | |||||||||||||||
| 168 | sub curry_runtime { | ||||||||||||||
| 169 | 7 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
curry_runtime: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 170 | 7 | my @list = @$self; | |||||||||||||
| 171 | |||||||||||||||
| 172 | # Should we strip out the call pointcuts | ||||||||||||||
| 173 | 7 | my $strip = shift; | |||||||||||||
| 174 | 7 | ▶ | unless ( defined $strip ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | # Are there any elements that MUST exist at run-time? | ||||||||||||||
| 176 | 6 | ▶ | if ( $self->match_runtime ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | # If we have any nested logic that themselves contain | ||||||||||||||
| 178 | # call pointcuts, we can't strip. | ||||||||||||||
| 179 | $strip = not scalar grep { | ||||||||||||||
| 180 | 4 | ▶ | $_->isa('Aspect::Pointcut::Logic') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | and | ||||||||||||||
| 182 | $_->match_contains('Aspect::Pointcut::Call') | ||||||||||||||
| 183 | } @list; | ||||||||||||||
| 184 | } else { | ||||||||||||||
| 185 | # Nothing at runtime, so we can strip | ||||||||||||||
| 186 | 2 | $strip = 1; | |||||||||||||
| 187 | } | ||||||||||||||
| 188 | } | ||||||||||||||
| 189 | |||||||||||||||
| 190 | # Curry down our children | ||||||||||||||
| 191 | 8 | @list = grep { defined $_ } map { | |||||||||||||
| 192 | 7 | ▶ | $_->isa('Aspect::Pointcut::Call') | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 193 | ? $strip | ||||||||||||||
| 194 | ? $_->curry_runtime($strip) | ||||||||||||||
| 195 | : $_ | ||||||||||||||
| 196 | : $_->curry_runtime($strip) | ||||||||||||||
| 197 | } @list; | ||||||||||||||
| 198 | |||||||||||||||
| 199 | # If none are left, curry us away to nothing | ||||||||||||||
| 200 | 7 | ▶ | return unless @list; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 201 | |||||||||||||||
| 202 | # If only one remains, curry us away to just that child | ||||||||||||||
| 203 | 5 | ▶ | return $list[0] if @list == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 204 | |||||||||||||||
| 205 | # Create our clone to hold the curried subset | ||||||||||||||
| 206 | 2 | return ref($self)->new( @list ); | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | |||||||||||||||
| 209 | 21 | 1; | |||||||||||||
| 210 | |||||||||||||||
| 211 | __END__ | ||||||||||||||