Skip to content

Commit

Permalink
SweetArgs and test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Dieter Pearcey authored and nothingmuch committed Jan 11, 2009
1 parent 85694ef commit b75e86c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/MooseX/POE/SweetArgs.pm
@@ -0,0 +1,35 @@
package MooseX::POE::SweetArgs;

use Moose;
extends qw(MooseX::POE::Meta::Class);

around add_state_method => sub {
my $orig = shift;
my ($self, $name, $method) = @_;
$orig->($self, $name, sub {
$method->(@_[POE::Session::OBJECT(), POE::Session::ARG0()..$#_])
});
};

1;

=head1 NAME
MooseX::POE::SweetArgs - sugar around MooseX::POE event arguments
=head1 SYNOPSIS
package Thing;
# must come before MooseX::POE!
use metaclass 'MooseX::POE::SweetArgs';
use MooseX::POE;
# declare events like usual
event on_success => sub {
# unpack args like a Perl sub, not a POE event
my ($self, $foo, $bar) = @_;
...
};
=cut
26 changes: 26 additions & 0 deletions t/06_sweet.t
@@ -0,0 +1,26 @@
use strict;
use Test::More 'no_plan';

{
package Counter;
use metaclass 'MooseX::POE::SweetArgs';

has count => (is => 'rw', default => 1);

sub START {
my ($self) = @_;
$self->yield(add => 5);
}

event add => sub {
my ($self, $n) = @_;
::is(scalar @_, 2, 'correct number of args');
$self->count( $self->count + $n );
};

no MooseX::POE;
}

my $counter = Counter->new;
POE::Kernel->run;
is($counter, 6, 'correct final count');

0 comments on commit b75e86c

Please sign in to comment.