Circle/Ruleable.pm

filter: Statementstmt 38.0% 46 / 121 Branchbran 3.5% 1 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.8% 14 / 23 Podpod 0.0% 0 / 10 totaltotal 30.0% 61 / 203 CC 27 SCAR 40.4
CC
27
Cov
29%
command_rules_describe
182.0
command_rules_list
30.0
__ANON__
30.0
CRAP 57.0
« Circle/Net/IRC/Channel.pm ↑ Summary Circle/GlobalRules.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::Ruleable;
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 4 ▶use base qw( Circle::Commandable );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.173320';
12  
13 sub init_rulestore
14 2 ▶{
Subroutine init_rulestore: called CC=1 SCAR=0.0
Pod undocumented
15 2 my $self = shift;
16 2 my %args = @_;
17  
18 2 $self->{rulestore} = Circle::Rule::Store->new( %args );
19 }
20  
21 sub run_rulechain
22 4 ▶{
Subroutine run_rulechain: called CC=2 SCAR=8.1
Pod undocumented
23 4 my $self = shift;
24 4 my ( $chainname, $event ) = @_;
25  
26 4 ▶ return if eval { $self->{rulestore}->run( $chainname, $event ); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->{"rulestore"}->run($chainname, $event); 1 } } 4 0 4
27  
28 0 my $err = $@; chomp $err;
29 0 $self->responderr( "Exception during processing of rulechain '$chainname': $err" );
30 }
31  
32 sub command_rules
33 0 ▶ : Command_description("Display or manipulate action rules")
Subroutine command_rules: not called CC=1 SCAR=0.0
Pod undocumented
34 {
35 # The body doesn't matter as it never gets run
36 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 sub command_rules_list
39 0 ▶ : Command_description("List the action rules")
Subroutine command_rules_list: not called CC=5 SCAR=34.0
Pod undocumented
40 : Command_subof('rules')
41 : Command_default()
42 : Command_arg('chain?')
43 {
44 0 my $self = shift;
45 0 my ( $chain, $cinv ) = @_;
46  
47 0 my $rulestore = $self->{rulestore};
48  
49 0 my @chains = $rulestore->chains;
50  
51 0 ▶ if( defined $chain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chain) 0 0 0
52 0 ▶ grep { $chain eq $_ } @chains or
Branch: 1 branch
BranchTrueFalseTotal
grep {$chain eq $_;} @chains or return $cinv->responderr("No such rule chain '${chain}'") 0 0 0
53 return $cinv->responderr( "No such rule chain '$chain'" );
54 }
55  
56 0 foreach my $chain ( sort @chains ) {
57 0 $cinv->respond( "Chain '$chain':" );
58 0 my @rules = $rulestore->get_chain( $chain )->deparse_rules();
59 0 $cinv->respond( "$_: $rules[$_]" ) for 0 .. $#rules;
60 }
61  
62 0 return;
63 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 sub command_rules_add
66 0 ▶ : Command_description("Add a new rule")
Subroutine command_rules_add: not called CC=1 SCAR=6.9
Pod undocumented
67 : Command_subof('rules')
68 : Command_arg('chain')
69 : Command_arg('spec', eatall => 1)
70 {
71 0 my $self = shift;
72 0 my ( $chain, $spec, $cinv ) = @_;
73  
74 0 my $rulestore = $self->{rulestore};
75  
76 0 $rulestore->get_chain( $chain )->append_rule( $spec );
77  
78 0 $cinv->respond( "Added to chain $chain" );
79 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 sub command_rules_insert
82 0 ▶ : Command_description("Insert a rule before another rule")
Subroutine command_rules_insert: not called CC=2 SCAR=17.9
Pod undocumented
83 : Command_subof('rules')
84 : Command_arg('chain')
85 : Command_arg('index')
86 : Command_arg('spec', eatall => 1)
87 {
88 0 my $self = shift;
89 0 my ( $chain, $index, $spec, $cinv ) = @_;
90  
91 0 ▶ $index =~ m/^\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
$index =~ /^\d+$/ or return $cinv->responderr("Bad index: $index") 0 0 0
92 return $cinv->responderr( "Bad index: $index" );
93  
94 0 my $rulestore = $self->{rulestore};
95  
96 0 $rulestore->get_chain( $chain )->insert_rule( $index, $spec );
97  
98 0 $cinv->respond( "Inserted in $chain before rule $index" );
99 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 sub command_rules_replace
102 0 ▶ : Command_description("Replace an existing rule with a new one")
Subroutine command_rules_replace: not called CC=1 SCAR=6.9
Pod undocumented
103 : Command_subof('rules')
104 : Command_arg('chain')
105 : Command_arg('index')
106 : Command_arg('spec', eatall => 1)
107 {
108 0 my $self = shift;
109 0 my ( $chain, $index, $spec, $cinv ) = @_;
110  
111 # We'll do this by inserting our new rule before the one we want to
112 # replace. If it works, delete the old one, which will now be one further
113 # down.
114  
115 0 my $rulestore = $self->{rulestore};
116 0 my $rulechain = $rulestore->get_chain( $chain );
117  
118 0 $rulechain->insert_rule( $index, $spec );
119 0 $rulechain->delete_rule( $index + 1 );
120  
121 0 $cinv->respond( "Replaced $chain rule $index" );
122 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 sub command_rules_delete
125 0 ▶ : Command_description("Delete a rule")
Subroutine command_rules_delete: not called CC=2 SCAR=17.9
Pod undocumented
126 : Command_subof('rules')
127 : Command_arg('chain')
128 : Command_arg('index')
129 {
130 0 my $self = shift;
131 0 my ( $chain, $index, $cinv ) = @_;
132  
133 0 ▶ $index =~ m/^\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
$index =~ /^\d+$/ or return $cinv->responderr("Bad index: $index") 0 0 0
134 return $cinv->responderr( "Bad index: $index" );
135  
136 0 my $rulestore = $self->{rulestore};
137  
138 0 $rulestore->get_chain( $chain )->delete_rule( $index );
139  
140 0 $cinv->respond( "Deleted $chain rule $index" );
141 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 sub command_rules_describe
144 0 ▶ : Command_description("Describe rule conditions or actions")
Subroutine command_rules_describe: not called CC=13 SCAR=52.0
Pod undocumented
145 : Command_subof('rules')
146 : Command_arg('name?')
147 : Command_opt('conds=+', desc => "List conditions")
148 : Command_opt('actions=+', desc => "List actions")
149 {
150 0 my $self = shift;
151 0 my ( $name, $opts, $cinv ) = @_;
152  
153 0 my $rulestore = $self->{rulestore};
154  
155 0 my @names;
156
157 0 ▶ if( defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
158 0 @names = ( $name );
159 }
160 else {
161 # List both if neither or both options specified
162 0 ▶ push @names, sort $rulestore->list_conds if !$opts->{actions} or $opts->{conds};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"actions"} or $opts->{"conds"} 0 0 0
Condition0%0/3
not $opts->{"actions"} or $opts->{"conds"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $opts->{"actions"} or $opts->{"conds"}
not $opts->{"actions"} $opts->{"conds"}
Truth table0%0/3
not(A) or B
A= not $opts->{"actions"}B= $opts->{"conds"}
ABresult
1X1
011
000
163 0 ▶ push @names, sort $rulestore->list_actions if !$opts->{conds} or $opts->{actions};
Branch: 1 branch
BranchTrueFalseTotal
if not $opts->{"conds"} or $opts->{"actions"} 0 0 0
Condition0%0/3
not $opts->{"conds"} or $opts->{"actions"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $opts->{"conds"} or $opts->{"actions"}
not $opts->{"conds"} $opts->{"actions"}
Truth table0%0/3
not(A) or B
A= not $opts->{"conds"}B= $opts->{"actions"}
ABresult
1X1
011
000
164 }
165  
166 0 for my $name ( @names ) {
167 0 ▶ if( my $attrs = eval { $rulestore->describe_cond( $name ) } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $attrs = eval { do { $rulestore->describe_cond($name) } }) { } 0 0 0
elsif ($attrs = eval { do { $rulestore->describe_action($name) } }) { } 0 0 0
168 0 ▶ my $description = $attrs->{desc} || "[has no description]";
Condition0%0/2
$attrs->{'desc'} || '[has no description]'
l!l
00
MC/DC0%0/1
$attrs->{'desc'} || '[has no description]'
$attrs->{'desc'}
Truth table0%0/2
A
A= $attrs->{'desc'}
Aresult
11
00
169 0 $cinv->respond( "Condition '$name': $description" );
170 0 ▶ $cinv->respond( " $name($attrs->{format})" ) if defined $attrs->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"format"} 0 0 0
171 }
172 0 elsif( $attrs = eval { $rulestore->describe_action( $name ) } ) {
173 0 ▶ my $description = $attrs->{desc} || "[has no description]";
Condition0%0/2
$attrs->{'desc'} || '[has no description]'
l!l
00
MC/DC0%0/1
$attrs->{'desc'} || '[has no description]'
$attrs->{'desc'}
Truth table0%0/2
A
A= $attrs->{'desc'}
Aresult
11
00
174 0 $cinv->respond( "Action '$name': $description" );
175 0 ▶ $cinv->respond( " $name($attrs->{format})" ) if defined $attrs->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"format"} 0 0 0
176 }
177 else {
178 0 $cinv->responderr( "No such condition or action '$name'" );
179 }
180 }
181  
182 0 return;
183 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 4 ▶use Class::Method::Modifiers qw( install_modifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
186 sub APPLY_Ruleable
187 8 ▶{
Subroutine APPLY_Ruleable: called CC=1 SCAR=5.3
Pod undocumented
188 8 my $caller = caller;
189  
190 0 ▶ install_modifier $caller, after => load_configuration => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
191 0 my $self = shift;
192 0 my ( $ynode ) = @_;
193  
194 0 ▶ return unless my $rules_ynode = $ynode->{rules};
Branch: 1 branch
BranchTrueFalseTotal
unless my $rules_ynode = $ynode->{"rules"} 0 0 0
195 0 my $rulestore = $self->{rulestore};
196  
197 0 foreach my $chain ( keys %$rules_ynode ) {
198 0 my $chain_ynode = $rules_ynode->{$chain};
199 0 my $chain = $rulestore->new_chain( $chain ); # or fetch the existing one
200 0 $chain->clear;
201 0 $chain->append_rule( $_ ) for @$chain_ynode;
202 }
203 8 };
204  
205 0 ▶ install_modifier $caller, after => store_configuration => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
206 0 my $self = shift;
207 0 my ( $ynode ) = @_;
208  
209 0 my $rulestore = $self->{rulestore};
210 0 ▶ my $rules_ynode = $ynode->{rules} ||= YAML::Node->new({});
Condition0%0/3
$ynode->{"rules"} ||= "YAML::Node"->new({})
l!l&&r!l&&!r
000
MC/DC0%0/2
$ynode->{"rules"} ||= "YAML::Node"->new({})
$ynode->{"rules"} "YAML::Node"->new({})
Truth table0%0/3
A ||= B
A= $ynode->{"rules"}B= "YAML::Node"->new({})
ABresult
1X1
011
000
211  
212 0 foreach my $chain ( $rulestore->chains ) {
213 0 my $chain_ynode = $rules_ynode->{$chain} = [
214 $rulestore->get_chain( $chain )->deparse_rules(),
215 ];
216 }
217  
218 # Delete any of the old ones
219 0 ▶ $rulestore->get_chain( $_ ) or delete $rules_ynode->{$_} for keys %$rules_ynode;
Branch: 1 branch
BranchTrueFalseTotal
$rulestore->get_chain($_) or delete $rules_ynode->{$_} 0 0 0
220 8 };
221 }
222  
223 4 0x55AA;
« Circle/Net/IRC/Channel.pm ↑ Summary Circle/GlobalRules.pm »