Created
March 20, 2017 14:53
-
-
Save xsawyerx/ecdae8341364450eeed018f90ab7eb6e to your computer and use it in GitHub Desktop.
Dancer2::Plugin::With
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Dancer2::Plugin::With; | |
use strict; | |
use warnings; | |
use Dancer2::Plugin; | |
plugin_keywords( qw< add_with route_with > ); | |
has 'with_reg' => ( | |
'is' => 'ro', | |
'default' => sub { +{} }, | |
); | |
sub add_with { | |
my ( $self, $name, $cb ) = @_; | |
if ( $name && $cb ) { | |
$self->with_reg->{$name} = $cb; | |
} | |
} | |
sub route_with { | |
my ( $self, $name, $route_cb ); | |
# This happens when loading, so early on | |
# (Instead of during run-time) | |
my $cb = $self->with_reg->{$name} | |
or die "$name is not registered\n"; | |
return sub { | |
$cb->() | |
and return $route_cb->(); | |
# Maybe weaken? | |
$self->dsl->send_error("Behavior failed.", 400); | |
}; | |
} | |
1; | |
__END__ | |
add_with 'extra_check' => sub { | |
... | |
}; | |
get '/' => route_with 'extra_check' => sub { | |
... | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment