Aspect

 view release on metacpan or  search on metacpan

lib/Aspect/Pointcut/And.pm  view on Meta::CPAN

package Aspect::Pointcut::And;

use strict;
use Aspect::Pointcut::Logic ();

our $VERSION = '1.04';
our @ISA     = 'Aspect::Pointcut::Logic';





######################################################################
# Constructor

sub new {
	my $class = shift;
	my @parts = @_;

	# Validate the pointcut subexpressions
	foreach my $part ( @parts ) {
		next if Params::Util::_INSTANCE($part, 'Aspect::Pointcut');
		Carp::croak("Attempted to apply pointcut logic to non-pointcut '$part'");
	}

	# Collapse nested and statements at constructor time so we don't have
	# to do so multiple times later on during currying.
	while ( scalar grep { $_->isa('Aspect::Pointcut::And') } @parts ) {
		@parts = map {
			$_->isa('Aspect::Pointcut::And') ? @$_ : $_
		} @parts;
	}

	$class->SUPER::new(@parts);
}





######################################################################
# Weaving Methods

sub compile_weave {
	my $self = shift;

	# Handle special cases
	my @children = grep {
		ref $_ or $_ ne 1
	} map {
		$_->compile_weave
	} @$self;
	unless ( @children ) {
		# Potential bug, but why would we legitimately be empty
		return 1;
	}
	if ( @children == 1 ) {
		return $children[0];
	}

	# Collapse string conditions together,
	# and further collapse code conditions together.
	my @string = ();
	my @code   = ();
	foreach my $child ( @children ) {
		unless ( ref $child ) {
			push @string, $child;
			next;
		}
		if ( @string ) {
			my $group = join ' and ', map { "( $_ )" } @string;
			push @code, eval "sub () { $group }";
			@string = ();
		}
		push @code, $child;
	}

	if ( @string ) {
		my $group = join ' and ', map { "( $_ )" } @string;
		unless ( @code ) {
			# This is the only thing we have
			return $group;
		}
		push @code, eval "sub () { $group }";
	}

	# Join the groups
	return sub {
		foreach my $child ( @code ) {
			return 0 unless $child->();
		}
		return 1;
	};
}



( run in 0.879 second using v1.01-cache-2.11-cpan-5a3173703d6 )