| 1 | package Slick::EventHandler; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.036; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Types::Standard qw(HashRef); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Slick::Events qw(EVENTS); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use List::Util qw(reduce); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | has event_handlers => ( | ||||||||||
| 12 | is => 'ro', | ||||||||||
| 13 | lazy => 1, | ||||||||||
| 14 | isa => HashRef, | ||||||||||
| 15 | 11 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 11 | my $r; | |||||||||
| 17 | 11 | @$r{ @{ EVENTS() } } = map { [] } EVENTS->@*; | |||||||||
| 18 | 11 | return $r; | |||||||||
| 19 | } | ||||||||||
| 20 | 1 | ); | |||||||||
| 21 | |||||||||||
| 22 | # Register an event (middleware) | ||||||||||
| 23 | 2 | ▶ | sub on { | ||||||||
|
Subroutine
on: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||
| 24 | 2 | my ( $self, $event, $code ) = @_; | |||||||||
| 25 | |||||||||||
| 26 | 2 | ▶ | croak qq{Invalid type specified for event, expected CODE got } . ref($code) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | unless ( ref($code) eq 'CODE' ); | ||||||||||
| 28 | |||||||||||
| 29 | croak qq{Invalid event '$event', I only know of ( } | ||||||||||
| 30 | 0 | ▶ | . ( reduce { $a . ', ' . $b } EVENTS->@* ) . ' )' | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 31 | 2 | ▶ | unless exists $self->event_handlers->{$event}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | |||||||||||
| 33 | 2 | push @{ $self->event_handlers->{$event} }, $code; | |||||||||
| 34 | 2 | return $code; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 1 | 1; | |||||||||
| 38 | |||||||||||
| 39 | =encoding utf8 | ||||||||||
| 40 | |||||||||||
| 41 | =head1 NAME | ||||||||||
| 42 | Slick::EventHandler | ||||||||||
| 43 | |||||||||||
| 44 | =head1 SYNOPSIS | ||||||||||
| 45 | |||||||||||
| 46 | A L<Moo::Role> that allows the registering of events via a C<on> method. | ||||||||||
| 47 | |||||||||||
| 48 | =head1 API | ||||||||||
| 49 | |||||||||||
| 50 | =head2 on | ||||||||||
| 51 | |||||||||||
| 52 | Registers a C<CodeRef> with a given event name. Only allows registering of events | ||||||||||
| 53 | that exist within L<Slick::Events>. | ||||||||||
| 54 | |||||||||||
| 55 | =over 2 | ||||||||||
| 56 | |||||||||||
| 57 | =item * L<Slick::Context> | ||||||||||
| 58 | |||||||||||
| 59 | =item * L<Slick::Database> | ||||||||||
| 60 | |||||||||||
| 61 | =item * L<Slick::DatabaseExecutor> | ||||||||||
| 62 | |||||||||||
| 63 | =item * L<Slick::DatabaseExecutor::MySQL> | ||||||||||
| 64 | |||||||||||
| 65 | =item * L<Slick::DatabaseExecutor::Pg> | ||||||||||
| 66 | |||||||||||
| 67 | =item * L<Slick::EventHandler> | ||||||||||
| 68 | |||||||||||
| 69 | =item * L<Slick::Events> | ||||||||||
| 70 | |||||||||||
| 71 | =item * L<Slick::Methods> | ||||||||||
| 72 | |||||||||||
| 73 | =item * L<Slick::RouteMap> | ||||||||||
| 74 | |||||||||||
| 75 | =item * L<Slick::Util> | ||||||||||
| 76 | |||||||||||
| 77 | =back | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||