oFono/Helpers/EventMgr.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 194 CC 5 SCAR 17.6
CC
5
Cov
0%
trigger_event
20.0
new
6.0
add_event
2.0
CRAP 5.8
1 package Net::Radio::oFono::Helpers::EventMgr;
2  
3 use strict;
4 use warnings;
5  
6 use 5.010;
7  
8 use Carp::Assert;
9 use Hash::MoreUtils qw(slice_grep);
10 use Params::Util qw(_ARRAY0 _ARRAY _CODELIKE _HASH _STRING);
11 use Scalar::Util qw(blessed refaddr);
12  
13 use Log::Any qw($log);
14  
15 =head1 NAME
16  
17 Net::Radio::oFono::Helpers::EventMgr - simple event manager
18  
19 DESCRIPTION
20  
21 Base class for classes which want be able to trigger events and want to
22 delegate the event management.
23  
24 =head1 METHODS
25  
26 =head2 new
27  
28 Constructs a new instance of a C<BDCtask>. C<BDCtask> is an abstract base
29 class, so instantiating this class directly is prohibited.
30  
31 B<Parameters>:
32  
33 This constructor expects a hash containing the named parameters:
34  
35 =over 4
36  
37 =item C<ON_*>
38  
39 Event handler. See L<add_event> for further details.
40  
41 The value for this parameter can be either a code reference, then an
42 event without a I<MEMO> item is generated, or a hash reference with
43 following items:
44  
45 =over 8
46  
47 =item C<FUNC>
48  
49 Code reference to the callback routine.
50  
51 =item C<MEMO>
52  
53 Memo given to the callback routine when event is triggered.
54  
55 =back
56  
57 =back
58  
59 B<Supported events>:
60  
61 None by default - all added by derived classes ...
62  
63 =cut
64  
65 sub new
66 {
67 my ( $class, %params ) = @_;
68  
69 $log->is_debug() and $log->debugf( '%s::new( %s )', $class, [ keys %params ] );
70  
71 my $self = bless( {}, $class );
72  
73 my %event_params = slice_grep( sub { $_ =~ m/^ON_/ }, \%params, keys(%params) );
74 my %events;
75  
76 foreach my $event ( keys(%event_params) )
77 {
78 affirm { _CODELIKE( $event_params{$event} ) or _HASH( $event_params{$event} ) };
79 $events{$event} =
80 _CODELIKE( $event_params{$event} )
81 ? [ { FUNC => $event_params{$event} } ]
82 : [ $event_params{$event} ];
83 affirm { _CODELIKE( $events{$event}->[0]->{FUNC} ) };
84 }
85  
86 $self->{events} = \%events;
87 $self->{triggered} = {};
88  
89 return $self;
90 }
91  
92 =head2 add_event
93  
94 Adds an event to the list of actions to be executed when an event point
95 is reached.
96  
97 B<Parameters>:
98  
99 Expects following parameters in order of appearance:
100  
101 =over 8
102  
103 =item string
104  
105 The name of the event for this callback
106  
107 =item coderef
108  
109 Specifies a code reference which should be called when the event is triggered.
110 The parameters passed to the callback are:
111  
112 =over 12
113  
114 =item *
115  
116 C<$memo> when specified
117  
118 =item *
119  
120 C<$event_mgr>
121  
122 =item *
123  
124 C<$event_name>
125  
126 =item
127  
128 C<$event_info> when given
129  
130 =back
131  
132 Whereby the first parameter is the I<MEMO> parameter specified here and
133 it's omitted when not specified. The fourth parameter is an optional
134 information field given by the triggering routine.
135  
136 =item scalar
137  
138 Specifies a scalar which will be given as first argument to the callback
139 function which is specified with I<FUNC>.
140  
141 =back
142  
143 B<Return values>:
144  
145 This method returns C<$self>.
146  
147 =cut
148  
149 sub add_event
150 {
151 my ( $self, $event, $func, $memo ) = @_;
152  
153 _HASH($func) and ( $func, $memo ) = @$func{ "FUNC", "MEMO" };
154  
155 affirm { _CODELIKE($func) };
156 $self->{events}->{$event} //= [];
157  
158 my $elem = {
159 FUNC => $func,
160 MEMO => $memo
161 };
162 Net::Radio::oFono::Helpers::EventMgr::Container->_add_to( $elem, $self->{events}->{$event} );
163  
164 return $self;
165 }
166  
167 =head2 add_events(%)
168  
169 Runs add_event for each key => value pair given.
170  
171 =cut
172  
173 sub add_events
174 {
175 my ( $self, %event_params ) = @_;
176  
177 foreach my $event ( keys(%event_params) )
178 {
179 $self->add_event( $event, $event_params{$event} );
180 }
181  
182 return;
183 }
184  
185 =head2 remove_event
186  
187 Removed a previously added event handler from the list of actions to be
188 executed when an event point is reached.
189  
190 B<Parameters>:
191  
192 Expects following parameters in order of appearance:
193  
194 =over 8
195  
196 =item string
197  
198 The name of the event for this callback.
199  
200 =item coderef
201  
202 Specifies a code reference which should be called when the event is triggered.
203 The parameters passed to the callback are:
204  
205 =over 12
206  
207 =item *
208  
209 C<$memo> when specified
210  
211 =back
212  
213 =back
214  
215 =cut
216  
217 sub remove_event
218 {
219 my ( $self, $event, $func, $memo ) = @_;
220  
221 _HASH($func) and ( $func, $memo ) = @$func{ "FUNC", "MEMO" };
222  
223 affirm { _CODELIKE($func) };
224 $self->{events}->{$event} //= [];
225  
226 my $elem = {
227 FUNC => $func,
228 MEMO => $memo
229 };
230 Net::Radio::oFono::Helpers::EventMgr::Container->_remove_from( $elem,
231 $self->{events}->{$event} );
232  
233 return $self;
234 }
235  
236 =head2 set_event
237  
238 Sets the list of actions to be executed when an event point is reached
239 to a list containing just the specified event callback.
240  
241 Any previously set or added event notification callback is silently
242 discarded.
243  
244 B<Parameters>:
245  
246 Expects following parameters in order of appearance:
247  
248 =over 8
249  
250 =item string
251  
252 The name of the event for this callback
253  
254 =item coderef
255  
256 Specifies a code reference which should be called when the event is triggered.
257 The parameters passed to the callback are:
258  
259 =over 12
260  
261 =item *
262  
263 C<$memo> when specified
264  
265 =item *
266  
267 C<$event_mgr>
268  
269 =item *
270  
271 C<$event_name>
272  
273 =item
274  
275 C<$event_info> when given
276  
277 =back
278  
279 Whereby the first parameter is the I<MEMO> parameter specified here and
280 it's omitted when not specified. The fourth parameter is an optional
281 information field given by the triggering routine.
282  
283 =item scalar
284  
285 Specifies a scalar which will be given as first argument to the callback
286 function which is specified with I<FUNC>.
287  
288 =back
289  
290 B<Return values>:
291  
292 This method returns C<$self>.
293  
294 =cut
295  
296 sub set_event
297 {
298 my ( $self, $event, $func, $memo ) = @_;
299  
300 _HASH($func) and ( $func, $memo ) = @$func{ "FUNC", "MEMO" };
301  
302 affirm { _CODELIKE($func) };
303 $self->{events}->{$event} = [
304 {
305 FUNC => $func,
306 MEMO => $memo
307 }
308 ];
309  
310 return $self;
311 }
312  
313 =head2 trigger_event
314  
315 The C<event> method handles the triggering of events. Every notification
316 callback noted for an event is invoked when the event is triggered first
317 time.
318  
319 When an event is tried to be triggered twice, an exception is thrown to
320 tell, it's forbidden.
321  
322 B<Parameter>:
323  
324 =over 4
325  
326 =item string
327  
328 The name of the event which should be triggered.
329  
330 =back
331  
332 B<Return values>:
333  
334 This method returns C<$self>.
335  
336 B<Note>:
337  
338 It's strongly prohibited to override this method in derived classes. The
339 expected behaviour may change without notification to the authors or
340 maintainers of derived classes.
341  
342 =cut
343  
344 sub trigger_event
345 {
346 my ( $self, $event, $info ) = @_;
347  
348 my $handled = 0;
349 $log
350 and $log->is_debug()
351 and
352 $log->debugf( 'Event <%s> reached for %s ( %s )', $event, ref($self) || __PACKAGE__, $info );
353  
354 if ( defined( $self->{events}->{$event} ) )
355 {
356 foreach my $eventHandler ( @{ $self->{events}->{$event} } )
357 {
358 my @event_params = (
359 (
360 defined( $eventHandler->{MEMO} ) ? ( $eventHandler->{MEMO} )
361 : ()
362 ),
363 $self, $event,
364 (
365 defined($info) ? ($info)
366 : ()
367 ),
368 );
369 &{ $eventHandler->{FUNC} }(@event_params);
370 ++$handled;
371 }
372  
373 $log
374 and $log->is_debug()
375 and $log->debugf( 'Event <%s> got triggered for %s ( %s ) -- %d handlers',
376 $event, ref($self) || __PACKAGE__,
377 $info, $handled );
378 }
379  
380 return $self;
381 }
382  
383 sub DESTROY { delete $_[0]->{events}; return; }
384  
385 { # hide from CPAN
386  
387 package Net::Radio::oFono::Helpers::EventMgr::Container;
388  
389 use base qw(Net::Radio::oFono::Helpers::Container);
390  
391 sub _where
392 {
393 my ( $self, $elem, $ary ) = @_;
394  
395 my $reffunc = refaddr( $elem->{FUNC} );
396 my $refmemo = refaddr( $elem->{MEMO} );
397  
398 my $match =
399 sub { refaddr( $_->{FUNC} ) == $reffunc and refaddr( $_->{MEMO} ) == $refmemo; };
400 return firstidx( $match, @{$ary} );
401 }
402 }
403  
404 =head1 BUGS
405  
406 Please report any bugs or feature requests to C<bug-net-radio-ofono at rt.cpan.org>, or through
407 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Radio-oFono>. I will be notified, and then you'll
408 automatically be notified of progress on your bug as I make changes.
409  
410 If you think you've found a bug then please read "How to Report Bugs
411 Effectively" by Simon Tatham:
412 L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
413  
414 =head1 SUPPORT
415  
416 You can find documentation for this module with the perldoc command.
417  
418 perldoc Net::Radio::oFono
419  
420 You can also look for information at:
421  
422 =over 4
423  
424 =item * RT: CPAN's request tracker (report bugs here)
425  
426 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Radio-oFono>
427  
428 If you think you've found a bug then please read "How to Report Bugs
429 Effectively" by Simon Tatham:
430 L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
431  
432 =item * AnnoCPAN: Annotated CPAN documentation
433  
434 L<http://annocpan.org/dist/Net-Radio-oFono>
435  
436 =item * CPAN Ratings
437  
438 L<http://cpanratings.perl.org/d/Net-Radio-oFono>
439  
440 =item * Search CPAN
441  
442 L<http://search.cpan.org/dist/Net-Radio-oFono/>
443  
444 =back
445  
446 =head2 Where can I go for help with a concrete version?
447  
448 Bugs and feature requests are accepted against the latest version
449 only. To get patches for earlier versions, you need to get an
450 agreement with a developer of your choice - who may or not report the
451 issue and a suggested fix upstream (depends on the license you have
452 chosen).
453  
454 =head2 Business support and maintenance
455  
456 For business support you can contact Jens via his CPAN email
457 address rehsackATcpan.org. Please keep in mind that business
458 support is neither available for free nor are you eligible to
459 receive any support based on the license distributed with this
460 package.
461  
462 =head1 ACKNOWLEDGEMENTS
463  
464 =head1 AUTHOR
465  
466 Jens Rehsack, C<< <rehsack at cpan.org> >>
467  
468 =head1 LICENSE AND COPYRIGHT
469  
470 Copyright 2012 Jens Rehsack.
471  
472 This program is free software; you can redistribute it and/or modify it
473 under the terms of either: the GNU General Public License as published
474 by the Free Software Foundation; or the Artistic License.
475  
476 See http://dev.perl.org/licenses/ for more information.
477  
478 =cut
479  
480 1;