File Coverage

blib/lib/Bot/Backbone/Dispatcher/Predicate.pm
Criterion Covered Total %
statement 72 96 75.0
branch 0 4 0.0
condition n/a
subroutine 24 32 75.0
pod 0 8 0.0
total 96 140 68.5


line stmt bran cond sub pod time code
1             package Bot::Backbone::Dispatcher::Predicate;
2             $Bot::Backbone::Dispatcher::Predicate::VERSION = '0.142820';
3 3     3   24 use v5.10;
  3         7  
  3         99  
4 3     3   1220 use Moose::Role;
  3         10769  
  3         15  
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.142820';
14 3     3   12798 use v5.10;
  3         30  
  3         98  
15 3     3   12 use Moose;
  3         3  
  3         17  
16              
17             with 'Bot::Backbone::Dispatcher::Predicate';
18              
19             has name => ( is => 'ro', isa => 'Str', required => 1 );
20              
21             sub do_it {
22 0     0 0   my ($self, $service, $message) = @_;
23              
24 0           my $redispatch_service = $service->get_service($self->name);
25 0           return $redispatch_service->dispatch_message($message);
26             }
27              
28             sub more_predicates {
29 0     0 0   my ($self, $service) = @_;
30              
31 0           my $redispatch_service = $service->get_service($self->name);
32 0           my $dispatcher = $redispatch_service->dispatcher;
33             return (
34 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.142820';
45 3     3   14272 use v5.10;
  3         8  
  3         96  
46 3     3   11 use Moose;
  3         2  
  3         13  
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 0     0 0   my ($self, $service) = @_;
65 0           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.142820';
74 3     3   13785 use v5.10;
  3         15  
  3         135  
75 3     3   11 use Moose;
  3         4  
  3         15  
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.142820';
104 3     3   13756 use v5.10;
  3         7  
  3         110  
105 3     3   12 use Moose;
  3         3  
  3         13  
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.142820';
125 3     3   13502 use v5.10;
  3         8  
  3         95  
126 3     3   11 use Moose;
  3         4  
  3         11  
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.142820';
161 3     3   13396 use v5.10;
  3         8  
  3         165  
162 3     3   16 use Moose;
  3         3  
  3         14  
163              
164             extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
165              
166 3     3   14846 use Bot::Backbone::Types qw( VolumeLevel );
  3         30449  
  3         21  
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.142820';
191 3     3   4607 use v5.10;
  3         8  
  3         135  
192 3     3   14 use Moose;
  3         4  
  3         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.142820';
265 3     3   15085 use v5.10;
  3         8  
  3         97  
266 3     3   12 use Moose::Role;
  3         7  
  3         21  
267              
268 3     3   11154 use Bot::Backbone::Types qw( DispatcherType );
  3         5  
  3         15  
269              
270             has dispatcher_type => (
271             is => 'ro',
272             isa => DispatcherType,
273             required => 1,
274             coerce => 1,
275             );
276              
277             sub select_invocant {
278 0     0 0   my ($self, $service) = @_;
279 0 0         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.142820';
296 3     3   4148 use v5.10;
  3         8  
  3         93  
297 3     3   12 use Moose;
  3         4  
  3         13  
298              
299             with qw(
300             Bot::Backbone::Dispatcher::Predicate
301             Bot::Backbone::Dispatcher::Predicate::Functor
302             );
303              
304             sub do_it {
305 0     0 0   my ($self, $service, $message) = @_;
306              
307 0           my $invocant = $self->select_invocant($service);
308 0           my @responses = $self->call_the_code($invocant, $message);
309 0 0         if (@responses) {
310 0           for my $response (@responses) {
311 0           $message->reply($invocant, $response);
312             }
313              
314 0           return 1;
315             }
316              
317 0           return '';
318             }
319              
320 0     0 0   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.142820';
328              
329 3     3   14114 use v5.10;
  3         8  
  3         96  
330 3     3   12 use Moose;
  3         4  
  3         11  
331              
332             with qw(
333             Bot::Backbone::Dispatcher::Predicate
334             Bot::Backbone::Dispatcher::Predicate::Functor
335             );
336              
337             sub do_it {
338 0     0 0   my ($self, $service, $message) = @_;
339 0           my $invocant = $self->select_invocant($service);
340 0           return $self->call_the_code($invocant, $message);
341             }
342              
343 0     0 0   sub more_predicates { () }
344              
345             __PACKAGE__->meta->make_immutable;
346             }
347              
348             1;
349              
350             __END__
351              
352             =pod
353              
354             =encoding UTF-8
355              
356             =head1 NAME
357              
358             Bot::Backbone::Dispatcher::Predicate - Defines the predicate packages responsible for aiding dispatch
359              
360             =head1 VERSION
361              
362             version 0.142820
363              
364             =head1 DESCRIPTION
365              
366             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.
367              
368             =head1 AUTHOR
369              
370             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
371              
372             =head1 COPYRIGHT AND LICENSE
373              
374             This software is copyright (c) 2014 by Qubling Software LLC.
375              
376             This is free software; you can redistribute it and/or modify it under
377             the same terms as the Perl 5 programming language system itself.
378              
379             =cut