Backbone/Dispatcher.pm

filter: Statementstmt 100% 32 / 32 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 100% 50 / 50 CC 5 SCAR 6.6
CC
5
Cov
100%
dispatch_message
4.0
add_predicate_or_return
2.0
BEGIN
1.0
CRAP 1.9
« Backbone/Service/Role/Storage.pm ↑ Summary Backbone/Bot.pm »
1 package Bot::Backbone::Dispatcher;
2 4 $Bot::Backbone::Dispatcher::VERSION = '0.160630';
3 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Bot::Backbone::Types qw( PredicateList );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Bot::Backbone::Dispatcher::PredicateIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Simple dispatching tool
10  
11  
12 has predicates => (
13 is => 'ro',
14 isa => PredicateList,
15 required => 1,
16 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 traits => [ 'Array' ],
18 handles => {
19 add_predicate => 'push',
20 list_predicates => 'elements',
21 },
22 );
23  
24  
25 has also_predicates => (
26 is => 'ro',
27 isa => PredicateList,
28 required => 1,
29 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 4 traits => [ 'Array' ],
31 handles => {
32 add_also_predicate => 'push',
33 list_also_predicates => 'elements',
34 },
35  
36 );
37  
38  
39 sub dispatch_message {
40 34 ▶ my ($self, $service, $message) = @_;
Subroutine dispatch_message: called CC=4 SCAR=13.9
Pod documented
41  
42 34 for my $predicate ($self->list_predicates) {
43 my $success = $message->set_bookmark_do(sub {
44 192 ▶ $predicate->do_it($service, $message);
Subroutine __ANON__: called CC=1 SCAR=0.0
45 192 });
46 192 ▶ last if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 16 176 192
47 }
48  
49 34 for my $predicate ($self->list_also_predicates) {
50 12 ▶ $message->set_bookmark_do(sub { $predicate->do_it($service, $message) });
Subroutine __ANON__: called CC=1 SCAR=0.0
51 }
52  
53 34 return;
54 }
55  
56  
57 sub add_predicate_or_return {
58 57 ▶ my ($self, $predicate, $options) = @_;
Subroutine add_predicate_or_return: called CC=2 SCAR=6.9
Pod documented
59  
60 57 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 31 26 57
61 31 return $predicate;
62 }
63 else {
64 26 $self->add_predicate($predicate);
65 }
66 }
67  
68  
69 sub predicate_iterator {
70 1 ▶ my $self = shift;
Subroutine predicate_iterator: called CC=1 SCAR=0.0
Pod documented
71 1 return Bot::Backbone::Dispatcher::PredicateIterator->new(
72 dispatcher => $self,
73 );
74 }
75  
76 4 __PACKAGE__->meta->make_immutable;
77  
78 __END__
79  
80 =pod
81  
82 =encoding UTF-8
83  
84 =head1 NAME
85  
86 Bot::Backbone::Dispatcher - Simple dispatching tool
87  
88 =head1 VERSION
89  
90 version 0.160630
91  
92 =head1 SYNOPSIS
93  
94 my $dispatcher = Bot::Backbone::Dispatcher->new(
95 predicates => \@predicates,
96 also_predicates => \@also_predicates,
97 );
98  
99 my $message = Bot::Backbone::Message->new(...);
100 $dispatcher->dispatch_message($message);
101  
102 =head1 DESCRIPTION
103  
104 A dispatcher is an array of predicates that are each executed in turn. Each predicate is a subroutine that is run against the message that may or may not take an action against it and is expected to return a boolean value declaring whether any action was taken.
105  
106 =head1 ATTRIBUTES
107  
108 =head2 predicates
109  
110 Predicates in this list are executed sequentially and in order. The first predicate to return a true value causes execution to cease so that any further predicates are ignored.
111  
112 =head2 also_predicates
113  
114 This list of predicates are not guaranteed to execute sequentially or in any particular order. The return value of these predicates will be ignored and all will be executed on every message, even those that have already been handled by a predicate in the L</predicates> list.
115  
116 =head1 METHODS
117  
118 =head2 dispatch_message
119  
120 $dispatcher->dispatch_message($message);
121  
122 Given a L<Bot::Backbone::Message>, this will execute each predicate attached to the dispatcher, using the policies described under L</predicates> and L</also_predicates>.
123  
124 =head2 add_predicate_or_return
125  
126 $dispatcher->add_predicate_or_return($predicate);
127  
128 Chances are that you probably don't need this method. However, if you are creating a new kind of predicate, you will probably want this method.
129  
130 This will do the right thing to either add a root level predicate to the dispatcher or return the predicate for chaining within another predicate. You probably want to read the code in L<Bot::Backbone::DispatchSugar> for examples.
131  
132 =head2 predicate_iterator
133  
134 my $iterator = $dispatcher->predicate_iterator;
135 while (my $predicate = $iterator->next) {
136 # do something...
137 }
138  
139 Returns a L<Bot::Backbone::Dispatcher::PredicateIterator> for this dispatcher.
140  
141 =head1 AUTHOR
142  
143 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
144  
145 =head1 COPYRIGHT AND LICENSE
146  
147 This software is copyright (c) 2016 by Qubling Software LLC.
148  
149 This is free software; you can redistribute it and/or modify it under
150 the same terms as the Perl 5 programming language system itself.
151  
152 =cut
« Backbone/Service/Role/Storage.pm ↑ Summary Backbone/Bot.pm »