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.161950';
3 4     4   28 use v5.10;
  4         9  
4 4     4   12 use Moose();
  4         3  
  4         45  
5 4     4   13 use Moose::Exporter;
  4         12  
  4         47  
6 4     4   139 use Carp();
  4         4  
  4         77  
7              
8 4     4   1586 use Bot::Backbone::Dispatcher::Predicate;
  4         11  
  4         3608  
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 27 my ($meta, $name) = @_;
29 3         86 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 11 my ($meta, $predicate) = @_;
40 1         43 my $dispatcher = $meta->building_dispatcher;
41 1         38 $dispatcher->add_also_predicate($predicate);
42             }
43              
44             sub command($$) {
45 23     23 0 215 my ($meta, $match, $predicate) = @_;
46 23         777 my $dispatcher = $meta->building_dispatcher;
47              
48 23         705 $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 9 my ($meta, $predicate) = @_;
58 1         30 my $dispatcher = $meta->building_dispatcher;
59              
60 1         36 $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 20 my ($meta, $predicate) = @_;
69 1         46 my $dispatcher = $meta->building_dispatcher;
70              
71 1         84 $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 13 my ($meta, $predicate) = @_;
80 1         54 my $dispatcher = $meta->building_dispatcher;
81              
82 1         43 $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 14 my ($meta, $predicate) = @_;
92 1         52 my $dispatcher = $meta->building_dispatcher;
93              
94 1         51 $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 16 my ($meta, $predicate) = @_;
104 1         43 my $dispatcher = $meta->building_dispatcher;
105              
106 1         41 $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 12 my ($meta, $predicate) = @_;
116 1         30 my $dispatcher = $meta->building_dispatcher;
117              
118 1         30 $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 10 my ($meta, $arg_code, $predicate) = @_;
129 1         29 my $dispatcher = $meta->building_dispatcher;
130              
131 1         2 my @args;
132             {
133 1         1 local $WITH_ARGS = \@args;
  1         4  
134 1         37 $arg_code->();
135             }
136              
137             $dispatcher->add_predicate_or_return(
138 1         47 Bot::Backbone::Dispatcher::Predicate::GivenParameters->new(
139             parameters => \@args,
140             next_predicate => $predicate,
141             )
142             );
143             }
144              
145             sub parameter($@) {
146 1     1 0 10 my ($name, %config) = @_;
147 1         5 push @$WITH_ARGS, [ $name, \%config ];
148             }
149              
150             sub as(&) {
151 4     4 0 37345 my $code = shift;
152 4         19 return $code;
153             }
154              
155             sub _respond {
156 5     5   18 my ($meta, $code, $dispatcher_type) = @_;
157 5         161 my $dispatcher = $meta->building_dispatcher;
158              
159 5   33     21 $dispatcher_type //= $meta;
160 5         155 $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         4 _respond($meta, $code, $dispatcher_type);
171             }
172              
173             sub _run_this {
174 19     19   22 my ($meta, $code, $dispatcher_type) = @_;
175 19         677 my $dispatcher = $meta->building_dispatcher;
176              
177 19   33     67 $dispatcher_type //= $meta;
178 19         657 $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 174 my ($meta, $code, $dispatcher_type) = @_;
188 15         28 _run_this($meta, $code, $dispatcher_type);
189             }
190              
191             sub _by_method {
192 8     8   21 my ($meta, $name) = @_;
193              
194 8 50       32 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   13 my ($self, $message) = @_;
199              
200 8         42 my $method = $self->can($name);
201 8 50       20 if (defined $method) {
202 8         28 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         802 };
208             }
209              
210             sub respond_by_method($) {
211 4     4 0 39 my ($meta, $name) = @_;
212              
213 4         8 my $code = _by_method($meta, $name);
214 4         12 _respond($meta, \&$code);
215             }
216              
217             sub run_this_method($) {
218 4     4 0 44 my ($meta, $name) = @_;
219              
220 4         9 my $code = _by_method($meta, $name);
221 4         12 _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.161950
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