Backbone/SendPolicy/Aggregate.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 6 / 6 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 91.8% 45 / 49 CC 9 SCAR 17.7
CC
9
Cov
95%
allow_send
8.0
_build_policies
2.0
BEGIN
1.0
CRAP 5.9
« Backbone/Service/Role/SendPolicy.pm ↑ Summary Backbone/Dispatcher/Predicate.pm »
1 package Bot::Backbone::SendPolicy::Aggregate;
2 4 $Bot::Backbone::SendPolicy::Aggregate::VERSION = '0.160630';
3 4 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 with 'Bot::Backbone::SendPolicy';
7  
8 # ABSTRACT: Pull several send policies together
9  
10  
11 4 has config => (
12 is => 'ro',
13 isa => 'ArrayRef',
14 required => 1,
15 traits => [ 'Array' ],
16 handles => {
17 'config_pairs' => 'elements',
18 },
19 );
20  
21  
22 4 has policies => (
23 is => 'ro',
24 isa => 'ArrayRef',
25 required => 1,
26 lazy_build => 1,
27 traits => [ 'Array' ],
28 handles => {
29 'all_policies' => 'elements',
30 },
31 );
32  
33 sub _build_policies {
34 3 ▶ my $self = shift;
Subroutine _build_policies: called CC=2 SCAR=6.9
35  
36 3 my @policies;
37 3 for my $config_pair ($self->config_pairs) {
38 3 my ($class_name, $policy_config) = @$config_pair;
39 3 push @policies, $class_name->new(%$policy_config, bot => $self->bot);
40 }
41  
42 3 return \@policies;
43 }
44  
45  
46 sub allow_send {
47 897 ▶ my ($self, $options) = @_;
Subroutine allow_send: called CC=8 SCAR=20.8
Pod documented
48  
49 897 my $final_result = { allow => 1, after => 0 };
50 897 POLICY: for my $policy ($self->all_policies) {
51 897 my $result = $policy->allow_send($options);
52  
53 897 ▶ $final_result->{allow} &&= $result->{allow};
Condition66%2/3
$final_result->{"allow"} &&= $result->{"allow"}
!ll&&!rl&&r
0583314
MC/DC50%1/2
$final_result->{"allow"} &&= $result->{"allow"}
$final_result->{"allow"} $result->{"allow"}
Truth table66%2/3
A &&= B
A= $final_result->{"allow"}B= $result->{"allow"}
ABresult
0X0
100
111
54 $final_result->{after} = $result->{after}
55 897 ▶ if ($result->{after} // 0) > $final_result->{after};
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"after"} // 0) > $final_result->{"after"} 298 599 897
Condition100%2/2
$result->{"after"} // 0
l!l
298599
MC/DC100%1/1
$result->{"after"} // 0
$result->{"after"}
Truth table100%2/2
A
A= $result->{"after"}
Aresult
11
00
56  
57 897 ▶ last POLICY unless $final_result->{allow};
Branch: 1 branch
BranchTrueFalseTotal
unless $final_result->{"allow"} 583 314 897
58 }
59  
60 # Don't need after if there's no delay or if allow is false
61 delete $final_result->{after} if $final_result->{after} <= 0
62 897 ▶ or not $final_result->{allow};
Branch: 1 branch
BranchTrueFalseTotal
if $final_result->{"after"} <= 0 or not $final_result->{"allow"} 599 298 897
Condition66%2/3
$final_result->{"after"} <= 0 or not $final_result->{"allow"}
l!l&&r!l&&!r
5990298
MC/DC50%1/2
$final_result->{"after"} <= 0 or not $final_result->{"allow"}
$final_result->{"after"} <= 0 not $final_result->{"allow"}
Truth table66%2/3
A or not(B)
A= $final_result->{"after"} <= 0B= not $final_result->{"allow"}
ABresult
1X1
011
000
63  
64 897 return $final_result;
65 }
66  
67 4 __PACKAGE__->meta->make_immutable;
68  
69 __END__
70  
71 =pod
72  
73 =encoding UTF-8
74  
75 =head1 NAME
76  
77 Bot::Backbone::SendPolicy::Aggregate - Pull several send policies together
78  
79 =head1 VERSION
80  
81 version 0.160630
82  
83 =head1 DESCRIPTION
84  
85 You probably don't need to worry about this policy directly. Simply by defining a policy using the C<send_policy> helper loaded by L<Bot::Backbone>, you will end up using this to mix them together.
86  
87 Basically, this just provides tools for configuring multiple policies and makes sure that the most restrictive policies win.
88  
89 =head1 ATTRIBUTES
90  
91 =head2 config
92  
93 This is the send policy configuration to use.
94  
95 =head2 policies
96  
97 This is the list of L<Bot::Backbone::SendPolicy> objects to apply. These are built automatically based upon L</config>.
98  
99 =head1 METHODS
100  
101 =head2 allow_send
102  
103 Applies all the L</policies> to the message and returns the most restrictive send policy results.
104  
105 This does perform short circuiting
106  
107 =head1 AUTHOR
108  
109 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
110  
111 =head1 COPYRIGHT AND LICENSE
112  
113 This software is copyright (c) 2016 by Qubling Software LLC.
114  
115 This is free software; you can redistribute it and/or modify it under
116 the same terms as the Perl 5 programming language system itself.
117  
118 =cut
« Backbone/Service/Role/SendPolicy.pm ↑ Summary Backbone/Dispatcher/Predicate.pm »