Bot-Backbone
view release on metacpan or search on metacpan
lib/Bot/Backbone.pm view on Meta::CPAN
# do nothing, we now have the exact name
}
else {
$class_name = join '::', 'Bot::Backbone', $section, $class_name;
}
Class::Load::load_class($class_name);
return $class_name;
}
sub send_policy($%) {
my ($meta, $name, @config) = @_;
my @final_config;
while (my ($class_name, $policy_config) = splice @config, 0, 2) {
$class_name = _resolve_class_name($meta, 'SendPolicy', $class_name);
push @final_config, [ $class_name, $policy_config ];
}
$meta->add_send_policy($name, \@final_config);
}
sub service($%) {
my ($meta, $name, %config) = @_;
my $class_name = _resolve_class_name($meta, 'Service', $config{service});
$config{service} = $class_name;
$meta->add_service($name, \%config);
if (my $service_meta = Moose::Util::find_meta($class_name)) {
Moose::Util::ensure_all_roles($meta, $service_meta->all_bot_roles)
if $service_meta->isa('Bot::Backbone::Meta::Class::Service')
and $service_meta->has_bot_roles;
}
}
sub dispatcher($$) {
my ($meta, $name, $code) = @_;
my $dispatcher = Bot::Backbone::Dispatcher->new;
{
$meta->building_dispatcher($dispatcher);
$code->();
$meta->no_longer_building_dispatcher;
}
$meta->add_dispatcher($name, $dispatcher);
lib/Bot/Backbone/DispatchSugar.pm view on Meta::CPAN
spoken shouted whispered
given_parameters
also
respond respond_by_method
run_this run_this_method
redispatch_to
) ],
as_is => [ qw( parameter as ) ],
);
sub redispatch_to($) {
my ($meta, $name) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::RedispatchTo->new(
name => $name,
)
);
}
sub also($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_also_predicate($predicate);
}
sub command($$) {
my ($meta, $match, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Command->new(
match => $match,
next_predicate => $predicate,
)
);
}
sub not_command($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::NotCommand->new(
next_predicate => $predicate,
)
);
}
sub to_me($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::ToMe->new(
next_predicate => $predicate,
)
);
}
sub not_to_me($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::ToMe->new(
negate => 1,
next_predicate => $predicate,
)
);
}
sub spoken($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Volume->new(
volume => 'spoken',
next_predicate => $predicate,
)
);
}
sub shouted($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Volume->new(
volume => 'shout',
next_predicate => $predicate,
)
);
}
sub whispered($) {
my ($meta, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Volume->new(
volume => 'whisper',
next_predicate => $predicate,
)
);
}
our $WITH_ARGS;
sub given_parameters(&$) {
my ($meta, $arg_code, $predicate) = @_;
my $dispatcher = $meta->building_dispatcher;
my @args;
{
local $WITH_ARGS = \@args;
$arg_code->();
}
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::GivenParameters->new(
parameters => \@args,
next_predicate => $predicate,
)
);
}
sub parameter($@) {
my ($name, %config) = @_;
push @$WITH_ARGS, [ $name, \%config ];
}
sub as(&) {
my $code = shift;
return $code;
}
sub _respond {
my ($meta, $code, $dispatcher_type) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher_type //= $meta;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Respond->new(
dispatcher_type => $dispatcher_type,
the_code => $code,
)
);
}
sub respond(&) {
my ($meta, $code, $dispatcher_type) = @_;
_respond($meta, $code, $dispatcher_type);
}
sub _run_this {
my ($meta, $code, $dispatcher_type) = @_;
my $dispatcher = $meta->building_dispatcher;
$dispatcher_type //= $meta;
$dispatcher->add_predicate_or_return(
Bot::Backbone::Dispatcher::Predicate::Run->new(
dispatcher_type => $dispatcher_type,
the_code => $code,
)
);
}
sub run_this(&) {
my ($meta, $code, $dispatcher_type) = @_;
_run_this($meta, $code, $dispatcher_type);
}
sub _by_method {
my ($meta, $name) = @_;
Carp::croak("no such method as $name found on ", $meta->name)
unless defined $meta->find_method_by_name($name);
lib/Bot/Backbone/DispatchSugar.pm view on Meta::CPAN
my $method = $self->can($name);
if (defined $method) {
return $self->$method($message);
}
else {
Carp::croak("no such method as $name found on ", $self->meta->name);
}
};
}
sub respond_by_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_respond($meta, \&$code);
}
sub run_this_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_run_this($meta, \&$code);
}
sub respond_by_service_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_respond($meta, \&$code, 'service');
}
sub respond_by_bot_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_respond($meta, \&$code, 'bot');
}
sub run_this_service_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_run_this($meta, \&$code, 'service');
}
sub run_this_bot_method($) {
my ($meta, $name) = @_;
my $code = _by_method($meta, $name);
_run_this($meta, \&$code, 'bot');
}
# These are documented in Bot::Backbone and Bot::Backbone::Service
lib/Bot/Backbone/Service.pm view on Meta::CPAN
};
sub with_bot_roles {
my ($meta, @roles) = @_;
Class::Load::load_class($_) for @roles;
$meta->add_bot_roles(@roles);
}
sub service_dispatcher($) {
my ($meta, $code) = @_;
ensure_all_roles($meta->name, 'Bot::Backbone::Service::Role::Dispatch');
$meta->dispatch_builder(sub {
my $dispatcher = Bot::Backbone::Dispatcher->new;
{
$meta->building_dispatcher($dispatcher);
$code->();
$meta->no_longer_building_dispatcher,
( run in 0.763 second using v1.01-cache-2.11-cpan-65fba6d93b7 )