File Coverage

blib/lib/Bot/Backbone/Dispatcher.pm
Criterion Covered Total %
statement 12 27 44.4
branch 0 4 0.0
condition n/a
subroutine 4 9 44.4
pod 3 3 100.0
total 19 43 44.1


line stmt bran cond sub pod time code
1             package Bot::Backbone::Dispatcher;
2             $Bot::Backbone::Dispatcher::VERSION = '0.142820';
3 3     3   39 use v5.10;
  3         6  
  3         130  
4 3     3   13 use Moose;
  3         4  
  3         20  
5              
6 3     3   10031 use Bot::Backbone::Types qw( PredicateList );
  3         6  
  3         25  
7 3     3   5142 use Bot::Backbone::Dispatcher::PredicateIterator;
  3         846  
  3         1086  
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 0     0 1   my ($self, $service, $message) = @_;
41              
42 0           for my $predicate ($self->list_predicates) {
43             my $success = $message->set_bookmark_do(sub {
44 0     0     $predicate->do_it($service, $message);
45 0           });
46 0 0         last if $success;
47             }
48              
49 0           for my $predicate ($self->list_also_predicates) {
50 0     0     $message->set_bookmark_do(sub { $predicate->do_it($service, $message) });
  0            
51             }
52              
53 0           return;
54             }
55              
56              
57             sub add_predicate_or_return {
58 0     0 1   my ($self, $predicate, $options) = @_;
59              
60 0 0         if (defined wantarray) {
61 0           return $predicate;
62             }
63             else {
64 0           $self->add_predicate($predicate);
65             }
66             }
67              
68              
69             sub predicate_iterator {
70 0     0 1   my $self = shift;
71 0           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.142820
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) 2014 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