From 188ad8e49c1fa2802eaa55b3d4ae978f2c58c23c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 31 Oct 2009 15:16:49 -0500 Subject: convert Bot::Games to use IM::Engine::Plugin::Commands --- bin/run | 22 ++-- dist.ini | 3 +- lib/Bot/Games.pm | 160 +---------------------------- lib/Bot/Games/Game.pm | 49 ++------- lib/Bot/Games/Game/24.pm | 6 +- lib/Bot/Games/Game/Chess.pm | 8 +- lib/Bot/Games/Game/Ghost.pm | 6 +- lib/Bot/Games/Game/Role/CurrentPlayer.pm | 4 +- lib/Bot/Games/Game/Spook.pm | 4 +- lib/Bot/Games/Game/Superghost.pm | 4 +- lib/Bot/Games/Game/Xghost.pm | 4 +- lib/Bot/Games/OO.pm | 18 +--- lib/Bot/Games/OO/Game.pm | 65 ------------ lib/Bot/Games/OO/Game/Role.pm | 28 ----- lib/Bot/Games/OO/Role.pm | 4 +- lib/Bot/Games/Trait/Attribute/Command.pm | 62 ----------- lib/Bot/Games/Trait/Attribute/Formatted.pm | 58 ----------- lib/Bot/Games/Trait/Class/Command.pm | 33 ------ lib/Bot/Games/Trait/Class/Formatted.pm | 26 ----- lib/Bot/Games/Trait/Method/Command.pm | 24 ----- lib/Bot/Games/Trait/Method/Formatted.pm | 22 ---- 21 files changed, 50 insertions(+), 560 deletions(-) delete mode 100644 lib/Bot/Games/OO/Game.pm delete mode 100644 lib/Bot/Games/OO/Game/Role.pm delete mode 100644 lib/Bot/Games/Trait/Attribute/Command.pm delete mode 100644 lib/Bot/Games/Trait/Attribute/Formatted.pm delete mode 100644 lib/Bot/Games/Trait/Class/Command.pm delete mode 100644 lib/Bot/Games/Trait/Class/Formatted.pm delete mode 100644 lib/Bot/Games/Trait/Method/Command.pm delete mode 100644 lib/Bot/Games/Trait/Method/Formatted.pm diff --git a/bin/run b/bin/run index 1d24279..011e74a 100644 --- a/bin/run +++ b/bin/run @@ -2,13 +2,21 @@ use strict; use warnings; use lib 'lib'; +use IM::Engine; use Bot::Games; -Bot::Games->new( - server => 'irc.efnet.net', - channels => ['#netmonster'], - nick => 'doygames', - username => 'doygames', - name => 'doygames', - prefix => '@', +IM::Engine->new( + interface => { + protocol => 'REPL', + }, + plugins => [ + Commands => { + namespace => 'Bot::Games::Game', + exclude_commands => qr/Bot::Games::Game::Role/, + prefix => '@', + alias => { + sg => 'superghost', + }, + }, + ], )->run; diff --git a/dist.ini b/dist.ini index 7291dae..56a947e 100644 --- a/dist.ini +++ b/dist.ini @@ -8,7 +8,7 @@ copyright_holder = Jesse Luehrs [@Classic] [Prereq] -Bot::BasicBot = 0 +IM::Engine::Plugin::Commands = 0 DateTime = 0 Math::Expression::Evaluator = 0 Module::Pluggable = 0 @@ -16,6 +16,5 @@ MooseX::AttributeHelpers = 0.18_01 Moose = 0.64 List::Util = 1.05 Games::Word = 0.04 -MooseX::NonMoose = 0.01 Time::Duration = 0 Chess::Rep = 0 diff --git a/lib/Bot/Games.pm b/lib/Bot/Games.pm index 6f40d4f..ce7fc66 100644 --- a/lib/Bot/Games.pm +++ b/lib/Bot/Games.pm @@ -1,159 +1,5 @@ package Bot::Games; -use Bot::Games::OO; -use List::Util qw/first/; -use Module::Pluggable - search_path => 'Bot::Games::Game', - except => qr/Bot::Games::Game::Role/, - sub_name => 'games'; -extends 'Bot::BasicBot'; - -has prefix => ( - is => 'rw', - isa => 'Str', - default => '!', -); - -has active_games => ( - is => 'ro', - isa => 'HashRef[Bot::Games::Game]', - lazy => 1, - default => sub { {} }, - init_arg => undef, -); - -has alias => ( - is => 'ro', - isa => 'HashRef[Str]', - lazy => 1, - default => sub { { - sg => 'superghost', - xg => 'xghost', - } }, -); - -my $say; -my $say_args; - -sub BUILD { - my $self = shift; - $say = sub { - shift; - unshift @_, 'body' if @_ % 2 == 1; - my %overrides = @_; - my $formatter = delete $overrides{formatter}; - $overrides{body} = $formatter->($overrides{body}) - if $formatter && exists $overrides{body}; - return $self->say(%$say_args, %overrides); - }; - require Bot::Games::Game; - Bot::Games::Game->meta->add_method(say => $say); - Bot::Games::Game->meta->make_immutable; -} - -sub said { - my $self = shift; - my ($args) = @_; - my $prefix = $self->prefix; - $say_args = $args; - - return if $args->{channel} eq 'msg'; - return unless $args->{body} =~ /^$prefix(\w+)(?:\s+(.*))?/; - my ($game_name, $action) = (lc($1), $2); - return join ' ', map { $self->prefix . $_} $self->game_list - if $game_name eq 'games'; - if ($game_name eq 'help') { - $game_name = $action; - $game_name =~ s/^-//; - $action = '-help'; - } - $game_name = $self->find_game($game_name); - return unless $game_name; - - my $output; - my $game = $self->active_games->{$game_name}; - if (!defined $game) { - my $game_package = $self->game_package($game_name); - eval "require $game_package"; - if ($@) { - warn $@; - $self->$say((split /\n/, $@)[0]); - return; - } - $game = $game_package->new; - $self->active_games->{$game_name} = $game; - } - if (!$self->active_games->{$game_name}->is_active - && (!defined($action) || $action !~ /^-/)) { - $self->$say($game->init($args->{who})) if $game->can('init'); - $self->active_games->{$game_name}->is_active(1); - } - - return unless defined $action; - - if ($action =~ /^-(\w+)\s*(.*)/) { - my ($action, $arg) = ($1, $2); - if (my $method_meta = $game->meta->get_command($action)) { - if ($method_meta->needs_init - && !$self->active_games->{$game_name}->is_active) { - $self->$say("Game $game_name hasn't started yet!"); - return; - } - my $body = $method_meta->execute($game, $arg, - {player => $args->{who}}); - my @extra_args = $method_meta->meta->does_role('Bot::Games::Trait::Method::Formatted') ? (formatter => $method_meta->formatter) : (); - $self->$say($body, @extra_args); - } - else { - $self->$say("Unknown command $action for game $game_name"); - return; - } - } - else { - # XXX: need better handling for "0", but B::BB doesn't currently - # handle that properly either, so - # also, this should probably be factored into $say, i think? - my $turn = $game->turn($args->{who}, $action); - $self->$say($turn) if $turn; - } - - if (!$game->is_active) { - delete $self->active_games->{$game_name}; - } - - return; -} - -sub valid_game { - my $self = shift; - my ($name) = @_; - my $package = $self->game_package($name); - return (grep { $package eq $_ } $self->games) ? 1 : 0; -} - -sub game_package { - my $self = shift; - my ($name) = @_; - return first { /\Q::$name\E$/i } $self->games; -} - -sub game_list { - my $self = shift; - return sort map { s/Bot::Games::Game:://; lc } $self->games; -} - -sub find_game { - my $self = shift; - my ($abbrev) = @_; - return $abbrev if $self->valid_game($abbrev); - return $self->alias->{$abbrev} - if exists $self->alias->{$abbrev} - && $self->valid_game($self->alias->{$abbrev}); - my @possibilities = grep { /^$abbrev/ } $self->game_list; - return $possibilities[0] if @possibilities == 1; - return; -} - -__PACKAGE__->meta->make_immutable; -no Bot::Games::OO; - +use strict; +use warnings; +use IM::Engine::Plugin::Commands; 1; diff --git a/lib/Bot/Games/Game.pm b/lib/Bot/Games/Game.pm index d57461d..a9d0862 100644 --- a/lib/Bot/Games/Game.pm +++ b/lib/Bot/Games/Game.pm @@ -1,18 +1,10 @@ package Bot::Games::Game; -use Bot::Games::OO::Game; +use Bot::Games::OO; use DateTime; use Time::Duration; -has help => ( - is => 'ro', - isa => 'Str', - default => 'This game doesn\'t have any help text!', - command => 1, - needs_init => 0, -); - has players => ( - traits => ['Collection::Array'], + traits => ['MooseX::AttributeHelpers::Trait::Collection::Array'], is => 'ro', isa => 'ArrayRef[Str]', auto_deref => 1, @@ -55,19 +47,12 @@ has last_turn_time => ( }, ); -has is_active => ( - is => 'rw', - isa => 'Bool', - command => 1, - needs_init => 0, -); - -sub turn { - my $turn = inner(); - return $turn if defined($turn); - return "Games must provide a turn method"; +sub default { + my $self = shift; + return "Games must provide a turn method" unless $self->can('turn'); + $self->turn(@_); } -after turn => sub { shift->last_turn_time(DateTime->now) }; +after default => sub { shift->last_turn_time(DateTime->now) }; sub allow_new_player { 1 } around add_player => sub { @@ -80,25 +65,9 @@ around add_player => sub { return; }; -command cmdlist => sub { - my $self = shift; - my @commands; - for my $method ($self->meta->get_all_methods) { - push @commands, $method->name - if $method->meta->can('does_role') - && $method->meta->does_role('Bot::Games::Trait::Method::Command'); - } - return \@commands; -}, needs_init => 0, - formatter => sub { - my $list = shift; - return join ' ', sort map { '-' . $_ } @$list - }; - sub _diff_from_now { ago(time - shift->epoch, 3) } -# this happens in Bot::Games, since we want to add the say method from there -#__PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +__PACKAGE__->meta->make_immutable; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/24.pm b/lib/Bot/Games/Game/24.pm index a353900..dd072c4 100644 --- a/lib/Bot/Games/Game/24.pm +++ b/lib/Bot/Games/Game/24.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::24; -use Bot::Games::OO::Game; +use Bot::Games::OO; extends 'Bot::Games::Game'; use List::Util qw/shuffle/; @@ -28,7 +28,7 @@ sub init { return $self->generate_24; } -augment turn => sub { +sub turn { my $self = shift; my ($player, $expr) = @_; $self->add_player($player) unless $self->has_player($player); @@ -101,6 +101,6 @@ sub evaluate { } __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/Chess.pm b/lib/Bot/Games/Game/Chess.pm index 554fe9e..c3eb14f 100644 --- a/lib/Bot/Games/Game/Chess.pm +++ b/lib/Bot/Games/Game/Chess.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Chess; -use Bot::Games::OO::Game; +use Bot::Games::OO; extends 'Bot::Games::Game'; with 'Bot::Games::Game::Role::CurrentPlayer'; @@ -17,7 +17,7 @@ has game => ( ); has turn_count => ( - traits => ['Counter'], + traits => ['MooseX::AttributeHelpers::Trait::Counter'], is => 'ro', isa => 'Int', default => 1, @@ -26,7 +26,7 @@ has turn_count => ( } ); -augment turn => sub { +sub turn { my $self = shift; my ($player, $move) = @_; $self->maybe_add_player($player); @@ -91,6 +91,6 @@ sub format_turn { } __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/Ghost.pm b/lib/Bot/Games/Game/Ghost.pm index 6f8ad83..97dbbb9 100644 --- a/lib/Bot/Games/Game/Ghost.pm +++ b/lib/Bot/Games/Game/Ghost.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Ghost; -use Bot::Games::OO::Game; +use Bot::Games::OO; use Games::Word::Wordlist; extends 'Bot::Games::Game'; with 'Bot::Games::Game::Role::CurrentPlayer'; @@ -41,7 +41,7 @@ around state => sub { return $self->$orig($state); }; -augment turn => sub { +sub turn { my $self = shift; my ($player, $state) = @_; $self->maybe_add_player($player); @@ -126,6 +126,6 @@ around maybe_add_player => sub { }; __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/Role/CurrentPlayer.pm b/lib/Bot/Games/Game/Role/CurrentPlayer.pm index 2dbe524..1479ef8 100644 --- a/lib/Bot/Games/Game/Role/CurrentPlayer.pm +++ b/lib/Bot/Games/Game/Role/CurrentPlayer.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Role::CurrentPlayer; -use Bot::Games::OO::Game::Role; +use Bot::Games::OO::Role; requires 'players', 'num_players', 'add_player'; @@ -40,6 +40,6 @@ sub maybe_add_player { } } -no Bot::Games::OO::Game::Role; +no Bot::Games::OO::Role; 1; diff --git a/lib/Bot/Games/Game/Spook.pm b/lib/Bot/Games/Game/Spook.pm index cfb577f..7cecca0 100644 --- a/lib/Bot/Games/Game/Spook.pm +++ b/lib/Bot/Games/Game/Spook.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Spook; -use Bot::Games::OO::Game; +use Bot::Games::OO; use Games::Word qw/is_subpermutation/; extends 'Bot::Games::Game::Ghost'; @@ -23,6 +23,6 @@ command valid_word_from_state => sub { }, formatter => 'Bool'; __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/Superghost.pm b/lib/Bot/Games/Game/Superghost.pm index f286425..3cc593e 100644 --- a/lib/Bot/Games/Game/Superghost.pm +++ b/lib/Bot/Games/Game/Superghost.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Superghost; -use Bot::Games::OO::Game; +use Bot::Games::OO; extends 'Bot::Games::Game::Ghost'; has '+help' => ( @@ -21,6 +21,6 @@ command valid_word_from_state => sub { }, formatter => 'Bool'; __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/Game/Xghost.pm b/lib/Bot/Games/Game/Xghost.pm index 6973ab4..799d7ce 100644 --- a/lib/Bot/Games/Game/Xghost.pm +++ b/lib/Bot/Games/Game/Xghost.pm @@ -1,5 +1,5 @@ package Bot::Games::Game::Xghost; -use Bot::Games::OO::Game; +use Bot::Games::OO; use Games::Word qw/is_substring/; extends 'Bot::Games::Game::Ghost'; @@ -21,6 +21,6 @@ command valid_word_from_state => sub { }, formatter => 'Bool'; __PACKAGE__->meta->make_immutable; -no Bot::Games::OO::Game; +no Bot::Games::OO; 1; diff --git a/lib/Bot/Games/OO.pm b/lib/Bot/Games/OO.pm index 177af74..119718d 100644 --- a/lib/Bot/Games/OO.pm +++ b/lib/Bot/Games/OO.pm @@ -1,24 +1,10 @@ package Bot::Games::OO; -use Moose (); +use IM::Engine::Plugin::Commands::OO (); use MooseX::AttributeHelpers; use Moose::Exporter; -use Moose::Util::MetaRole; Moose::Exporter->setup_import_methods( - also => ['Moose'], + also => ['IM::Engine::Plugin::Commands::OO'], ); -sub init_meta { - shift; - my %options = @_; - Moose->init_meta(%options); - Moose::Util::MetaRole::apply_metaclass_roles( - for_class => $options{for_class}, - metaclass_roles => ['MooseX::NonMoose::Meta::Role::Class'], - constructor_metaclass_roles => - ['MooseX::NonMoose::Meta::Role::Constructor'], - ); - return $options{for_class}->meta; -} - 1; diff --git a/lib/Bot/Games/OO/Game.pm b/lib/Bot/Games/OO/Game.pm deleted file mode 100644 index 5820b80..0000000 --- a/lib/Bot/Games/OO/Game.pm +++ /dev/null @@ -1,65 +0,0 @@ -package Bot::Games::OO::Game; -use Bot::Games::OO (); - -sub command { - my $caller = shift; - my $name = shift; - my $code; - $code = shift if ref($_[0]) eq 'CODE'; - my %args = @_; - - my $method_meta = $caller->meta->get_method($name); - my $superclass = Moose::blessed($method_meta) - || $caller->meta->method_metaclass; - my @method_metaclass_roles = ('Bot::Games::Trait::Method::Command'); - push @method_metaclass_roles, 'Bot::Games::Trait::Method::Formatted' - if $args{formatter}; - my $method_metaclass = Moose::Meta::Class->create_anon_class( - superclasses => [$superclass], - roles => \@method_metaclass_roles, - cache => 1, - ); - if ($method_meta) { - $method_metaclass->rebless_instance($method_meta); - } - else { - $method_meta = $method_metaclass->name->wrap( - $code, - package_name => $caller, - name => $name, - ); - $caller->meta->add_method($name, $method_meta); - } - for my $attr (map { $_->meta->get_attribute_list } @method_metaclass_roles) { - next unless exists $args{$attr}; - my $value = $args{$attr}; - my $munge_method = "_munge_$attr"; - $value = $method_meta->$munge_method($value) - if $method_meta->can($munge_method); - $method_meta->$attr($value); - } -} - -Moose::Exporter->setup_import_methods( - with_caller => ['command'], - also => ['Bot::Games::OO'], -); - -sub init_meta { - shift; - my %options = @_; - Moose->init_meta(%options); - Moose::Util::MetaRole::apply_metaclass_roles( - for_class => - $options{for_class}, - attribute_metaclass_roles => - ['Bot::Games::Trait::Attribute::Command', - 'Bot::Games::Trait::Attribute::Formatted'], - metaclass_roles => - ['Bot::Games::Trait::Class::Command', - 'Bot::Games::Trait::Class::Formatted'], - ); - return $options{for_class}->meta; -} - -1; diff --git a/lib/Bot/Games/OO/Game/Role.pm b/lib/Bot/Games/OO/Game/Role.pm deleted file mode 100644 index 8aee3a4..0000000 --- a/lib/Bot/Games/OO/Game/Role.pm +++ /dev/null @@ -1,28 +0,0 @@ -package Bot::Games::OO::Game::Role; -use Bot::Games::OO::Role (); - -# XXX: is there a better way to go about this? -*command = \&Bot::Games::OO::Game::command; -Moose::Exporter->setup_import_methods( - with_caller => ['command'], - also => ['Bot::Games::OO::Role'], -); - -sub init_meta { - shift; - my %options = @_; - Moose::Role->init_meta(%options); - Moose::Util::MetaRole::apply_metaclass_roles( - for_class => - $options{for_class}, - attribute_metaclass_roles => - ['Bot::Games::Trait::Attribute::Command', - 'Bot::Games::Trait::Attribute::Formatted'], - metaclass_roles => - ['Bot::Games::Trait::Class::Command', - 'Bot::Games::Trait::Class::Formatted'], - ); - return $options{for_class}->meta; -} - -1; diff --git a/lib/Bot/Games/OO/Role.pm b/lib/Bot/Games/OO/Role.pm index 80810f2..43bcf4f 100644 --- a/lib/Bot/Games/OO/Role.pm +++ b/lib/Bot/Games/OO/Role.pm @@ -1,10 +1,10 @@ package Bot::Games::OO::Role; -use Moose (); +use IM::Engine::Plugin::Commands::OO::Role (); use MooseX::AttributeHelpers; use Moose::Exporter; Moose::Exporter->setup_import_methods( - also => ['Moose::Role'], + also => ['IM::Engine::Plugin::Commands::OO::Role'], ); 1; diff --git a/lib/Bot/Games/Trait/Attribute/Command.pm b/lib/Bot/Games/Trait/Attribute/Command.pm deleted file mode 100644 index acc306e..0000000 --- a/lib/Bot/Games/Trait/Attribute/Command.pm +++ /dev/null @@ -1,62 +0,0 @@ -package Bot::Games::Trait::Attribute::Command; -use Bot::Games::OO::Role; - -has command => ( - is => 'rw', - isa => 'Bool', - default => 0, -); - -has needs_init => ( - is => 'rw', - isa => 'Bool', - default => 1, -); - -has _use_accessor_metatrait => ( - is => 'rw', - isa => 'Bool', - default => 0, -); - -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 && $self->_use_accessor_metatrait; - 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); -}; - -around _process_accessors => sub { - my $orig = shift; - my $self = shift; - my ($type) = @_; - $self->_use_accessor_metatrait(1) if $type eq 'accessor' - || $type eq 'reader'; - my @ret = $self->$orig(@_); - $self->_use_accessor_metatrait(0); - return @ret; -}; - -no Bot::Games::OO::Role; - -1; diff --git a/lib/Bot/Games/Trait/Attribute/Formatted.pm b/lib/Bot/Games/Trait/Attribute/Formatted.pm deleted file mode 100644 index 00db707..0000000 --- a/lib/Bot/Games/Trait/Attribute/Formatted.pm +++ /dev/null @@ -1,58 +0,0 @@ -package Bot::Games::Trait::Attribute::Formatted; -use Bot::Games::OO::Role; - -# when the attribute is being constructed, the accessor methods haven't been -# generated yet, so we need to store the formatter here, and then apply it -# after the accessor methods exist -has formatter => ( - is => 'rw', - isa => 'CodeRef', - predicate => 'has_formatter', -); - -before _process_options => sub { - my $self = shift; - my ($name, $options) = @_; - warn "only commands will have a formatter applied" - if exists($options->{formatter}) && !$options->{command}; -}; - -after attach_to_class => sub { - my $self = shift; - my ($meta) = @_; - return if $self->has_formatter; - return unless $self->command; - return unless $self->has_type_constraint; - my $tc = $self->type_constraint; - for my $tc_name ($meta->formattable_tcs) { - if ($tc->is_a_type_of($tc_name)) { - $self->formatter($meta->formatter_for($tc_name)); - return; - } - } -}; - -around accessor_metaclass => sub { - my $orig = shift; - my $self = shift; - my $metaclass = $self->$orig(@_); - return $metaclass unless $self->has_formatter; - return Moose::Meta::Class->create_anon_class( - superclasses => [$metaclass], - roles => ['Bot::Games::Trait::Method::Formatted'], - cache => 1, - )->name; -}; - -after install_accessors => sub { - my $self = shift; - if ($self->has_formatter) { - my $formatter = $self->formatter; - my $method_meta = $self->get_read_method_ref; - $method_meta->formatter($formatter); - } -}; - -no Bot::Games::OO::Role; - -1; diff --git a/lib/Bot/Games/Trait/Class/Command.pm b/lib/Bot/Games/Trait/Class/Command.pm deleted file mode 100644 index 8e70200..0000000 --- a/lib/Bot/Games/Trait/Class/Command.pm +++ /dev/null @@ -1,33 +0,0 @@ -package Bot::Games::Trait::Class::Command; -use Bot::Games::OO::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); -}); - -sub get_command { - my $self = shift; - my ($action) = @_; - my $method_meta = $self->find_method_by_name($action); - return $method_meta - if blessed($method_meta) - && $method_meta->meta->can('does_role') - && $method_meta->meta->does_role('Bot::Games::Trait::Method::Command'); -} - -no Bot::Games::OO::Role; - -1; diff --git a/lib/Bot/Games/Trait/Class/Formatted.pm b/lib/Bot/Games/Trait/Class/Formatted.pm deleted file mode 100644 index 6c3092a..0000000 --- a/lib/Bot/Games/Trait/Class/Formatted.pm +++ /dev/null @@ -1,26 +0,0 @@ -package Bot::Games::Trait::Class::Formatted; -use Bot::Games::OO::Role; - -has default_formatters => ( - metaclass => 'Collection::ImmutableHash', - is => 'ro', - isa => 'HashRef[CodeRef]', - builder => '_build_default_formatters', - provides => { - get => 'formatter_for', - exists => 'has_formatter', - keys => 'formattable_tcs', - }, -); - -sub _build_default_formatters { - { - 'ArrayRef' => sub { join ', ', @{ shift() } }, - 'Bool' => sub { return shift() ? 'true' : 'false' }, - 'Object' => sub { shift() . "" }, - } -} - -no Bot::Games::OO::Role; - -1; diff --git a/lib/Bot/Games/Trait/Method/Command.pm b/lib/Bot/Games/Trait/Method/Command.pm deleted file mode 100644 index 25bd2ff..0000000 --- a/lib/Bot/Games/Trait/Method/Command.pm +++ /dev/null @@ -1,24 +0,0 @@ -package Bot::Games::Trait::Method::Command; -use Bot::Games::OO::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 Bot::Games::OO::Role; - -1; diff --git a/lib/Bot/Games/Trait/Method/Formatted.pm b/lib/Bot/Games/Trait/Method/Formatted.pm deleted file mode 100644 index cdd4c60..0000000 --- a/lib/Bot/Games/Trait/Method/Formatted.pm +++ /dev/null @@ -1,22 +0,0 @@ -package Bot::Games::Trait::Method::Formatted; -use Bot::Games::OO::Role; - -has formatter => ( - is => 'rw', - isa => 'CodeRef', - default => sub { sub { - warn "no formatter specified!"; - return @_; - } }, -); - -sub _munge_formatter { - my $self = shift; - my ($format) = @_; - return $format if ref($format) eq 'CODE'; - return $self->associated_metaclass->formatter_for($format); -} - -no Bot::Games::OO::Role; - -1; -- cgit v1.2.3-54-g00ecf