Last active
December 20, 2015 00:48
-
-
Save leedo/6043998 to your computer and use it in GitHub Desktop.
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
package Fuhbot::Plugin; | |
use v5.14; | |
use warnings; | |
use mop; | |
class Fuhbot::PluginClass extends mop::class { | |
has $commands = []; | |
has $routes = []; | |
has $events = []; | |
method events { $events } | |
method commands { $commands } | |
method routes { $routes } | |
method add_event { push @$events, [@_] } | |
method add_command { push @$commands, [@_] } | |
method add_route { push @$routes, [@_] } | |
} | |
# traits to be used by plugin methods | |
sub command { | |
if ($_[0]->isa('mop::method')) { | |
my ($method, $name) = @_; | |
$method->associated_meta->add_command($method, $name); | |
} | |
} | |
sub route { | |
if ($_[0]->isa('mop::method')) { | |
my ($method, $type, $pattern) = @_; | |
$method->associated_meta->add_route($method, $type, $pattern); | |
} | |
} | |
sub event { | |
if ($_[0]->isa('mop::method')) { | |
my ($method, $name) = @_; | |
$method->associated_meta->add_route($method, $name); | |
} | |
class Fuhbot::Plugin metaclass Fuhbot::PluginClass is abstract { | |
has $broadcast is ro; | |
has $brain is ro; | |
has $config is ro; | |
method prepare_plugin { } | |
method name ($key) { return $config->{$key} } | |
method broadcast (@msgs) { | |
if (@msgs) { | |
$broadcast->($_, $config->{ircs}) for @msgs; | |
} | |
} | |
method shorten ($url) { | |
my $cb = pop; | |
my %args = @_; | |
if (my $fmt = $self->config("shorten_format")) { | |
$args{format} = $fmt; | |
} | |
Fuhbot::Util::shorten($url, %args, $cb); | |
} | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment