File Coverage

blib/lib/Bot/Backbone/DispatchSugar.pm
Criterion Covered Total %
statement 77 90 85.5
branch 2 4 50.0
condition 2 6 33.3
subroutine 25 29 86.2
pod 0 20 0.0
total 106 149 71.1


line stmt bran cond sub pod time code
1             package Bot::Backbone::DispatchSugar;
2             $Bot::Backbone::DispatchSugar::VERSION = '0.160630';
3 4     4   28 use v5.10;
  4         7  
4 4     4   12 use Moose();
  4         4  
  4         44  
5 4     4   10 use Moose::Exporter;
  4         4  
  4         26  
6 4     4   107 use Carp();
  4         5  
  4         50  
7              
8 4     4   1386 use Bot::Backbone::Dispatcher::Predicate;
  4         31  
  4         3661  
9              
10             # ABSTRACT: Shared sugar methods for dispatch
11              
12              
13             Moose::Exporter->setup_import_methods(
14                 with_meta => [ qw(
15             command not_command
16             to_me not_to_me
17             spoken shouted whispered
18             given_parameters
19             also
20             respond respond_by_method
21             run_this run_this_method
22             redispatch_to
23             ) ],
24                 as_is => [ qw( parameter as ) ],
25             );
26              
27             sub redispatch_to($) {
28 3     3 0 26     my ($meta, $name) = @_;
29 3         85     my $dispatcher = $meta->building_dispatcher;
30              
31 3         88     $dispatcher->add_predicate_or_return(
32                     Bot::Backbone::Dispatcher::Predicate::RedispatchTo->new(
33                         name => $name,
34                     )
35                 );
36             }
37              
38             sub also($) {
39 1     1 0 10     my ($meta, $predicate) = @_;
40 1         29     my $dispatcher = $meta->building_dispatcher;
41 1         33     $dispatcher->add_also_predicate($predicate);
42             }
43              
44             sub command($$) {
45 23     23 0 194     my ($meta, $match, $predicate) = @_;
46 23         675     my $dispatcher = $meta->building_dispatcher;
47              
48 23         633     $dispatcher->add_predicate_or_return(
49                     Bot::Backbone::Dispatcher::Predicate::Command->new(
50                         match => $match,
51                         next_predicate => $predicate,
52                     )
53                 );
54             }
55              
56             sub not_command($) {
57 1     1 0 11     my ($meta, $predicate) = @_;
58 1         29     my $dispatcher = $meta->building_dispatcher;
59              
60 1         34     $dispatcher->add_predicate_or_return(
61                     Bot::Backbone::Dispatcher::Predicate::NotCommand->new(
62                         next_predicate => $predicate,
63                     )
64                 );
65             }
66              
67             sub to_me($) {
68 1     1 0 10     my ($meta, $predicate) = @_;
69 1         29     my $dispatcher = $meta->building_dispatcher;
70              
71 1         34     $dispatcher->add_predicate_or_return(
72                     Bot::Backbone::Dispatcher::Predicate::ToMe->new(
73                         next_predicate => $predicate,
74                     )
75                 );
76             }
77              
78             sub not_to_me($) {
79 1     1 0 11     my ($meta, $predicate) = @_;
80 1         55     my $dispatcher = $meta->building_dispatcher;
81              
82 1         47     $dispatcher->add_predicate_or_return(
83                     Bot::Backbone::Dispatcher::Predicate::ToMe->new(
84                         negate => 1,
85                         next_predicate => $predicate,
86                     )
87                 );
88             }
89              
90             sub spoken($) {
91 1     1 0 11     my ($meta, $predicate) = @_;
92 1         28     my $dispatcher = $meta->building_dispatcher;
93              
94 1         34     $dispatcher->add_predicate_or_return(
95                     Bot::Backbone::Dispatcher::Predicate::Volume->new(
96                         volume => 'spoken',
97                         next_predicate => $predicate,
98                     )
99                 );
100             }
101              
102             sub shouted($) {
103 1     1 0 10     my ($meta, $predicate) = @_;
104 1         29     my $dispatcher = $meta->building_dispatcher;
105              
106 1         28     $dispatcher->add_predicate_or_return(
107                     Bot::Backbone::Dispatcher::Predicate::Volume->new(
108                         volume => 'shout',
109                         next_predicate => $predicate,
110                     )
111                 );
112             }
113              
114             sub whispered($) {
115 1     1 0 10     my ($meta, $predicate) = @_;
116 1         30     my $dispatcher = $meta->building_dispatcher;
117              
118 1         29     $dispatcher->add_predicate_or_return(
119                     Bot::Backbone::Dispatcher::Predicate::Volume->new(
120                         volume => 'whisper',
121                         next_predicate => $predicate,
122                     )
123                 );
124             }
125              
126             our $WITH_ARGS;
127             sub given_parameters(&$) {
128 1     1 0 9     my ($meta, $arg_code, $predicate) = @_;
129 1         30     my $dispatcher = $meta->building_dispatcher;
130              
131 1         2     my @args;
132                 {
133 1         1         local $WITH_ARGS = \@args;
  1         2  
134 1         2         $arg_code->();
135                 }
136              
137                 $dispatcher->add_predicate_or_return(
138 1         57         Bot::Backbone::Dispatcher::Predicate::GivenParameters->new(
139                         parameters => \@args,
140                         next_predicate => $predicate,
141                     )
142                 );
143             }
144              
145             sub parameter($@) {
146 1     1 0 9     my ($name, %config) = @_;
147 1         3     push @$WITH_ARGS, [ $name, \%config ];
148             }
149              
150             sub as(&) {
151 4     4 0 35497     my $code = shift;
152 4         27     return $code;
153             }
154              
155             sub _respond {
156 5     5   5     my ($meta, $code, $dispatcher_type) = @_;
157 5         151     my $dispatcher = $meta->building_dispatcher;
158              
159 5   33     20     $dispatcher_type //= $meta;
160 5         149     $dispatcher->add_predicate_or_return(
161                     Bot::Backbone::Dispatcher::Predicate::Respond->new(
162                         dispatcher_type => $dispatcher_type,
163                         the_code => $code,
164                     )
165                 );
166             }
167              
168             sub respond(&) {
169 1     1 0 10     my ($meta, $code, $dispatcher_type) = @_;
170 1         3     _respond($meta, $code, $dispatcher_type);
171             }
172              
173             sub _run_this {
174 19     19   21     my ($meta, $code, $dispatcher_type) = @_;
175 19         557     my $dispatcher = $meta->building_dispatcher;
176              
177 19   33     54     $dispatcher_type //= $meta;
178 19         519     $dispatcher->add_predicate_or_return(
179                     Bot::Backbone::Dispatcher::Predicate::Run->new(
180                         dispatcher_type => $dispatcher_type,
181                         the_code => $code,
182                     )
183                 );
184             }
185              
186             sub run_this(&) {
187 15     15 0 150     my ($meta, $code, $dispatcher_type) = @_;
188 15         19     _run_this($meta, $code, $dispatcher_type);
189             }
190              
191             sub _by_method {
192 8     8   7     my ($meta, $name) = @_;
193              
194 8 50       24     Carp::croak("no such method as $name found on ", $meta->name)
195                     unless defined $meta->find_method_by_name($name);
196              
197                 return sub {
198 8     8   9         my ($self, $message) = @_;
199              
200 8         25         my $method = $self->can($name);
201 8 50       19         if (defined $method) {
202 8         18             return $self->$method($message);
203                     }
204                     else {
205 0         0             Carp::croak("no such method as $name found on ", $self->meta->name);
206                     }
207 8         638     };
208             }
209              
210             sub respond_by_method($) {
211 4     4 0 36     my ($meta, $name) = @_;
212              
213 4         8     my $code = _by_method($meta, $name);
214 4         10     _respond($meta, \&$code);
215             }
216              
217             sub run_this_method($) {
218 4     4 0 40     my ($meta, $name) = @_;
219              
220 4         10     my $code = _by_method($meta, $name);
221 4         9     _run_this($meta, \&$code);
222             }
223              
224             sub respond_by_service_method($) {
225 0     0 0       my ($meta, $name) = @_;
226              
227 0               my $code = _by_method($meta, $name);
228 0               _respond($meta, \&$code, 'service');
229             }
230              
231             sub respond_by_bot_method($) {
232 0     0 0       my ($meta, $name) = @_;
233              
234 0               my $code = _by_method($meta, $name);
235 0               _respond($meta, \&$code, 'bot');
236             }
237              
238             sub run_this_service_method($) {
239 0     0 0       my ($meta, $name) = @_;
240              
241 0               my $code = _by_method($meta, $name);
242 0               _run_this($meta, \&$code, 'service');
243             }
244              
245             sub run_this_bot_method($) {
246 0     0 0       my ($meta, $name) = @_;
247              
248 0               my $code = _by_method($meta, $name);
249 0               _run_this($meta, \&$code, 'bot');
250             }
251              
252             # These are documented in Bot::Backbone and Bot::Backbone::Service
253              
254              
255              
256             1;
257              
258             __END__
259            
260             =pod
261            
262             =encoding UTF-8
263            
264             =head1 NAME
265            
266             Bot::Backbone::DispatchSugar - Shared sugar methods for dispatch
267            
268             =head1 VERSION
269            
270             version 0.160630
271            
272             =head1 DESCRIPTION
273            
274             Do not use this package directly.
275            
276             See L<Bot::Backbone> and L<Bot::Backbone::Service>.
277            
278             =for Pod::Coverage also
279             as
280             command
281             given_parameters
282             not_command
283             not_to_me
284             parameter
285             redispatch_to
286             respond
287             respond_by_method
288             respond_by_service_method
289             respond_by_bot_method
290             run_this
291             run_this_method
292             run_this_service_method
293             run_this_bot_method
294             shouted
295             spoken
296             to_me
297             whispered
298            
299             =head1 AUTHOR
300            
301             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
302            
303             =head1 COPYRIGHT AND LICENSE
304            
305             This software is copyright (c) 2016 by Qubling Software LLC.
306            
307             This is free software; you can redistribute it and/or modify it under
308             the same terms as the Perl 5 programming language system itself.
309            
310             =cut
311