| 1 | package Bot::Backbone::Service::Role::Dispatch; | ||||||||||||||
| 2 | 4 | $Bot::Backbone::Service::Role::Dispatch::VERSION = '0.161950'; | |||||||||||||
| 3 | 4 | ▶ | use v5.10; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 4 | with 'Bot::Backbone::Service::Role::SendPolicy'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | # ABSTRACT: Role for services that can perform dispatch | ||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | has dispatcher_name => ( | |||||||||||||
| 14 | is => 'rw', | ||||||||||||||
| 15 | isa => 'Str', | ||||||||||||||
| 16 | init_arg => 'dispatcher', | ||||||||||||||
| 17 | predicate => 'has_dispatcher', | ||||||||||||||
| 18 | ); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | |||||||||||||||
| 21 | 4 | has dispatcher => ( | |||||||||||||
| 22 | is => 'rw', | ||||||||||||||
| 23 | isa => 'Bot::Backbone::Dispatcher', | ||||||||||||||
| 24 | init_arg => undef, | ||||||||||||||
| 25 | lazy_build => 1, | ||||||||||||||
| 26 | predicate => 'has_setup_the_dispatcher', | ||||||||||||||
| 27 | ); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 9 | ▶ | sub _build_dispatcher { | ||||||||||||
|
Subroutine
_build_dispatcher: called CC=3 SCAR=11.0
| |||||||||||||||
| 30 | 9 | my $self = shift; | |||||||||||||
| 31 | |||||||||||||||
| 32 | # If a named dispatcher is given use that | ||||||||||||||
| 33 | 9 | ▶ | if ($self->has_dispatcher) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 34 | 2 | return $self->bot->meta->dispatchers->{ $self->dispatcher_name }; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # If we have a dispatch builder | ||||||||||||||
| 38 | elsif ($self->meta->has_dispatch_builder) { | ||||||||||||||
| 39 | 3 | $self->dispatcher_name('<service_dispatcher>'); | |||||||||||||
| 40 | 3 | return $self->meta->run_dispatch_builder; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | # Use an empty dispatcher | ||||||||||||||
| 44 | else { | ||||||||||||||
| 45 | 4 | $self->dispatcher_name('<empty>'); | |||||||||||||
| 46 | 4 | return Bot::Backbone::Dispatcher->new; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | 4 | has commands => ( | |||||||||||||
| 52 | is => 'ro', | ||||||||||||||
| 53 | isa => 'HashRef[Str]', | ||||||||||||||
| 54 | predicate => 'has_custom_commands', | ||||||||||||||
| 55 | traits => [ 'Hash' ], | ||||||||||||||
| 56 | handles => { | ||||||||||||||
| 57 | command_map => 'elements', | ||||||||||||||
| 58 | }, | ||||||||||||||
| 59 | ); | ||||||||||||||
| 60 | |||||||||||||||
| 61 | |||||||||||||||
| 62 | 1 | ▶ | sub _apply_command_rewrite { | ||||||||||||
|
Subroutine
_apply_command_rewrite: called CC=4 SCAR=13.9
| |||||||||||||||
| 63 | 1 | my $self = shift; | |||||||||||||
| 64 | 1 | my %commands = reverse $self->command_map; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 1 | my $iterator = $self->dispatcher->predicate_iterator; | |||||||||||||
| 67 | 1 | ▶ | while (my $predicate = $iterator->next_predicate) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 4 | ▶ | if ($predicate->isa('Bot::Backbone::Dispatcher::Predicate::Command')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 2 | ▶ | if ($commands{ $predicate->match }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | 2 | $predicate->match( $commands{ $predicate->match } ); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 9 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 77 | 9 | my $self = shift; | |||||||||||||
| 78 | |||||||||||||||
| 79 | 9 | ▶ | $self->_apply_command_rewrite if $self->has_custom_commands; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | |||||||||||||||
| 83 | 34 | ▶ | sub dispatch_message { | ||||||||||||
|
Subroutine
dispatch_message: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 84 | 34 | my ($self, $message) = @_; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 34 | ▶ | if ($self->has_dispatcher) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 34 | $self->dispatcher->dispatch_message($self, $message); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | 9 | ▶ | before initialize => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 93 | 9 | my $self = shift; | |||||||||||||
| 94 | 9 | $self->dispatcher; | |||||||||||||
| 95 | 4 | }; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 4 | 1; | |||||||||||||
| 98 | |||||||||||||||
| 99 | __END__ | ||||||||||||||