File Coverage

blib/lib/Devel/Events/Handler/Multiplex.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Devel::Events::Handler::Multiplex;
4 2     2   22379 use Moose;
  0            
  0            
5              
6             with qw/Devel::Events::Handler/;
7              
8             use Set::Object;
9              
10             has '_handlers' => (
11             is => 'ro',
12             isa => 'Set::Object',
13             default => sub { Set::Object->new },
14             );
15              
16             sub BUILD {
17             my ( $self, $param ) = @_;
18              
19             if ( my $handlers = $param->{handlers} ) {
20             $self->add_handler( @$handlers );
21             }
22             }
23              
24             sub add_handler {
25             my ( $self, @h ) = @_;
26             $self->_handlers->insert(@h);
27             }
28              
29             sub remove_handler {
30             my ( $self, @h ) = @_;
31             $self->_handlers->remove(@h);
32             }
33              
34             sub handlers {
35             my ( $self, @h ) = @_;
36              
37             if ( @h ) {
38             $self->_handlers( Set::Object->new(@h) );
39             }
40              
41             $self->_handlers->members;
42             }
43              
44             sub new_event {
45             my ( $self, @event ) = @_;
46              
47             foreach my $handler ( $self->handlers ) {
48             $handler->new_event(@event);
49             }
50             }
51              
52             __PACKAGE__;
53              
54             __END__
55              
56             =pod
57              
58             =head1 NAME
59              
60             Devel::Events::Handler::Multiplex - Delegate events to multiple handlers
61              
62             =head1 SYNOPSIS
63              
64             use Devel::Events::Handler::Multiplex;
65              
66             my $h = Devel::Events::Handler::Multiplex->new(
67             handlers => \@handlers,
68             );
69              
70             $h->add_handler( $other_handler );
71              
72             $h->new_event( ... );
73              
74             $h->remove_handler( $some_handler );
75              
76             =head1 DESCRIPTION
77              
78             This handler repeats events to any number of sub handlers.
79              
80             It is useful as a central hub, delegating to any number of sub listeners, from
81             any number of generators.
82              
83             =head1 METHODS
84              
85             =over 4
86              
87             =item new_event @handlers
88              
89             Delegates the event to every one of the sub handlers.
90              
91             =item handlers
92              
93             Lists the handlers
94              
95             =item add_handler $handler
96              
97             Add a handler to the set of registered handlers.
98              
99             =item remove_handler $handler
100              
101             Remove a handler from the set of registered handlers.
102              
103             =back
104              
105             =cut
106              
107