blib/lib/Bot/Cobalt/Core/Role/Timers.pm

filter: Statementstmt 20.5% 22 / 107 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 43.7% 7 / 16 Podpod 100% 7 / 7 totaltotal 17.4% 36 / 206 CC 32 SCAR 44.5
CC
32
Cov
13%
timer_set_hashref
240.0
timer_set
30.0
timer_get
20.0
CRAP 85.6
« blib/lib/Bot/Cobalt/Plugin/RDB/AsyncSearch/Worker.pm ↑ Summary blib/lib/Bot/Cobalt/IRC.pm »
1 package Bot::Cobalt::Core::Role::Timers;
2 1 $Bot::Cobalt::Core::Role::Timers::VERSION = '0.021003';
3 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Bot::Cobalt::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use List::Objects::WithUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 requires qw/
15 log
16 debug
17 send_event
18 /;
19  
20  
21 ## FIXME tests are nonexistant
22  
23 has TimerPool => (
24 is => 'rw',
25 1 ▶ builder => sub { hash },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 );
27  
28  
29 0 ▶sub timer_gen_unique_id {
Subroutine timer_gen_unique_id: not called CC=2 SCAR=17.9
Pod documented
30 0 my ($self) = @_;
31 0 my @p = ( 'a'..'f', 1..9 );
32 0 my $id = join '', map { $p[rand@p] } 1 .. 4;
33 0 ▶ $id .= $p[rand@p] while $self->TimerPool->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
$self->TimerPool->exists($id) and do { $id .= $p[rand @p] } 0 0 0
34 0 $id
35 }
36  
37 0 ▶sub timer_set {
Subroutine timer_set: not called CC=5 SCAR=34.0
Pod documented
38 0 my ($self, $item) = @_;
39
40 0 my ($caller_pkg, $caller_line) = (caller)[0,2];
41 0 my $d_line = "$caller_pkg line $caller_line";
42
43 0 my $timer = $item;
44 0 ▶ unless (blessed $item && $item->isa('Bot::Cobalt::Timer') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($item) and $item->isa("Bot::Cobalt::Timer")) 0 0 0
Condition0%0/3
builtin::blessed($item) and $item->isa("Bot::Cobalt::Timer")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($item) and $item->isa("Bot::Cobalt::Timer")
builtin::blessed($item) $item->isa("Bot::Cobalt::Timer")
Truth table0%0/3
A and B
A= builtin::blessed($item)B= $item->isa("Bot::Cobalt::Timer")
ABresult
0X0
100
111
45 ## hashref-style (we hope)
46 0 $timer = $self->timer_set_hashref( $item, @_[2 .. $#_] );
47 }
48  
49 0 ▶ unless ($timer->has_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timer->has_id) 0 0 0
50 0 $timer->id( $self->timer_gen_unique_id );
51 }
52
53 ## Add to our TimerPool.
54 0 $self->TimerPool->set($timer->id => $timer);
55 0 $self->send_event( new_timer => $timer->id );
56  
57 0 ▶ $self->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
58 "timer_set; ".join ' ', $timer->id, $timer->delay, $timer->event
59 ) if $self->debug > 1;
60  
61 0 $timer->id
62 }
63  
64 0 ▶sub timer_set_hashref {
Subroutine timer_set_hashref: not called CC=15 SCAR=54.8
Pod documented
65 0 my ($self, $delay, $ev, $id) = @_;
66  
67 0 my ($caller_pkg, $caller_line) = (caller)[0,2];
68 0 my $d_line = "$caller_pkg line $caller_line";
69
70 0 ▶ unless (ref $ev eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ev eq "HASH") 0 0 0
71 0 ▶ if (ref $ev) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ev) { } 0 0 0
72 0 $self->log->warn(
73 "timer_set_hashref expected HASH but got $ev; $d_line"
74 );
75 return
76 0 } else {
77 ## Assume we were passed a simple string.
78 0 $ev = { Event => $ev };
79 }
80 }
81
82 0 my $timer = Bot::Cobalt::Timer->new;
83  
84 0 ▶ $timer->id($id) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
85  
86 ## Try to guess type, or default to 'event'
87 0 my $type = $ev->{Type};
88 0 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
89 0 ▶ if (defined $ev->{Text} && defined $ev->{Context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ev->{'Text'} and defined $ev->{'Context'}) { } 0 0 0
Condition0%0/3
defined $ev->{"Text"} and defined $ev->{"Context"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ev->{"Text"} and defined $ev->{"Context"}
defined $ev->{"Text"} defined $ev->{"Context"}
Truth table0%0/3
A and B
A= defined $ev->{"Text"}B= defined $ev->{"Context"}
ABresult
0X0
100
111
90 0 $type = 'msg'
91 } else {
92 0 $type = 'event'
93 }
94 }
95
96 0 my($event_name, @event_args);
97
98 TYPE: {
99 0 ▶ if ($type eq "event") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "event") 0 0 0
100 0 ▶ unless (defined $ev->{Event}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ev->{"Event"}) 0 0 0
101 0 $self->log->warn(
102 "timer_set_hashref no Event specified; $d_line"
103 );
104 return
105 0 }
106  
107 0 $timer->event( $ev->{Event} );
108 0 ▶ $timer->args( $ev->{Args}//[] );
Condition0%0/2
$ev->{"Args"} // []
l!l
00
MC/DC0%0/1
$ev->{"Args"} // []
$ev->{"Args"}
Truth table0%0/2
A
A= $ev->{"Args"}
Aresult
11
00
109
110 last TYPE
111 0 }
112
113 0 ▶ if (grep { $type eq $_ } qw/msg message privmsg action/) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$type eq $_;} "msg", "message", "privmsg", "action") 0 0 0
114 0 ▶ unless (defined $ev->{Text} && defined $ev->{Target}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ev->{"Text"} and defined $ev->{"Target"}) 0 0 0
Condition0%0/3
defined $ev->{"Text"} and defined $ev->{"Target"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ev->{"Text"} and defined $ev->{"Target"}
defined $ev->{"Text"} defined $ev->{"Target"}
Truth table0%0/3
A and B
A= defined $ev->{"Text"}B= defined $ev->{"Target"}
ABresult
0X0
100
111
115 0 $self->log->warn(
116 "timer_set_hashref; $type needs Text and Target; $d_line"
117 );
118  
119 return
120 0 }
121  
122 0 ▶ $timer->context( $ev->{Context} ) if defined $ev->{Context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ev->{"Context"} 0 0 0
123 0 $timer->target( $ev->{Target} );
124 0 $timer->text( $ev->{Text} );
125 0 $timer->type( $type );
126  
127 last TYPE
128 0 }
129
130 0 $self->log->error("Unknown type $type passed to timer_set_hashref");
131 return
132 0 }
133  
134 ## Tag w/ __PACKAGE__ if no alias is specified
135 0 ▶ $timer->alias( $ev->{Alias} // scalar caller );
Condition0%0/2
$ev->{"Alias"} // scalar caller
l!l
00
MC/DC0%0/1
$ev->{"Alias"} // scalar caller
$ev->{"Alias"}
Truth table0%0/2
A
A= $ev->{"Alias"}
Aresult
11
00
136
137 ## Start ticking.
138 0 $timer->delay( $delay );
139  
140 0 $timer
141 }
142  
143 0 ▶sub timer_get {
Subroutine timer_get: not called CC=4 SCAR=30.0
Pod documented
144 0 my ($self, $id) = @_;
145 0 ▶ return unless $id and $self->TimerPool->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $id and $self->TimerPool->exists($id) 0 0 0
Condition0%0/3
$id and $self->TimerPool->exists($id)
!ll&&!rl&&r
000
MC/DC0%0/2
$id and $self->TimerPool->exists($id)
$id $self->TimerPool->exists($id)
Truth table0%0/3
A and B
A= $idB= $self->TimerPool->exists($id)
ABresult
0X0
100
111
146  
147 0 ▶ $self->log->debug("timer retrieved; $id")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
148 if $self->debug > 1;
149  
150 0 $self->TimerPool->get($id)
151 }
152  
153 0 ▶sub timer_get_alias {
Subroutine timer_get_alias: not called CC=3 SCAR=24.8
Pod documented
154 ## get all timerIDs for this alias
155 0 my ($self, $alias) = @_;
156 0 ▶ return unless $alias;
Branch: 1 branch
BranchTrueFalseTotal
unless $alias 0 0 0
157  
158 0 my @timers;
159 0 ▶ $self->TimerPool->kv->visit(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
160 0 my ($id, $entry) = @$_;
161 0 ▶ push @timers, $id if $entry->alias eq $alias
Branch: 1 branch
BranchTrueFalseTotal
if $entry->alias eq $alias 0 0 0
162 0 });
163  
164 0 ▶ wantarray ? @timers : \@timers
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
165 }
166  
167 0 ▶sub timer_del {
Subroutine timer_del: not called CC=4 SCAR=30.0
Pod documented
168 ## delete a timer by its ID
169 ## doesn't care if the timerID actually exists or not.
170 0 my ($self, $id) = @_;
171 0 ▶ return unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
172  
173 0 ▶ $self->log->debug("timer del; $id")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
174 if $self->debug > 1;
175  
176 0 ▶ return unless $self->TimerPool->exists($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TimerPool->exists($id) 0 0 0
177  
178 0 my $deleted = $self->TimerPool->delete($id);
179 0 $self->send_event( 'deleted_timer', $id, $deleted->all );
180
181 0 $deleted->all
182 }
183  
184 0 ▶sub timer_del_alias {
Subroutine timer_del_alias: not called CC=3 SCAR=24.8
Pod documented
185 0 my ($self, $alias) = @_;
186 0 ▶ return unless $alias;
Branch: 1 branch
BranchTrueFalseTotal
unless $alias 0 0 0
187  
188 0 my @deleted;
189 0 ▶ $self->TimerPool->kv->visit(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
190 0 my ($id, $entry) = @$_;
191 0 ▶ if ($entry->alias eq $alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->alias eq $alias) 0 0 0
192 0 push @deleted, $id;
193 0 my $removed = $self->TimerPool->delete($id);
194 0 $self->send_event( deleted_timer => $id, $removed );
195 }
196 0 });
197  
198 0 ▶ wantarray ? @deleted : scalar @deleted
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
199 }
200  
201  
202 1 1;
203 __END__
« blib/lib/Bot/Cobalt/Plugin/RDB/AsyncSearch/Worker.pm ↑ Summary blib/lib/Bot/Cobalt/IRC.pm »