aboutsummaryrefslogtreecommitdiff
path: root/Mock/CommandRegistry.pm
diff options
context:
space:
mode:
Diffstat (limited to 'Mock/CommandRegistry.pm')
-rw-r--r--Mock/CommandRegistry.pm53
1 files changed, 49 insertions, 4 deletions
diff --git a/Mock/CommandRegistry.pm b/Mock/CommandRegistry.pm
index 166879d..72c84cf 100644
--- a/Mock/CommandRegistry.pm
+++ b/Mock/CommandRegistry.pm
@@ -3,13 +3,58 @@ package Mock::CommandRegistry;
use strict;
use warnings;
-use fields qw/commands/;
+use fields qw/_commands/;
sub new {
- my ($class) = @_;
- bless {}, $class;
+ my Mock::CommandRegistry $self = shift;
+ unless ($self) {
+ $self = fields::new($self);
+ $self->{_commands} = {};
+ }
+ bless {}, $self;
}
-sub register {
+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;