Skip to content

Commit

Permalink
use Moose::Exporter and traits refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingmuch committed Jan 11, 2009
1 parent 1b9c8b0 commit 85694ef
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 15 deletions.
48 changes: 48 additions & 0 deletions lib/MooseX/POE/Exporter.pm
@@ -0,0 +1,48 @@
#!/usr/bin/perl

package MooseX::POE::Exporter;
use Moose::Role;

use MooseX::POE::Exporter;

requires qw(_init_params _also_import);

sub event {
my ( $class, $name, $body ) = @_;
MooseX::POE::Meta::Class->initialize($class)->add_method( "on_$name" =>
MooseX::Async::Meta::Method::State->wrap(
event => $name,
name => "on_$name",
body => $body,
package_name => $class,
),
);
}

sub setup_import_methods {
my $class = shift;

Moose::Exporter->setup_import_methods(
exporting_package => $class,
export => [qw(event)],
with_caller => [qw(event)],
also => $class->_also_import,
);
}

sub init_meta {
my ( $pkg, %args ) = @_;

my $class = $args{for_class};

## no critic
eval qq{package $class; use POE; };
## use critic
die $@ if $@;

$pkg->_also_import->init_meta( $pkg->_init_params, %args );
}

__PACKAGE__

__END__
29 changes: 29 additions & 0 deletions lib/MooseX/POE/Meta/Class/Trait.pm
@@ -0,0 +1,29 @@
package MooseX::POE::Meta::Class::Trait;
use Moose::Role;

with qw(MooseX::POE::Meta::Trait);

sub get_new_session {
my ( $self, $instance ) = @_;

return POE::Session->create(
object_states => [
$instance => {
_start => '_start',
STARTALL => "STARTALL",
_stop => 'STOPALL',
( map { $_->event => $_->name } $self->get_all_events ),
# ( map { $_ => "on_$_" } grep { warn $_; s/^on_// } $self->list_all_methods ), # compat
},
],
args => [$instance],
heap => {},
);
}

sub get_session_id {
my ( $self, $instance ) = @_;
$self->get_attribute("_poe_session_id")->get_value($instance);
}

1;
8 changes: 4 additions & 4 deletions lib/MooseX/POE/Meta/Instance.pm
Expand Up @@ -9,7 +9,7 @@ use Scalar::Util ();

sub create_instance {
my $self = shift;
my $instance = $self->SUPER::create_instance(@_);
my $instance = $self->bless_instance_structure( {} );
my $session = $self->get_new_session($instance);
$instance->{heap} = $session->get_heap;
$instance->{session_id} = $session->ID;
Expand All @@ -20,11 +20,11 @@ sub get_new_session {
my ( $self, $instance ) = @_;
my $meta = $self->associated_metaclass;
return POE::Session->create(
inline_states => { _start => sub { POE::Kernel->yield('STARTALL') }, },
object_states => [
$instance => {
_start => '_start',
STARTALL => "STARTALL",
_stop => 'STOPALL',
STARTALL => 'STARTALL',
_stop => 'STOPALL',
map { $_ => $meta->get_state_method_name($_) }
map { $_->meta->get_events }
grep { $_->meta->isa('MooseX::POE::Meta::Class') }
Expand Down
28 changes: 28 additions & 0 deletions lib/MooseX/POE/Meta/Trait.pm
@@ -0,0 +1,28 @@
package MooseX::POE::Meta::Trait;
use Moose::Role;

with 'MooseX::Async::Meta::Trait' => {
excludes => [qw(get_events)],
};

sub filter_event_methods {
my ($self, @methods) = @_;

# FIXME make into a does_role test
grep { $_->isa("MooseX::Async::Meta::Method::State") } @methods;
}

sub get_events {
my $self = shift;
$self->filter_event_methods( $self->get_methods );
}

sub get_all_events {
my $self = shift;
$self->filter_event_methods( $self->get_all_methods );
}

__PACKAGE__

__END__
9 changes: 3 additions & 6 deletions lib/MooseX/POE/Object.pm
Expand Up @@ -2,8 +2,6 @@ package MooseX::POE::Object;
use strict;
our $VERSION = '0.050';

use POE;

use metaclass 'MooseX::POE::Meta::Class' =>
( instance_metaclass => 'MooseX::POE::Meta::Instance' );

Expand All @@ -17,10 +15,6 @@ sub yield { my $self = shift; POE::Kernel->post( $self->get_session_id, @_ ) }

sub call { my $self = shift; POE::Kernel->call( $self->get_session_id, @_ ) }

sub _start {
$_[KERNEL]->yield('STARTALL');
}

sub STARTALL {
# NOTE: we ask Perl if we even
# need to do this first, to avoid
Expand All @@ -44,6 +38,9 @@ sub STOPALL {
}
}

sub START {}
sub STOP {}

# __PACKAGE__->meta->add_method( _stop => sub { POE::Kernel->call('STOP') } );

__PACKAGE__->meta->alias_method( _default => 'DEFAULT' )
Expand Down
10 changes: 5 additions & 5 deletions lib/MooseX/POE/Object/Deferred.pm
@@ -1,16 +1,16 @@
package MooseX::POE::Object::Deferred;
use Moose;

use metaclass 'MooseX::POE::Meta::Class' =>
( instance_metaclass => 'MooseX::POE::Meta::Instance::Deferred' );
use metaclass 'MooseX::POE::Meta::Class';

extends qw(MooseX::POE::Object);


has '+_poe_session_id' => ( lazy => 1 );

sub start_session {
my $self = shift;
my $session = $self->meta->get_meta_instance->get_new_session($self);
$self->{session_id} = $session->ID;
$self->{heap} = $session->get_heap;
$self->get_session_id(); # force
}

no Moose;
Expand Down

0 comments on commit 85694ef

Please sign in to comment.