Circle/Rule/Chain.pm

filter: Statementstmt 29.8% 20 / 67 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 8 totaltotal 24.2% 25 / 103 CC 12 SCAR 27.4
CC
12
Cov
24%
parse_rule
42.0
run
10.8
delete_rule
6.0
CRAP 15.5
« Circle/Loggable.pm ↑ Summary Circle/Net/IRC/Target.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2010 -- leonerd@leonerd.org.uk
4  
5 package Circle::Rule::Chain;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.173320';
11  
12 4 ▶use Circle::Rule::Resultset;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new
15 6 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 6 my $class = shift;
17 6 my ( $store ) = @_;
18  
19 6 my $self = bless {
20 store => $store,
21 rules => [],
22 }, $class;
23  
24 6 return $self;
25 }
26  
27 sub parse_rule
28 0 ▶{
Subroutine parse_rule: not called CC=6 SCAR=37.4
Pod undocumented
29 0 my $self = shift;
30 0 my ( $spec ) = @_;
31  
32 0 my $store = $self->{store};
33  
34 0 my @conds;
35  
36 0 ▶ while( length $spec and $spec !~ m/^:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec and not $spec =~ /^:/) 0 0 0
Condition0%0/3
length $spec and not $spec =~ /^:/
!ll&&!rl&&r
000
MC/DC0%0/2
length $spec and not $spec =~ /^:/
length $spec not $spec =~ /^:/
Truth table0%0/3
A and not(B)
A= length $specB= not $spec =~ /^:/
ABresult
0X0
100
111
37 0 push @conds, $store->parse_cond( $spec );
38  
39 0 $spec =~ s/^\s+//; # trim ws
40 }
41  
42 0 ▶ $spec =~ s/^:\s*// or die "Expected ':' to separate condition and action\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^:\s*// or die "Expected ':' to separate condition and action\n" 0 0 0
43  
44 0 my @actions;
45  
46 0 ▶ while( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
47 0 push @actions, $store->parse_action( $spec );
48  
49 0 $spec =~ s/^\s+//; # trim ws
50 }
51  
52 0 ▶ @actions or die "Expected at least one action\n";
Branch: 1 branch
BranchTrueFalseTotal
@actions or die "Expected at least one action\n" 0 0 0
53  
54 0 return [ \@conds, \@actions ];
55 }
56  
57 sub append_rule
58 0 ▶{
Subroutine append_rule: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my $self = shift;
60 0 my ( $spec ) = @_;
61  
62 0 push @{ $self->{rules} }, $self->parse_rule( $spec );
63 }
64  
65 sub insert_rule
66 0 ▶{
Subroutine insert_rule: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my $self = shift;
68 0 my ( $index, $spec ) = @_;
69  
70 # TODO: Consider what happens if index is OOB
71  
72 0 splice @{ $self->{rules} }, $index, 0, $self->parse_rule( $spec );
73 }
74  
75 sub delete_rule
76 0 ▶{
Subroutine delete_rule: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my $self = shift;
78 0 my ( $index ) = @_;
79  
80 0 ▶ $index < @{ $self->{rules} } or die "No rule at index $index\n";
Branch: 1 branch
BranchTrueFalseTotal
$index < @{$self->{"rules"};} or die "No rule at index $index\n" 0 0 0
81  
82 0 splice @{ $self->{rules} }, $index, 1, ();
83 }
84  
85 sub clear
86 0 ▶{
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my $self = shift;
88  
89 0 @{ $self->{rules} } = ();
90 }
91  
92 sub deparse_rules
93 0 ▶{
Subroutine deparse_rules: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $self = shift;
95  
96 0 my $store = $self->{store};
97  
98 0 my @ret;
99  
100 0 foreach my $rule ( @{ $self->{rules} } ) {
101 0 my ( $conds, $actions ) = @$rule;
102 0 push @ret, join( " ", map { $store->deparse_cond( $_ ) } @$conds ) .
103 ": " .
104 0 join( " ", map { $store->deparse_action( $_ ) } @$actions );
105 }
106  
107 0 return @ret;
108 }
109  
110 sub run
111 4 ▶{
Subroutine run: called CC=5 SCAR=23.8
Pod undocumented
112 4 my $self = shift;
113 4 my ( $event ) = @_;
114  
115 4 my $store = $self->{store};
116  
117 4 RULE: foreach my $rule ( @{ $self->{rules} } ) {
118 0 my ( $conds, $actions ) = @$rule;
119  
120 0 my $results = Circle::Rule::Resultset->new();
121  
122 0 foreach my $cond ( @$conds ) {
123 0 ▶ $store->eval_cond( $cond, $event, $results )
Branch: 1 branch
BranchTrueFalseTotal
$store->eval_cond($cond, $event, $results) or next RULE 0 0 0
124 or next RULE;
125 }
126  
127 # We've got this far - run the actions
128  
129 0 foreach my $action ( @$actions ) {
130 # TODO: Consider eval{} wrapping
131 0 $store->eval_action( $action, $event, $results );
132 }
133  
134 # All rules are independent - for now at least
135 }
136 }
137  
138 4 0x55AA;
« Circle/Loggable.pm ↑ Summary Circle/Net/IRC/Target.pm »