| 1 | package Eve::EventMap; | ||||||||||
| 2 | |||||||||||
| 3 | 9 | ▶ | use parent qw(Eve::Class); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Eve::EventMap - maps events to event handlers. | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | my $event_map = Eve::EventMap->new(); | ||||||||||
| 15 | |||||||||||
| 16 | $event_map->bind( | ||||||||||
| 17 | event_class => 'Eve::Event::Foo', | ||||||||||
| 18 | handler => $some_handler); | ||||||||||
| 19 | |||||||||||
| 20 | $event_map->bind( | ||||||||||
| 21 | event_class => 'Eve::Event::Bar', | ||||||||||
| 22 | handler => $another_handler); | ||||||||||
| 23 | |||||||||||
| 24 | my $event = Eve::Event::Foo->new(event_map => $event_map); | ||||||||||
| 25 | |||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||
| 27 | |||||||||||
| 28 | B<Eve::EventMap> is the facility on the one hand used to bind | ||||||||||
| 29 | events to event handlers, on the other hand to extract handlers that | ||||||||||
| 30 | need to be run when a certain event is triggered. | ||||||||||
| 31 | |||||||||||
| 32 | =head1 METHODS | ||||||||||
| 33 | |||||||||||
| 34 | =head2 B<init()> | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | sub init { | ||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 39 | 0 | my $self = shift; | |||||||||
| 40 | |||||||||||
| 41 | 0 | $self->{'_map'} = {}; | |||||||||
| 42 | |||||||||||
| 43 | 0 | return; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | =head2 B<bind()> | ||||||||||
| 47 | |||||||||||
| 48 | Binds an event class to an event handler. | ||||||||||
| 49 | |||||||||||
| 50 | =head3 Arguments | ||||||||||
| 51 | |||||||||||
| 52 | =over 4 | ||||||||||
| 53 | |||||||||||
| 54 | =item C<event_class> | ||||||||||
| 55 | |||||||||||
| 56 | an event class (B<Eve::Event> derivative) | ||||||||||
| 57 | |||||||||||
| 58 | =item C<handler> | ||||||||||
| 59 | |||||||||||
| 60 | a handler instance (B<Eve::EventHandler::Class> derivative). | ||||||||||
| 61 | |||||||||||
| 62 | =back | ||||||||||
| 63 | |||||||||||
| 64 | =head3 Throws | ||||||||||
| 65 | |||||||||||
| 66 | =over 4 | ||||||||||
| 67 | |||||||||||
| 68 | =item C<Eve::Exception::Duplicate> | ||||||||||
| 69 | |||||||||||
| 70 | trying to bind a handler duplicating one that has already been bound | ||||||||||
| 71 | to the event. | ||||||||||
| 72 | |||||||||||
| 73 | =back | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub bind { | ||||||||
|
Subroutine
bind: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 78 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 79 | 0 | Eve::Support::arguments(\%arg_hash, my ($event_class, $handler)); | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | if (not exists $self->_map->{$event_class}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | $self->_map->{$event_class} = []; | |||||||||
| 83 | } else { | ||||||||||
| 84 | 0 | ▶ | if (grep($_ == $handler, @{$self->_map->{$event_class}})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | Eve::Exception::Duplicate->throw( | |||||||||
| 86 | message => 'Duplicate handler in the event map: '.$handler); | ||||||||||
| 87 | } | ||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | push(@{$self->_map->{$event_class}}, $handler); | |||||||||
| 91 | |||||||||||
| 92 | 0 | return; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =head2 B<get_handler_list()> | ||||||||||
| 96 | |||||||||||
| 97 | Returns handler list for an event. | ||||||||||
| 98 | |||||||||||
| 99 | =head3 Arguments | ||||||||||
| 100 | |||||||||||
| 101 | =over 4 | ||||||||||
| 102 | |||||||||||
| 103 | =item C<event> | ||||||||||
| 104 | |||||||||||
| 105 | an event instance we are requesting handlers for. | ||||||||||
| 106 | |||||||||||
| 107 | =back | ||||||||||
| 108 | |||||||||||
| 109 | =head3 Returns | ||||||||||
| 110 | |||||||||||
| 111 | Unified list of handlers bound to the event and to all its | ||||||||||
| 112 | ancestors. Note that if one handler is bound to an event and its | ||||||||||
| 113 | ancestors it will be returned only once. | ||||||||||
| 114 | |||||||||||
| 115 | =cut | ||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | sub get_handler_list { | ||||||||
|
Subroutine
get_handler_list: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 118 | 0 | my ($self, %arg_hash) = @_; | |||||||||
| 119 | 0 | Eve::Support::arguments(\%arg_hash, my $event); | |||||||||
| 120 | |||||||||||
| 121 | 0 | my $handler_list = []; | |||||||||
| 122 | 0 | for my $event_class (keys %{$self->_map}) { | |||||||||
| 123 | 0 | ▶ | if ($event->isa($event_class)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | push(@{$handler_list}, @{$self->_map->{$event_class}}); | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 0 | return Eve::Support::unique(list => $handler_list); | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | =head1 SEE ALSO | ||||||||||
| 132 | |||||||||||
| 133 | =over 4 | ||||||||||
| 134 | |||||||||||
| 135 | =item L<Eve::Class> | ||||||||||
| 136 | |||||||||||
| 137 | =item L<Eve::Event> | ||||||||||
| 138 | |||||||||||
| 139 | =item L<Eve::EventHandler> | ||||||||||
| 140 | |||||||||||
| 141 | =item L<Eve::Exception> | ||||||||||
| 142 | |||||||||||
| 143 | =back | ||||||||||
| 144 | |||||||||||
| 145 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 146 | |||||||||||
| 147 | Copyright 2012 Igor Zinovyev. | ||||||||||
| 148 | |||||||||||
| 149 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 150 | under the terms of either: the GNU General Public License as published | ||||||||||
| 151 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 152 | |||||||||||
| 153 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 154 | |||||||||||
| 155 | |||||||||||
| 156 | =head1 AUTHOR | ||||||||||
| 157 | |||||||||||
| 158 | =over 4 | ||||||||||
| 159 | |||||||||||
| 160 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||||||||||
| 161 | |||||||||||
| 162 | =back | ||||||||||
| 163 | |||||||||||
| 164 | =cut | ||||||||||
| 165 | |||||||||||
| 166 | 9 | 1; | |||||||||