package Mock::CommandRegistry; use strict; use warnings; use fields qw/_commands/; sub new { my Mock::CommandRegistry $self = shift; unless ($self) { $self = fields::new($self); $self->{_commands} = {}; } bless {}, $self; } sub run { my ($self, $command, %ctx) = @_; # XXX should run all commands matching? Only first? ... } sub run_owned { my ($self, $owner, $command, %ctx) = @_; die "$owner::$command not registered\n" unless $self->is_registered_to_owner($owner, $command); # XXX plugins need to eventually use the ctx hash directly. Much more # readable than remembering the long chain of magic positional args each # time you write a new cmd plugin my @args = ( $ctx{irc}, $ctx{logger}, $ctx{who}, $ctx{where}, $ctx{ided}, $ctx{rest}, $ctx{no_reenter}, $ctx{args} ); $self->{_commands}->{$owner}->{$command}->(@args); } sub is_registered { my ($self, $command) = @_; # XXX should tell if any owner registers a given command ... } sub is_registered_to_owner { my ($self, $owner, $command) = @_; defined $self->{_commands}->{$owner}->{$command}; } sub register { my ($self, $owner, $command, $coderef) = @_; $self->{_commands}->{$owner}->{$command} = $coderef; } 1;