lib/MooseX/Role/Listenable.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 90.3% 28 / 31 CC 2 SCAR 3.2
CC
2
Cov
92%
remove_door_opened_listener
2.0
door_opened
2.0
__ANON__
1.0
CRAP 1.4
↑ Summary
1 package MooseX::Role::Listenable;
2  
3 # ABSTRACT: A parameterized role for observable objects
4  
5 =head1 NAME
6  
7 MooseX::Role::Listenable - A parameterized role for observable objects
8  
9 =head1 SYNOPSIS
10  
11 # a class with an observable feature- notifies observers
12 # when door opened
13 package Car;
14 use Moose;
15 with 'MooseX::Role::Listenable' => {event => 'door_opened'};
16 sub open_door {
17 ... # actually open the door
18 $self->door_opened; # notify observers
19 }
20  
21 # an observer class that can listen to door_opened events
22 package Dashboard;
23 use Moose;
24 sub door_opened { print "Got door_opened event!\n" }
25  
26 # attach observer to observable
27 $car->add_door_opened_listener($dashboard);
28  
29 # detach
30 $car->remove_door_opened_listener($dashboard);
31  
32 =head1 DESCRIPTION
33  
34 A simple implemenation of the observable pattern. By adding this to a class:
35  
36 with 'MooseX::Role::Listenable' => {event => 'some_event_name'};
37  
38 You are making the class observable for the event 'some_event_name'. You
39 can call the method C<some_event_name()> on the object, and all listeners
40 added with C<add_some_event_name_listener()> will be notified. Listeners
41 will be notified by calling their method C<some_event_name()>.
42  
43 Note the list of listeners is a C<Set::Object::Weak>, so be sure to keep
44 a reference to them somewhere else.
45  
46 =head1 SEE ALSO
47  
48 C<Class::Listener>, C<Class::Observable>, and C<Aspect::Library::Listenable>.
49  
50 =head1 AUTHOR
51  
52 Ran Eilam <eilara@cpan.org>
53  
54 =head1 COPYRIGHT
55  
56 Ran Eilam <eilara@cpan.org>
57  
58 This library is free software; you can redistribute it and/or modify
59 it under the same terms as Perl itself.
60  
61 =cut
62  
63 1 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Set::Object::Weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 1 parameter event => (isa => 'Str', required => 1);
67  
68 2 ▶role {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 2 my $p = shift;
70 2 my $event = $p->event;
71 2 my $list = "_${event}_listeners";
72  
73 2 has $list => (
74 is => 'ro',
75 lazy_build => 1,
76 isa => 'Set::Object::Weak',
77 );
78  
79 2 ▶ method "_build_$list" => sub { Set::Object::Weak->new };
Subroutine _build__door_closed_listeners: called CC=1 SCAR=0.0
80  
81 2 ▶ method "add_${event}_listener" => sub {
Subroutine add_door_closed_listener: called CC=1 SCAR=0.0
82 2 my ($self, $listener) = @_;
83 2 $self->$list->insert($listener);
84 2 };
85  
86 0 ▶ method "remove_${event}_listener" => sub {
Subroutine remove_door_opened_listener: not called CC=1 SCAR=6.9
87 0 my ($self, $listener) = @_;
88 0 $self->$list->remove($listener);
89 2 };
90  
91 2 ▶ method $event => sub {
Subroutine door_opened: called CC=2 SCAR=6.9
92 2 my ($self, @args) = @_;
93 2 $_->$event(@args) for $self->$list->members;
94 2 };
95 1 };
96  
97 1 1;
↑ Summary