Skip to content

Commit

Permalink
Add a constructor meta trait so we can inling things. Needs git versi…
Browse files Browse the repository at this point in the history
…on (will be 0.73) of Moose

Signed-off-by: Chris Prather <chris@prather.org>
  • Loading branch information
ashb authored and perigrin committed Apr 2, 2009
1 parent 352090a commit cd2f908
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Load the Module::Install bundled in ./inc/
use inc::Module::Install 0.77;
use inc::Module::Install 0.79;

# Define metadata
name 'MooseX-POE';
all_from 'lib/MooseX/POE.pm';

# Specific dependencies
requires 'Moose' => '0.26';
requires 'Moose' => '0.73';
requires 'MooseX::Async' => '0.01';
requires 'POE' => '0.9999';
requires 'Sub::Name' => '0.04';
Expand Down
5 changes: 2 additions & 3 deletions lib/MooseX/POE.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ sub init_meta {
Moose::Util::MetaRole::apply_metaclass_roles(
for_class => $for,
metaclass_roles => [ 'MooseX::POE::Meta::Trait::Class' ],
instance_metaclass_roles => [
'MooseX::POE::Meta::Trait::Instance',
],
constructor_class_roles => [ 'MooseX::POE::Meta::Trait::Constructor' ],
instance_metaclass_roles => [ 'MooseX::POE::Meta::Trait::Instance' ],
);

Moose::Util::MetaRole::apply_base_class_roles(
Expand Down
35 changes: 35 additions & 0 deletions lib/MooseX/POE/Meta/Trait/Constructor.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package MooseX::POE::Meta::Trait::Constructor;

use Moose::Role;

sub _expected_constructor_class { "MooseX::POE::Meta::Trait::Object" }

around _generate_instance => sub {
my $orig = shift;
my ($self, $var, $class_var) = @_;

my %events = $self->meta_instance->associated_metaclass->get_all_events;
$events{STARTALL} = 'STARTALL';
$events{_stop} = 'STOPALL';

my $events = join(', ', map {
s/'/\\'/g;
"'$_'"
} %events);

my $source = $orig->(@_) . <<"EOF"
my \$session = POE::Session->create(
inline_states => { _start => sub { POE::Kernel->yield('STARTALL') }, },
object_states => [
$var => { $events }
],
args => [ $var ],
heap => (${var}->{heap} ||= {}),
);
${var}->{session_id} = \$session->ID;
EOF
};

no Moose::Role;

1;
38 changes: 37 additions & 1 deletion t/07_aliased.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 3;
use Test::More tests => 6;

use POE;

Expand All @@ -19,6 +19,18 @@ my @log;
push @log, [ @_[ARG0 .. $#_ ] ];
};
}
{
package ImmutableFoo;
use MooseX::POE;

with qw(MooseX::POE::Aliased);

event foo => sub {
push @log, [ @_[ARG0 .. $#_ ] ];
};

__PACKAGE__->meta->make_immutable;
}

my $foo = Foo->new( alias => "blah" );

Expand All @@ -41,3 +53,27 @@ is( scalar(@log), 2, "two events" );

is_deeply( $log[0], ["this"], "first event under alias 'blah'" );
is_deeply( $log[1], ["that"], "second event under alias 'bar'" );

@log = ();
$foo = ImmutableFoo->new( alias => "blah" );

POE::Session->create(
inline_states => {
_start => sub {
$_[KERNEL]->yield("blah");
},
blah => sub {
$_[KERNEL]->post( blah => foo => "this" );
$foo->alias("bar");
$_[KERNEL]->post( bar => foo => "that" );
},
}
);

$poe_kernel->run;

is( scalar(@log), 2, "two events" );

is_deeply( $log[0], ["this"], "first event under alias 'blah'" );
is_deeply( $log[1], ["that"], "second event under alias 'bar'" );

2 changes: 1 addition & 1 deletion t/08_moosex_declare.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ role Rollo {

does_ok(Rollo->meta, "MooseX::POE::Meta::Role");

class App with Rollo is mutable {
class App with Rollo {
use MooseX::POE;

sub START {
Expand Down

0 comments on commit cd2f908

Please sign in to comment.