File Coverage

blib/lib/Beam/Event.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 25 25 100.0


line stmt bran cond sub pod time code
1             package Beam::Event;
2             our $VERSION = '1.007';
3             # ABSTRACT: Base Event class
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod # My::Emitter consumes the Beam::Emitter role
8             #pod my $emitter = My::Emitter->new;
9             #pod $emitter->on( "foo", sub {
10             #pod my ( $event ) = @_;
11             #pod print "Foo happened!\n";
12             #pod # stop this event from continuing
13             #pod $event->stop;
14             #pod } );
15             #pod my $event = $emitter->emit( "foo" );
16             #pod
17             #pod =head1 DESCRIPTION
18             #pod
19             #pod This is the base event class for C objects.
20             #pod
21             #pod The base class is only really useful for notifications. Create a subclass
22             #pod to add data attributes.
23             #pod
24             #pod =head1 SEE ALSO
25             #pod
26             #pod =over 4
27             #pod
28             #pod =item L
29             #pod
30             #pod =back
31             #pod
32             #pod =cut
33              
34 10     10   543 use strict;
  10         14  
  10         267  
35 10     10   32 use warnings;
  10         10  
  10         271  
36              
37 10     10   1801 use Moo;
  10         40745  
  10         71  
38 10     10   6350 use Types::Standard qw(:all);
  10         45742  
  10         95  
39              
40             #pod =attr name
41             #pod
42             #pod The name of the event. This is the string that is given to L.
43             #pod
44             #pod =cut
45              
46             has name => (
47             is => 'ro',
48             isa => Str,
49             required => 1,
50             );
51              
52             #pod =attr emitter
53             #pod
54             #pod The emitter of this event. This is the object that created the event.
55             #pod
56             #pod =cut
57              
58             has emitter => (
59             is => 'ro',
60             isa => ConsumerOf['Beam::Emitter'],
61             required => 1,
62             );
63              
64             #pod =attr is_default_stopped
65             #pod
66             #pod This is true if anyone called L on this event.
67             #pod
68             #pod Your L should check this attribute before trying to do
69             #pod what the event was notifying about.
70             #pod
71             #pod =cut
72              
73             has is_default_stopped => (
74             is => 'rw',
75             isa => Bool,
76             default => sub { 0 },
77             );
78              
79             #pod =attr is_stopped
80             #pod
81             #pod This is true if anyone called L on this event.
82             #pod
83             #pod When using L, this is checked automatically
84             #pod after every callback, and event processing is stopped if this is true.
85             #pod
86             #pod =cut
87              
88             has is_stopped => (
89             is => 'rw',
90             isa => Bool,
91             default => sub { 0 },
92             );
93              
94             #pod =method stop_default ()
95             #pod
96             #pod Calling this will cause the default behavior of this event to be stopped.
97             #pod
98             #pod B Your event-emitting object must check L for this
99             #pod behavior to work.
100             #pod
101             #pod =cut
102              
103             sub stop_default {
104 3     3 1 510 my ( $self ) = @_;
105 3         57 $self->is_default_stopped( 1 );
106             }
107              
108             #pod =method stop ()
109             #pod
110             #pod Calling this will immediately stop any further processing of this event.
111             #pod Also calls L.
112             #pod
113             #pod =cut
114              
115             sub stop {
116 2     2 1 554 my ( $self ) = @_;
117 2         9 $self->stop_default;
118 2         54 $self->is_stopped( 1 );
119             }
120              
121             1;
122              
123             __END__