Backbone/Dispatcher/Predicate.pm

filter: Statementstmt 92.1% 164 / 178 Branchbran 62.5% 15 / 24 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 97.4% 38 / 39 Podpod 0.0% 0 / 8 totaltotal 85.0% 221 / 260 CC 17 SCAR 16.1
CC
17
Cov
87%
__ANON__
23.1
__ANON__
3.1
do_it
3.1
CRAP 5.0
« Backbone/SendPolicy/Aggregate.pm ↑ Summary Backbone/SendPolicy/MinimumInterval.pm »
1 package Bot::Backbone::Dispatcher::Predicate;
2 4 $Bot::Backbone::Dispatcher::Predicate::VERSION = '0.142220';
3 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Defines the predicate packages responsible for aiding dispatch
7  
8  
9 4 requires qw( do_it more_predicates );
10  
11 {
12 package Bot::Backbone::Dispatcher::Predicate::RedispatchTo;
13 4 $Bot::Backbone::Dispatcher::Predicate::RedispatchTo::VERSION = '0.142220';
14 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 with 'Bot::Backbone::Dispatcher::Predicate';
18  
19 4 has name => ( is => 'ro', isa => 'Str', required => 1 );
20  
21 sub do_it {
22 13 ▶ my ($self, $service, $message) = @_;
Subroutine do_it: called CC=1 SCAR=0.0
Pod undocumented
23  
24 13 my $redispatch_service = $service->get_service($self->name);
25 13 return $redispatch_service->dispatch_message($message);
26 }
27  
28 sub more_predicates {
29 0 ▶ my ($self, $service) = @_;
Subroutine more_predicates: not called CC=1 SCAR=6.9
Pod undocumented
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 4 __PACKAGE__->meta->make_immutable;
40 }
41  
42 {
43 4 package Bot::Backbone::Dispatcher::Predicate::Nesting;
44 4 $Bot::Backbone::Dispatcher::Predicate::Nesting::VERSION = '0.142220';
45 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 4 has next_predicate => (
49 is => 'ro',
50 isa => 'Bot::Backbone::Dispatcher::Predicate',
51 required => 1,
52 handles => [ 'do_it' ],
53 );
54  
55 4 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 ▶ my ($self, $service) = @_;
Subroutine more_predicates: called CC=1 SCAR=0.0
Pod undocumented
65 2 return ($self->next_predicate);
66 }
67  
68 4 __PACKAGE__->meta->make_immutable;
69 }
70  
71 {
72 4 package Bot::Backbone::Dispatcher::Predicate::Command;
73 4 $Bot::Backbone::Dispatcher::Predicate::Command::VERSION = '0.142220';
74 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 4 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
78  
79 4 has match => (
80 is => 'rw',
81 isa => 'Str|RegexpRef',
82 required => 1,
83 );
84  
85 override do_it => sub {
86 186 ▶ my ($self, $service, $message) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
87  
88 return $message->set_bookmark_do(sub {
89 186 ▶ if ($message->match_next($self->match)) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($message->match_next($self->match)) 23 163 186
90 23 $message->add_flag('command');
91 23 return super();
92 }
93  
94 163 return '';
95 186 });
96 4 };
97  
98 4 __PACKAGE__->meta->make_immutable;
99 }
100  
101 {
102 4 package Bot::Backbone::Dispatcher::Predicate::NotCommand;
103 4 $Bot::Backbone::Dispatcher::Predicate::NotCommand::VERSION = '0.142220';
104 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 4 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
108  
109 override do_it => sub {
110 5 ▶ my ($self, $service, $message) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
111  
112 5 ▶ unless ($message->has_flag('command')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message->has_flag("command")) 1 4 5
113 1 return super();
114 }
115  
116 4 return '';
117 4 };
118  
119 4 __PACKAGE__->meta->make_immutable;
120 }
121  
122 {
123 4 package Bot::Backbone::Dispatcher::Predicate::ToMe;
124 4 $Bot::Backbone::Dispatcher::Predicate::ToMe::VERSION = '0.142220';
125 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 4 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
129  
130 4 has negate => (
131 is => 'ro',
132 isa => 'Bool',
133 required => 1,
134 default => '',
135 );
136  
137 override do_it => sub {
138 2 ▶ my ($self, $service, $message) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
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 2 ▶ if ($self->negate xor $message->is_to_me) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->negate xor $message->is_to_me) 1 1 2
Condition50%2/4
$self->negate xor $message->is_to_me
l&&rl&&!r!l&&r!l&&!r
1010
MC/DC50%1/2
$self->negate xor $message->is_to_me
$self->negate $message->is_to_me
Truth table50%2/4
A xor B
A= $self->negateB= $message->is_to_me
ABresult
110
101
011
000
149 1 return super();
150 }
151  
152 1 return '';
153 4 };
154  
155 4 __PACKAGE__->meta->make_immutable;
156 }
157  
158 {
159 4 package Bot::Backbone::Dispatcher::Predicate::Volume;
160 4 $Bot::Backbone::Dispatcher::Predicate::Volume::VERSION = '0.142220';
161 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164 4 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
165  
166 4 ▶ use Bot::Backbone::Types qw( VolumeLevel );
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 4 has volume => (
169 is => 'ro',
170 isa => VolumeLevel,
171 required => 1,
172 default => 'spoken',
173 );
174  
175 override do_it => sub {
176 3 ▶ my ($self, $service, $message) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
177  
178 3 ▶ if ($self->volume eq $message->volume) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->volume eq $message->volume) 1 2 3
179 1 return super();
180 }
181  
182 2 return '';
183 4 };
184  
185 4 __PACKAGE__->meta->make_immutable;
186 }
187  
188 {
189 4 package Bot::Backbone::Dispatcher::Predicate::GivenParameters;
190 4 $Bot::Backbone::Dispatcher::Predicate::GivenParameters::VERSION = '0.142220';
191 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 4 extends 'Bot::Backbone::Dispatcher::Predicate::Nesting';
195  
196 4 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 2 ▶ my ($self, $service, $message) = @_;
Subroutine __ANON__: called CC=1 SCAR=1.4
208  
209 return $message->set_bookmark_do(sub {
210 2 ▶ for my $arg ($self->all_parameters) {
Subroutine __ANON__: called CC=9 SCAR=31.4
211 2 my ($name, $config) = @$arg;
212  
213 # Match against ->args
214 2 ▶ if (defined $config->{match}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $config->{'match'}) { } 2 0 2
elsif (defined $config->{'match_original'}) { } 0 0 0
215 2 my $match = $config->{match};
216  
217 2 ▶ if (exists $config->{default}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{'default'} and not $message->has_more_args) { } 0 2 2
Condition33%1/3
exists $config->{"default"} and not $message->has_more_args
!ll&&!rl&&r
200
MC/DC0%0/2
exists $config->{"default"} and not $message->has_more_args
exists $config->{"default"} not $message->has_more_args
Truth table33%1/3
A and not(B)
A= exists $config->{"default"}B= not $message->has_more_args
ABresult
0X0
100
111
218 and not $message->has_more_args) {
219 0 $message->set_parameter($name => $config->{default});
220 }
221 else {
222 2 my $value = $message->match_next($match);
223 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 1 1 2
224 1 $message->set_parameter($name => $value);
225 }
226 else {
227 1 return '';
228 }
229 }
230 }
231  
232 # Match against ->text
233 elsif (defined $config->{match_original}) {
234 0 my $match = $config->{match_original};
235  
236 0 my $value = $message->match_next_original($match);
237 0 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
elsif (exists $config->{'default'}) { } 0 0 0
238 0 $message->set_parameter($name => $value);
239 }
240 elsif (exists $config->{default}) {
241 0 $message->set_parameter($name => $config->{default});
242 }
243 else {
244 0 return '';
245 }
246 }
247  
248 # What the...?
249 else {
250 0 Carp::carp("parameter $name missing 'match' or 'match_original'");
251 0 return '';
252 }
253 }
254  
255 1 return super();
256 2 });
257 4 };
258  
259 4 __PACKAGE__->meta->make_immutable;
260 }
261  
262 {
263 4 package Bot::Backbone::Dispatcher::Predicate::Functor;
264 4 $Bot::Backbone::Dispatcher::Predicate::Functor::VERSION = '0.142220';
265 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 4 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 4 ▶ use Bot::Backbone::Types qw( DispatcherType );
Subroutine BEGIN: called CC=1 SCAR=0.0
269  
270 4 has dispatcher_type => (
271 is => 'ro',
272 isa => DispatcherType,
273 required => 1,
274 coerce => 1,
275 );
276  
277 sub select_invocant {
278 20 ▶ my ($self, $service) = @_;
Subroutine select_invocant: called CC=2 SCAR=6.9
Pod undocumented
279 20 ▶ return $self->dispatcher_type eq 'bot' ? $service->bot : $service;
Branch: 1 branch
BranchTrueFalseTotal
$self->dispatcher_type eq 'bot' ? : 15 5 20
280 }
281  
282 4 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 4 package Bot::Backbone::Dispatcher::Predicate::Respond;
295 4 $Bot::Backbone::Dispatcher::Predicate::Respond::VERSION = '0.142220';
296 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
297 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
298  
299 4 with qw(
300 Bot::Backbone::Dispatcher::Predicate
301 Bot::Backbone::Dispatcher::Predicate::Functor
302 );
303  
304 sub do_it {
305 5 ▶ my ($self, $service, $message) = @_;
Subroutine do_it: called CC=3 SCAR=11.2
Pod undocumented
306  
307 5 my $invocant = $self->select_invocant($service);
308 5 my @responses = $self->call_the_code($invocant, $message);
309 5 ▶ if (@responses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@responses) 5 0 5
310 5 for my $response (@responses) {
311 5 $message->reply($invocant, $response);
312 }
313  
314 5 return 1;
315 }
316  
317 0 return '';
318 }
319  
320 1 ▶ sub more_predicates { () }
Subroutine more_predicates: called CC=1 SCAR=0.0
Pod undocumented
321  
322 4 __PACKAGE__->meta->make_immutable;
323 }
324  
325 {
326 4 package Bot::Backbone::Dispatcher::Predicate::Run;
327 4 $Bot::Backbone::Dispatcher::Predicate::Run::VERSION = '0.142220';
328 4 ▶ use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
329 4 ▶ use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
330  
331 4 with qw(
332 Bot::Backbone::Dispatcher::Predicate
333 Bot::Backbone::Dispatcher::Predicate::Functor
334 );
335  
336 sub do_it {
337 15 ▶ my ($self, $service, $message) = @_;
Subroutine do_it: called CC=1 SCAR=0.0
Pod undocumented
338 15 my $invocant = $self->select_invocant($service);
339 15 return $self->call_the_code($invocant, $message);
340 }
341  
342 1 ▶ sub more_predicates { () }
Subroutine more_predicates: called CC=1 SCAR=0.0
Pod undocumented
343  
344 4 __PACKAGE__->meta->make_immutable;
345 }
346  
347 4 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.142220
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) 2014 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
« Backbone/SendPolicy/Aggregate.pm ↑ Summary Backbone/SendPolicy/MinimumInterval.pm »