diff options
author | doy <doy@tozt.net> | 2009-04-26 19:25:37 -0500 |
---|---|---|
committer | doy <doy@tozt.net> | 2009-04-26 19:25:37 -0500 |
commit | 42385a5a09bee71148e745ccbfe0a8d5286255f8 (patch) | |
tree | 70da3ab0f3f9528efdcceb5a87e2ea682549f17b /lib/Bot/Games/Trait | |
parent | 27da6e113d34fef72de8b95b7261c137d6f9ef76 (diff) | |
download | bot-games-42385a5a09bee71148e745ccbfe0a8d5286255f8.tar.gz bot-games-42385a5a09bee71148e745ccbfe0a8d5286255f8.zip |
more renamespacing
Diffstat (limited to 'lib/Bot/Games/Trait')
-rw-r--r-- | lib/Bot/Games/Trait/Attribute/Command.pm | 45 | ||||
-rw-r--r-- | lib/Bot/Games/Trait/Class/Command.pm | 23 | ||||
-rw-r--r-- | lib/Bot/Games/Trait/Method/Command.pm | 24 |
3 files changed, 92 insertions, 0 deletions
diff --git a/lib/Bot/Games/Trait/Attribute/Command.pm b/lib/Bot/Games/Trait/Attribute/Command.pm new file mode 100644 index 0000000..9b51b13 --- /dev/null +++ b/lib/Bot/Games/Trait/Attribute/Command.pm @@ -0,0 +1,45 @@ +package Bot::Games::Trait::Attribute::Command; +use Moose::Role; + +has command => ( + is => 'rw', + isa => 'Bool', + default => 0, +); + +has needs_init => ( + is => 'rw', + isa => 'Bool', + default => 1, +); + +before _process_options => sub { + my $self = shift; + my ($name, $options) = @_; + warn "needs_init is useless for attributes without command" + if exists($options->{needs_init}) && !$options->{command}; +}; + +around accessor_metaclass => sub { + my $orig = shift; + my $self = shift; + my $metaclass = $self->$orig(@_); + return $metaclass unless $self->command; + return Moose::Meta::Class->create_anon_class( + superclasses => [$metaclass], + roles => ['Bot::Games::Trait::Method::Command'], + cache => 1, + )->name; +}; + +after install_accessors => sub { + my $self = shift; + return unless $self->command; + my $method_meta = $self->get_read_method_ref; + $method_meta->pass_args(0); + $method_meta->needs_init($self->needs_init); +}; + +no Moose::Role; + +1; diff --git a/lib/Bot/Games/Trait/Class/Command.pm b/lib/Bot/Games/Trait/Class/Command.pm new file mode 100644 index 0000000..3c242ae --- /dev/null +++ b/lib/Bot/Games/Trait/Class/Command.pm @@ -0,0 +1,23 @@ +package Bot::Games::Trait::Class::Command; +use Moose::Role; + +after ((map { "add_${_}_method_modifier" } qw/before after around/) => sub { + my $self = shift; + my $name = shift; + + my $method_meta = $self->get_method($name); + my $orig_method_meta = $method_meta->get_original_method; + return unless $orig_method_meta->meta->can('does_role') + && $orig_method_meta->meta->does_role('Bot::Games::Trait::Method::Command'); + my $pass_args = $orig_method_meta->pass_args; + my $method_metaclass = Moose::Meta::Class->create_anon_class( + superclasses => [blessed $method_meta], + roles => ['Bot::Games::Trait::Method::Command'], + cache => 1, + ); + $method_metaclass->rebless_instance($method_meta, pass_args => $pass_args); +}); + +no Moose::Role; + +1; diff --git a/lib/Bot/Games/Trait/Method/Command.pm b/lib/Bot/Games/Trait/Method/Command.pm new file mode 100644 index 0000000..e00cb43 --- /dev/null +++ b/lib/Bot/Games/Trait/Method/Command.pm @@ -0,0 +1,24 @@ +package Bot::Games::Trait::Method::Command; +use Moose::Role; + +has pass_args => ( + is => 'rw', + isa => 'Bool', + default => 1, +); + +has needs_init => ( + is => 'rw', + isa => 'Bool', + default => 1, +); + +around execute => sub { + my $orig = shift; + my $self = shift; + return $self->pass_args ? $self->$orig(@_) : $self->$orig($_[0]); +}; + +no Moose::Role; + +1; |