Skip to content

Commit

Permalink
use hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ambs committed Oct 18, 2011
1 parent 7f10e18 commit e967b48
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 35 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[ API CHANGES ]
* Deprecation of 'before', 'before_template' and 'after' in favor of hook
(Alberto Simões)

[ BUG FIXES ]
* Minor corrections (jamhed, felixdo)
Expand Down
22 changes: 19 additions & 3 deletions lib/Dancer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,26 @@ our @EXPORT = qw(

# Dancer's syntax

sub after { Dancer::Hook->new('after', @_) }
sub after {
Dancer::Deprecation->deprecated(reason => "use hooks!",
version => '1.3080',
fatal => 0);
Dancer::Hook->new('after', @_);
}
sub before {
Dancer::Deprecation->deprecated(reason => "use hooks!",
version => '1.3080',
fatal => 0);
Dancer::Hook->new('before', @_);
}
sub before_template {
Dancer::Deprecation->deprecated(reason => "use hooks!",
version => '1.3080',
fatal => 0);
Dancer::Hook->new('before_template', @_);
}

sub any { Dancer::App->current->registry->any_add(@_) }
sub before { Dancer::Hook->new('before', @_) }
sub before_template { Dancer::Hook->new('before_template', @_) }
sub captures { Dancer::SharedData->request->params->{captures} }
sub cookie { Dancer::Cookies->cookie( @_ ) }
sub cookies { Dancer::Cookies->cookies }
Expand Down
2 changes: 1 addition & 1 deletion lib/Dancer/Plugin/Ajax.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Dancer::Plugin;

register 'ajax' => \&ajax;

before sub {
hook before => sub {
if (request->is_ajax) {
content_type('text/xml');
}
Expand Down
2 changes: 1 addition & 1 deletion t/02_request/15_headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Test::TCP::test_tcp(
show_errors => 1,
startup_info => 0 );

after sub {
hook after => sub {
my $response = shift;
$response->header('X-Foo', 2);
};
Expand Down
10 changes: 5 additions & 5 deletions t/03_route_handler/05_filter.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ plan tests => 19;
{
my $i = 0;

before sub { content_type('text/xhtml'); };
before sub {
hook before => sub { content_type('text/xhtml'); };
hook before => sub {
if ( request->path_info eq '/redirect_from' ) {
redirect('/redirect_to');
}
Expand Down Expand Up @@ -53,7 +53,7 @@ plan tests => 19;

# filters and params
{
before sub {
hook before => sub {
return if request->path !~ /foo/;
ok( defined( params->{'format'} ),
"param format is defined in before filter" );
Expand All @@ -70,13 +70,13 @@ plan tests => 19;

# filter and halt
{
before sub {
hook before => sub {
unless (params->{'requested'}) {
return halt("stopped");
}
};

before sub {
hook before => sub {
unless (params->{'requested'}) {
halt("another halt");
}
Expand Down
2 changes: 1 addition & 1 deletion t/03_route_handler/18_auto_page.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Dancer::Test;
package Foo;
use Dancer;

before_template sub {
hook before_template => sub {
my $tokens = shift;
$tokens->{title} = "Dancer";
};
Expand Down
2 changes: 1 addition & 1 deletion t/03_route_handler/23_filter_error_catching.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Dancer::Test;
set show_errors => true;

{
before sub {
hook before => sub {
FooBar->send_error; # FAIL
};

Expand Down
2 changes: 1 addition & 1 deletion t/03_route_handler/29_redirect_immediately.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ plan tests => 5;

my $pass = 0;

before sub {
hook before => sub {
redirect '/'
unless request->path eq '/'
|| request->path eq '';
Expand Down
2 changes: 1 addition & 1 deletion t/03_route_handler/31_infinite_loop.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ response_status_is [GET => "/$i"] => 200,
'before not installed yet, response status is 200 looks good for GET /0';
response_content_is [GET => "/$i"], "whatever $i";

before sub {
hook before => sub {
++$i;
request->path_info("/$i");
};
Expand Down
2 changes: 1 addition & 1 deletion t/03_route_handler/99_bugs.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ plan tests => 19;
{
my $i = 0;

before( sub { redirect '/somewhere' if request->path eq '/' } );
hook before => sub { redirect '/somewhere' if request->path eq '/' };
get( '/', sub { $i++; 'Hello' } );

route_exists [ GET => '/' ];
Expand Down
2 changes: 1 addition & 1 deletion t/08_session/10_filter.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Test::More import => ['!pass'], tests => 2;
use Dancer ':syntax';
use Dancer::Test;

before sub {
hook before => sub {
my $data = session;
#warn "on a $data";
#redirect '/nonexistent'
Expand Down
2 changes: 1 addition & 1 deletion t/12_response/06_filter_halt_status.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plan skip_all => "JSON is needed to run this tests"
set serializer => 'JSON';
set environment => 'production';

before sub {
hook before => sub {
if (params->{'troll'}) {
status 401;
return halt({error => "Go away you troll!"})
Expand Down
2 changes: 1 addition & 1 deletion t/14_serializer/17_clear_serializer.t
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Test::TCP::test_tcp(

get '/' => sub { $data };

after sub { set serializer => undef };
hook after => sub { set serializer => undef };

Dancer->dance();
},
Expand Down
13 changes: 4 additions & 9 deletions t/22_hooks/00_syntax.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ use Test::More import => ['!pass'];

use Dancer ':syntax';

plan tests => 5;
plan tests => 4;

ok( before( sub { 'block before' } ), 'add a before filter' );
ok( after( sub { 'block after' } ), 'add an after filter' );
ok( hook(before => sub { 'block before' } ), 'add a before filter' );
ok( hook(after => sub { 'block after' } ), 'add an after filter' );

ok( before_template( sub { 'block before_template' } ),
ok( hook(before_template=> sub { 'block before_template' } ),
'add a before_template filter' );

ok(
hook( 'before', sub { 'block before' } ),
'add a before filter using the hook keyword'
);

eval {
hook( 'before', 'This is not a CodeRef' ),
};
Expand Down
4 changes: 2 additions & 2 deletions t/22_hooks/02_before.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ plan tests => 15;
my $i = 0;

ok(
before(
hook (before =>
sub {
content_type('text/xhtml');
}
)
);

ok(
before(
hook(before=>
sub {
if ( request->path_info eq '/redirect_from' ) {
redirect('/redirect_to');
Expand Down
2 changes: 1 addition & 1 deletion t/22_hooks/03_after.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Dancer::Test;
plan tests => 3;

ok(
after(
hook(after =>
sub {
my $response = shift;
$response->content('not index!');
Expand Down
2 changes: 1 addition & 1 deletion t/22_hooks/04_template.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Time::HiRes qw/gettimeofday tv_interval/;
my ($t0, $elapsed);

ok(
before_template sub {
hook before_template => sub {
my $tokens = shift;
$tokens->{foo} = 'bar';
$t0 = [gettimeofday];
Expand Down
2 changes: 1 addition & 1 deletion t/22_hooks/10_error_in_hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Test::More tests => 3, import => ['!pass'];
use Dancer ':syntax';
use Dancer::Test;

before_template sub {
hook before_template => sub {
die "plop";
};

Expand Down
2 changes: 1 addition & 1 deletion t/22_hooks/11_error_in_hook.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Test::More tests => 3, import => ['!pass'];
use Dancer ':syntax';
use Dancer::Test;

before_template sub {
hook before_template => sub {
status 500;
halt({error => "This is some error"});
};
Expand Down
2 changes: 1 addition & 1 deletion t/lib/LinkBlocker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Dancer::Plugin;

register block_links_from => sub {
my ($host) = @_;
before sub {
hook before => sub {
if (request->referer && request->referer =~ /http:\/\/$host/) {
status 403;
}
Expand Down
2 changes: 1 addition & 1 deletion t/lib/MyAppFoo.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package MyAppFoo;

use Dancer ':syntax';

before {apps => [qw/MyAppFoo/]}, sub {
hook before {apps => [qw/MyAppFoo/]}, sub {
halt('before block in foo');
};

Expand Down

0 comments on commit e967b48

Please sign in to comment.