Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use Moose::Exporter and traits refactor
- Loading branch information
1 parent
1b9c8b0
commit 85694ef
Showing
6 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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__ | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters