Skip to content

Instantly share code, notes, and snippets.

@xsawyerx
Created March 20, 2017 14:53
Show Gist options
  • Save xsawyerx/ecdae8341364450eeed018f90ab7eb6e to your computer and use it in GitHub Desktop.
Save xsawyerx/ecdae8341364450eeed018f90ab7eb6e to your computer and use it in GitHub Desktop.
Dancer2::Plugin::With
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