NAME

Mixin::Event::Dispatch - mixin methods for simple event/message dispatch framework

VERSION

version 0.001

SYNOPSIS

 # Add a handler then invoke it
 my $obj = Some::Class->new;
 $obj->add_handler_for_event(some_event => sub { my $self = shift; warn "had some_event: @_"; 1; });
 $obj->invoke_event(some_event => 'message here');

 # Attach event handler for all on_XXX named parameters
 package Event::User;
 sub configure {
	my $self = shift;
	my %args = @_;
	$self->add_handler_for_event(
		map { (/^on_(.*)$/) ? ($1 => $args{$_}) : () } for keys %args
	);
	return $self;
 }

DESCRIPTION

Add this in as a parent to your class, and it'll provide some methods for defining event handlers ("add_event_handler") and calling them ("invoke_event").

METHODS

invoke_event

Takes an event parameter, and optional additional parameters that are passed to any callbacks.

$self->invoke_event('new_message', from => 'fred', subject => 'test message');

Returns $self if a handler was found, undef if not.

add_handler_for_event

Adds handlers to the stack for the given events.

$self->add_handler_for_event(
	new_message	=> sub { warn @_; 1 },
	login		=> sub { warn @_; 1 },
	logout		=> sub { warn @_; 1 },
);

event_stack

Accessor for the event stack itself - should return a hashref which maps event names to arrayrefs for the currently defined handlers.

clear_event_handlers

Removes all queued event handlers.

Will also be called when defining the first handler to create the initial "event_stack" entry, should be overridden by subclass if something other than $self->{event_stack} should be used.

SEE ALSO

There are at least a dozen similar modules already on CPAN, eventually I'll add a list of them here.

AUTHOR

Tom Molesworth <cpan@entitymodel.com>

LICENSE

Copyright Tom Molesworth 2011. Licensed under the same terms as Perl itself.