Backbone/Service/Role/Dispatch.pm

filter: Statementstmt 100% 36 / 36 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 96.6% 58 / 60 CC 8 SCAR 9.1
CC
8
Cov
96%
_apply_command_rewrite
4.0
_build_dispatcher
3.0
dispatch_message
2.0
CRAP 2.5
« Backbone/Service/Role/ChatConsumer.pm ↑ Summary Backbone.pm »
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
BranchTrueFalseTotal
if ($self->has_dispatcher) { } 2 7 9
elsif ($self->meta->has_dispatch_builder) { } 3 4 7
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
BranchTrueFalseTotal
if (my $predicate = $iterator->next_predicate) 4 1 5
68 4 ▶ if ($predicate->isa('Bot::Backbone::Dispatcher::Predicate::Command')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicate->isa("Bot::Backbone::Dispatcher::Predicate::Command")) 2 2 4
69 2 ▶ if ($commands{ $predicate->match }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commands{$predicate->match}) 2 0 2
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
BranchTrueFalseTotal
if $self->has_custom_commands 1 8 9
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
BranchTrueFalseTotal
if ($self->has_dispatcher) 34 0 34
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__
« Backbone/Service/Role/ChatConsumer.pm ↑ Summary Backbone.pm »