Devel::Events::Handler::Log::Memory - An optional base role for event generators.


Devel-Events documentation Contained in the Devel-Events distribution.

Index


Code Index:

NAME

Top

Devel::Events::Handler::Log::Memory - An optional base role for event generators.

SYNOPSIS

Top

	use Devel::Events::Handler::Log::Memory;

	my $log = Devel::Events::Handler::Log::Memory->new();

	Some::Geneator->new( handler => $log );

DESCRIPTION

Top

This convenience role provides a basic send_event method, useful for implementing generators.

ATTRIBUTES

Top

events

The list of events.

Auto derefs.

matcher

The Devel::Events::Match instance used for event matching.

METHODS

Top

clear

Remove all events from the log.

Provided by MooseX::AttributeHelpers.

first $cond
first %args

Return the first event that matches a certain condition.

Delegates to Devel::Events::Match.

grep $cond
grep %args

Return the list of events that match a certain condition.

Delegates to Devel::Events::Match.

limit from => $cond, to => $cond, %args

Return events between two events. If if from or to is omitted then it returns all the events up to or from the other filter.

Delegates to Devel::Events::Match.

chunk $marker
chunk %args

Cuts the event log into chunks. When $marker matches a new chunk is opened.

Delegates to Devel::Events::Match.

new_event @event

Log the event to the events list by calling add_event.

add_event \@event

Provided by MooseX::AttributeHelpers.

replay $handler

Replay all the events in the log to $handler.

Useful if $handler does heavy analysis that you want to delay.

There isn't much to it:

	$handler->new_event(@$_) for $self->events;

So obviously you can replay subsets of events manually.

CAVEATS

Top

If any references are present in the event data then they will be preserved till the log is clear. This may cause leaks.

To overcome this problem use Devel::Events::Filter::Stringify. It will not allow overloading unless asked to, so it's safe to use without side effects.

TODO

Top

Add an option to always hash all the event data for convenience.

Make grep and limit into exportable functions, too.


Devel-Events documentation Contained in the Devel-Events distribution.

#!/usr/bin/perl

package Devel::Events::Handler::Log::Memory;
use Moose;

with qw/Devel::Events::Handler/;

use Devel::Events::Match;

use MooseX::AttributeHelpers;

has matcher => (
	isa => "Devel::Events::Match",
	is  => "rw",
	default => sub { Devel::Events::Match->new },
	handles => sub {
		my ( $attr, $meta ) = @_;
		
		my %mapping;

		foreach my $method ( $meta->get_method_list ) {
			next if $method =~ /^ (?: compile_cond | match ) $/x;
			next if __PACKAGE__->can($method);

			$mapping{$method} ||= sub {
				my ( $self, @args ) = @_;
				unshift @args, "match" if @args == 1;
				$self->matcher->$method( events => scalar($self->events), @args );
			}
		}

		return %mapping;
	}
);

has events => (
	metaclass => 'Collection::Array',
	isa => "ArrayRef",
	is  => "ro",
	default    => sub { [] },
	auto_deref => 1,
	provides   => {
		push  => 'add_event',
		clear => 'clear',
	},
);

sub new_event {
	my ( $self, @event ) = @_;
	$self->add_event(\@event);
}

sub replay {
	my ( $self, $handler ) = @_;
	$handler->new_event( @$_ ) for $self->events;
}

__PACKAGE__;

__END__