File Coverage

blib/lib/Bot/Cobalt/Timer.pm
Criterion Covered Total %
statement 32 43 74.4
branch 8 14 57.1
condition 2 3 66.6
subroutine 12 16 75.0
pod 3 4 75.0
total 57 80 71.2


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Timer;
2             $Bot::Cobalt::Timer::VERSION = '0.021003';
3 6     6   13965 use strictures 2;
  6         1164  
  6         180  
4 6     6   851 use Carp;
  6         9  
  6         307  
5              
6 6     6   380 use Bot::Cobalt::Common ':types';
  6         7  
  6         40  
7              
8 6     6   467 use Moo;
  6         7370  
  6         33  
9              
10             ## It's possible to pass in a different core.
11             ## (Allows timers to fire against different syndicators if needed)
12             has core => (
13             lazy => 1,
14             is => 'rw',
15             isa => HasMethods['send_event'],
16             builder => sub {
17 0     0   0 require Bot::Cobalt::Core;
18 0 0       0 Bot::Cobalt::Core->instance
19             || die "Cannot find active Bot::Cobalt::Core instance"
20             },
21             );
22              
23             ## May have a timer ID specified at construction for use by
24             ## timer pool managers; if not, creating IDs is up to them.
25             ## (See ::Core::Role::Timers)
26             has id => (
27             lazy => 1,
28             is => 'rw',
29             isa => Str,
30             predicate => 'has_id'
31             );
32              
33             ## 'at' is set regardless of whether delay()/at() is used
34             ## (or 0 if none is ever set)
35             has at => (
36             lazy => 1,
37             is => 'rw',
38             isa => Num,
39 1     1   392 builder => sub { 0 },
40             );
41              
42             has delay => (
43             lazy => 1,
44             is => 'rw',
45             isa => Num,
46             predicate => 'has_delay',
47             clearer => 'clear_delay',
48 0     0   0 builder => sub { 0 },
49             trigger => sub {
50             my ($self, $value) = @_;
51             $self->at( time() + $value );
52             },
53             );
54              
55             has event => (
56             lazy => 1,
57             is => 'rw',
58             isa => Str,
59             predicate => 'has_event',
60             );
61              
62             has args => (
63             lazy => 1,
64             is => 'rw',
65             isa => ArrayObj,
66             coerce => 1,
67 0     0   0 builder => sub { [] },
68             );
69              
70             has alias => (
71             is => 'rw',
72             isa => Str,
73 1     1   67 builder => sub { scalar caller },
74             );
75              
76             has context => (
77             lazy => 1,
78             is => 'rw',
79             isa => Str,
80             predicate => 'has_context',
81 0     0   0 builder => sub { 'Main' },
82             );
83              
84             has text => (
85             lazy => 1,
86             is => 'rw',
87             isa => Str,
88             predicate => 'has_text'
89             );
90              
91             has target => (
92             lazy => 1,
93             is => 'rw',
94             isa => Str,
95             predicate => 'has_target'
96             );
97              
98             has type => (
99             lazy => 1,
100             is => 'rw',
101             isa => Str,
102             builder => sub {
103 2     2   8617 my ($self) = @_;
104             # best guess:
105 2 100 66     16 $self->has_context && $self->has_target ? 'msg' : 'event'
106             },
107             coerce => sub {
108             $_[0] =~ /message|privmsg/i ? 'msg' : lc($_[0]) ;
109             },
110             );
111              
112              
113             sub _process_type {
114 1     1   1 my ($self) = @_;
115             ## If this is a special type, set up event and args.
116 1         14 my $type = lc $self->type;
117              
118 1 50       6 if (grep {; $_ eq $type } qw/msg message privmsg action/) {
  4         6  
119 0 0       0 my $ev_name = $type eq 'action' ?
120             'action' : 'message' ;
121 0         0 $self->event( $ev_name );
122              
123 0         0 my @ev_args = ( $self->context, $self->target, $self->text );
124 0         0 $self->args( \@ev_args );
125             }
126              
127             1
128 1         1 }
129              
130             sub is_ready {
131 3     3 1 3754 my ($self) = @_;
132 3 100       58 $self->at <= time ? 1 : ()
133             }
134              
135             sub execute {
136 1     1 1 10 my ($self) = @_;
137 1         3 $self->_process_type;
138              
139 1 50       13 unless ( $self->event ) {
140 0         0 carp "timer execute called but no event specified";
141             return
142 0         0 }
143              
144 1         17 my $args = $self->args;
145 1         7 $self->core->send_event( $self->event, @$args );
146 1         928 1
147             }
148              
149 2     2 1 606 sub execute_if_ready { execute_ready(@_) }
150             sub execute_ready {
151 2     2 0 2 my ($self) = @_;
152 2 100       5 $self->is_ready ? $self->execute : ()
153             }
154              
155              
156             1;
157             __END__