File Coverage

blib/lib/Bot/Backbone/Dispatcher.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package Bot::Backbone::Dispatcher;
2             $Bot::Backbone::Dispatcher::VERSION = '0.160630';
3 4     4   48 use v5.10;
  4         10  
4 4     4   16 use Moose;
  4         7  
  4         27  
5              
6 4     4   12589 use Bot::Backbone::Types qw( PredicateList );
  4         6  
  4         35  
7 4     4   6626 use Bot::Backbone::Dispatcher::PredicateIterator;
  4         1058  
  4         1201  
8              
9             # ABSTRACT: Simple dispatching tool
10              
11              
12             has predicates => (
13                 is => 'ro',
14                 isa => PredicateList,
15                 required => 1,
16                 default => sub { [] },
17                 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                 default => sub { [] },
30                 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     34 1 39     my ($self, $service, $message) = @_;
41              
42 34         1056     for my $predicate ($self->list_predicates) {
43                     my $success = $message->set_bookmark_do(sub {
44 192     192   496             $predicate->do_it($service, $message);
45 192         733         });
46 192 100       608         last if $success;
47                 }
48              
49 34         1122     for my $predicate ($self->list_also_predicates) {
50 12     12   52         $message->set_bookmark_do(sub { $predicate->do_it($service, $message) });
  12         34  
51                 }
52              
53 34         535     return;
54             }
55              
56              
57             sub add_predicate_or_return {
58 57     57 1 91     my ($self, $predicate, $options) = @_;
59              
60 57 100       85     if (defined wantarray) {
61 31         114         return $predicate;
62                 }
63                 else {
64 26         797         $self->add_predicate($predicate);
65                 }
66             }
67              
68              
69             sub predicate_iterator {
70 1     1 1 2     my $self = shift;
71 1         34     return Bot::Backbone::Dispatcher::PredicateIterator->new(
72                     dispatcher => $self,
73                 );
74             }
75              
76             __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
153