File Coverage

blib/lib/Bot/Backbone/Dispatcher/Predicate.pm
Criterion Covered Total %
statement 80 85 94.1
branch 3 4 75.0
condition n/a
subroutine 31 32 96.8
pod 0 8 0.0
total 114 129 88.3


line stmt bran cond sub pod time code
1             package Bot::Backbone::Dispatcher::Predicate;
2             $Bot::Backbone::Dispatcher::Predicate::VERSION = '0.160630';
3 4     4   26 use v5.10;
  4         7  
4 4     4   1419 use Moose::Role;
  4         11561  
  4         10  
5              
6             # ABSTRACT: Defines the predicate packages responsible for aiding dispatch
7              
8              
9             requires qw( do_it more_predicates );
10              
11             {
12                 package Bot::Backbone::Dispatcher::Predicate::RedispatchTo;
13             $Bot::Backbone::Dispatcher::Predicate::RedispatchTo::VERSION = '0.160630';
14 4     4   14072 use v5.10;
  4         36  
15 4     4   13     use Moose;
  4         3  
  4         18  
16              
17                 with 'Bot::Backbone::Dispatcher::Predicate';
18              
19                 has name => ( is => 'ro', isa => 'Str', required => 1 );
20              
21                 sub do_it {
22 13     13 0 24         my ($self, $service, $message) = @_;
23              
24 13         389         my $redispatch_service = $service->get_service($self->name);
25 13         39         return $redispatch_service->dispatch_message($message);
26                 }
27              
28                 sub more_predicates {
29 0     0 0 0         my ($self, $service) = @_;
30              
31 0         0         my $redispatch_service = $service->get_service($self->name);
32 0         0         my $dispatcher = $redispatch_service->dispatcher;
33                     return (
34 0         0             $dispatcher->list_predicates,
35                         $dispatcher->list_also_predicates,
36                     );
37                 }
38              
39                 __PACKAGE__->meta->make_immutable;
40             }
41              
42             {
43                 package Bot::Backbone::Dispatcher::Predicate::Nesting;
44             $Bot::Backbone::Dispatcher::Predicate::Nesting::VERSION = '0.160630';
45 4     4   15881 use v5.10;
  4         8  
46 4     4   12     use Moose;
  4         4  
  4         12  
47              
48                 has next_predicate => (
49                     is => 'ro',
50                     isa => 'Bot::Backbone::Dispatcher::Predicate',
51                     required => 1,
52                     handles => [ 'do_it' ],
53                 );
54              
55                 with 'Bot::Backbone::Dispatcher::Predicate';
56              
57             # This is what handles => [ 'do_it' ] is doing above
58             # sub do_it {
59             # my ($self, $service, $message) = @_;
60             # return $self->next_predicate->do_it($service, $message);
61             # }
62              
63                 sub more_predicates {
64 2     2 0 3         my ($self, $service) = @_;
65 2         61         return ($self->next_predicate);
66                 }
67              
68                 __PACKAGE__->meta->make_immutable;
69             }
70              
71             {
72                 package Bot::Backbone::Dispatcher::Predicate::Command;
73             $Bot::Backbone::Dispatcher::Predicate::Command::VERSION = '0.160630';
74 4     4   15504 use v5.10;
  4         10  
75 4     4   13     use Moose;
  4         2  
  4         12  
76              
77                 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
78              
79                 has match => (
80                     is => 'rw',
81                     isa => 'Str|RegexpRef',
82                     required => 1,
83                 );
84              
85                 override do_it => sub {
86                     my ($self, $service, $message) = @_;
87              
88                     return $message->set_bookmark_do(sub {
89                         if ($message->match_next($self->match)) {
90                             $message->add_flag('command');
91                             return super();
92                         }
93              
94                         return '';
95                     });
96                 };
97              
98                 __PACKAGE__->meta->make_immutable;
99             }
100              
101             {
102                 package Bot::Backbone::Dispatcher::Predicate::NotCommand;
103             $Bot::Backbone::Dispatcher::Predicate::NotCommand::VERSION = '0.160630';
104 4     4   15095 use v5.10;
  4         9  
105 4     4   12     use Moose;
  4         9  
  4         12  
106              
107                 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
108              
109                 override do_it => sub {
110                     my ($self, $service, $message) = @_;
111              
112                     unless ($message->has_flag('command')) {
113                         return super();
114                     }
115              
116                     return '';
117                 };
118              
119                 __PACKAGE__->meta->make_immutable;
120             }
121              
122             {
123                 package Bot::Backbone::Dispatcher::Predicate::ToMe;
124             $Bot::Backbone::Dispatcher::Predicate::ToMe::VERSION = '0.160630';
125 4     4   14990 use v5.10;
  4         9  
126 4     4   12     use Moose;
  4         3  
  4         19  
127              
128                 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
129              
130                 has negate => (
131                     is => 'ro',
132                     isa => 'Bool',
133                     required => 1,
134                     default => '',
135                 );
136              
137                 override do_it => sub {
138                     my ($self, $service, $message) = @_;
139                     
140             # XORs break my brain, so just as a reminder...
141             #
142             # negate | is_to_me | xor result
143             # F | F | F
144             # F | T | T
145             # T | F | T
146             # T | T | F
147              
148                     if ($self->negate xor $message->is_to_me) {
149                         return super();
150                     }
151              
152                     return '';
153                 };
154              
155                 __PACKAGE__->meta->make_immutable;
156             }
157              
158             {
159                 package Bot::Backbone::Dispatcher::Predicate::Volume;
160             $Bot::Backbone::Dispatcher::Predicate::Volume::VERSION = '0.160630';
161 4     4   15124 use v5.10;
  4         13  
162 4     4   12     use Moose;
  4         3  
  4         23  
163              
164                 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
165              
166 4     4   16408     use Bot::Backbone::Types qw( VolumeLevel );
  4         35926  
  4         18  
167              
168                 has volume => (
169                     is => 'ro',
170                     isa => VolumeLevel,
171                     required => 1,
172                     default => 'spoken',
173                 );
174              
175                 override do_it => sub {
176                     my ($self, $service, $message) = @_;
177              
178                     if ($self->volume eq $message->volume) {
179                         return super();
180                     }
181              
182                     return '';
183                 };
184              
185                 __PACKAGE__->meta->make_immutable;
186             }
187              
188             {
189                 package Bot::Backbone::Dispatcher::Predicate::GivenParameters;
190             $Bot::Backbone::Dispatcher::Predicate::GivenParameters::VERSION = '0.160630';
191 4     4   4770 use v5.10;
  4         10  
192 4     4   13     use Moose;
  4         4  
  4         20  
193              
194                 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
195              
196                 has parameters => (
197                     is => 'ro',
198                     isa => 'ArrayRef',
199                     required => 1,
200                     traits => [ 'Array' ],
201                     handles => {
202                         all_parameters => 'elements',
203                     },
204                 );
205              
206                 override do_it => sub {
207                     my ($self, $service, $message) = @_;
208              
209                     return $message->set_bookmark_do(sub {
210                         for my $arg ($self->all_parameters) {
211                             my ($name, $config) = @$arg;
212              
213             # Match against ->args
214                             if (defined $config->{match}) {
215                                 my $match = $config->{match};
216              
217                                 if (exists $config->{default}
218                                         and not $message->has_more_args) {
219                                     $message->set_parameter($name => $config->{default});
220                                 }
221                                 else {
222                                     my $value = $message->match_next($match);
223                                     if (defined $value) {
224                                         $message->set_parameter($name => $value);
225                                     }
226                                     else {
227                                         return '';
228                                     }
229                                 }
230                             }
231              
232             # Match against ->text
233                             elsif (defined $config->{match_original}) {
234                                 my $match = $config->{match_original};
235              
236                                 my $value = $message->match_next_original($match);
237                                 if (defined $value) {
238                                     $message->set_parameter($name => $value);
239                                 }
240                                 elsif (exists $config->{default}) {
241                                     $message->set_parameter($name => $config->{default});
242                                 }
243                                 else {
244                                     return '';
245                                 }
246                             }
247              
248             # What the...?
249                             else {
250                                 Carp::carp("parameter $name missing 'match' or 'match_original'");
251                                 return '';
252                             }
253                         }
254              
255                         return super();
256                     });
257                 };
258              
259                 __PACKAGE__->meta->make_immutable;
260             }
261              
262             {
263                 package Bot::Backbone::Dispatcher::Predicate::Functor;
264             $Bot::Backbone::Dispatcher::Predicate::Functor::VERSION = '0.160630';
265 4     4   16583 use v5.10;
  4         12  
266 4     4   17     use Moose::Role;
  4         4  
  4         27  
267              
268 4     4   12711     use Bot::Backbone::Types qw( DispatcherType );
  4         4  
  4         17  
269              
270                 has dispatcher_type => (
271                     is => 'ro',
272                     isa => DispatcherType,
273                     required => 1,
274                     coerce => 1,
275                 );
276              
277                 sub select_invocant {
278 20     20 0 19         my ($self, $service) = @_;
279 20 100       589         return $self->dispatcher_type eq 'bot' ? $service->bot : $service;
280                 }
281              
282                 has the_code => (
283                     is => 'ro',
284                     isa => 'CodeRef',
285                     required => 1,
286                     traits => [ 'Code' ],
287                     handles => {
288                         'call_the_code' => 'execute',
289                     },
290                 );
291             }
292              
293             {
294                 package Bot::Backbone::Dispatcher::Predicate::Respond;
295             $Bot::Backbone::Dispatcher::Predicate::Respond::VERSION = '0.160630';
296 4     4   4859 use v5.10;
  4         9  
297 4     4   15     use Moose;
  4         5  
  4         17  
298              
299                 with qw(
300             Bot::Backbone::Dispatcher::Predicate
301             Bot::Backbone::Dispatcher::Predicate::Functor
302             );
303              
304                 sub do_it {
305 5     5 0 55         my ($self, $service, $message) = @_;
306              
307 5         11         my $invocant = $self->select_invocant($service);
308 5         174         my @responses = $self->call_the_code($invocant, $message);
309 5 50       739         if (@responses) {
310 5         8             for my $response (@responses) {
311 5         19                 $message->reply($invocant, $response);
312                         }
313              
314 5         838             return 1;
315                     }
316              
317 0         0         return '';
318                 }
319              
320 1     1 0 34     sub more_predicates { () }
321              
322                 __PACKAGE__->meta->make_immutable;
323             }
324              
325             {
326                 package Bot::Backbone::Dispatcher::Predicate::Run;
327             $Bot::Backbone::Dispatcher::Predicate::Run::VERSION = '0.160630';
328 4     4   15320 use v5.10;
  4         9  
329 4     4   14     use Moose;
  4         4  
  4         16  
330              
331                 with qw(
332             Bot::Backbone::Dispatcher::Predicate
333             Bot::Backbone::Dispatcher::Predicate::Functor
334             );
335              
336                 sub do_it {
337 15     15 0 158         my ($self, $service, $message) = @_;
338 15         34         my $invocant = $self->select_invocant($service);
339 15         510         return $self->call_the_code($invocant, $message);
340                 }
341              
342 1     1 0 35     sub more_predicates { () }
343              
344                 __PACKAGE__->meta->make_immutable;
345             }
346              
347             1;
348              
349             __END__
350            
351             =pod
352            
353             =encoding UTF-8
354            
355             =head1 NAME
356            
357             Bot::Backbone::Dispatcher::Predicate - Defines the predicate packages responsible for aiding dispatch
358            
359             =head1 VERSION
360            
361             version 0.160630
362            
363             =head1 DESCRIPTION
364            
365             Not much to see here unless you want to define custom predicates. If that is your goal, you must read the code. You probably also want to read the code in L<Bot::Backbone::DispatchSugar> while you're at it.
366            
367             =head1 AUTHOR
368            
369             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
370            
371             =head1 COPYRIGHT AND LICENSE
372            
373             This software is copyright (c) 2016 by Qubling Software LLC.
374            
375             This is free software; you can redistribute it and/or modify it under
376             the same terms as the Perl 5 programming language system itself.
377            
378             =cut
379