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.161950';
3 4     4   29 use v5.10;
  4         8  
4 4     4   1480 use Moose::Role;
  4         12946  
  4         16  
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.161950';
14 4     4   16075 use v5.10;
  4         43  
15 4     4   14 use Moose;
  4         5  
  4         22  
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 36 my ($self, $service, $message) = @_;
23              
24 13         406 my $redispatch_service = $service->get_service($self->name);
25 13         51 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.161950';
45 4     4   17912 use v5.10;
  4         12  
46 4     4   13 use Moose;
  4         4  
  4         14  
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         59 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.161950';
74 4     4   17116 use v5.10;
  4         9  
75 4     4   15 use Moose;
  4         3  
  4         16  
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.161950';
104 4     4   17703 use v5.10;
  4         11  
105 4     4   20 use Moose;
  4         5  
  4         17  
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.161950';
125 4     4   19205 use v5.10;
  4         12  
126 4     4   25 use Moose;
  4         5  
  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.161950';
161 4     4   17711 use v5.10;
  4         10  
162 4     4   15 use Moose;
  4         6  
  4         24  
163              
164             extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
165              
166 4     4   17905 use Bot::Backbone::Types qw( VolumeLevel );
  4         39017  
  4         30  
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.161950';
191 4     4   5678 use v5.10;
  4         9  
192 4     4   17 use Moose;
  4         5  
  4         31  
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.161950';
265 4     4   18786 use v5.10;
  4         11  
266 4     4   15 use Moose::Role;
  4         4  
  4         30  
267              
268 4     4   13928 use Bot::Backbone::Types qw( DispatcherType );
  4         8  
  4         28  
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 22 my ($self, $service) = @_;
279 20 100       605 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.161950';
296 4     4   5669 use v5.10;
  4         21  
297 4     4   35 use Moose;
  4         5  
  4         21  
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 83 my ($self, $service, $message) = @_;
306              
307 5         21 my $invocant = $self->select_invocant($service);
308 5         179 my @responses = $self->call_the_code($invocant, $message);
309 5 50       996 if (@responses) {
310 5         10 for my $response (@responses) {
311 5         20 $message->reply($invocant, $response);
312             }
313              
314 5         882 return 1;
315             }
316              
317 0         0 return '';
318             }
319              
320 1     1 0 35 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.161950';
328 4     4   18678 use v5.10;
  4         13  
329 4     4   18 use Moose;
  4         7  
  4         22  
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 213 my ($self, $service, $message) = @_;
338 15         50 my $invocant = $self->select_invocant($service);
339 15         552 return $self->call_the_code($invocant, $message);
340             }
341              
342 1     1 0 36 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.161950
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