File Coverage

blib/lib/Catalyst/Plugin/RedirectAndDetach.pm
Criterion Covered Total %
statement 9 13 69.2
branch n/a
condition n/a
subroutine 3 4 75.0
pod 1 1 100.0
total 13 18 72.2


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::RedirectAndDetach;
2              
3 1     1   781 use strict;
  1         2  
  1         41  
4 1     1   7 use warnings;
  1         1  
  1         53  
5              
6             our $VERSION = '0.03';
7              
8 1     1   1157 use Params::Validate qw( validate_pos SCALAR OBJECT );
  1         10909  
  1         180  
9              
10              
11             {
12             my @spec = ( { type => SCALAR | OBJECT },
13             { type => SCALAR, default => undef } );
14              
15             sub redirect_and_detach
16             {
17 0     0 1   my $self = shift;
18 0           my ( $uri, $status ) = validate_pos( @_, @spec );
19              
20 0           $self->response()->redirect( $uri, $status );
21              
22 0           $self->detach();
23             }
24             }
25              
26             1;
27              
28             __END__
29              
30             =pod
31              
32             =head1 NAME
33              
34             Catalyst::Plugin::RedirectAndDetach - Redirect and detach at the same time
35              
36             =head1 SYNOPSIS
37              
38             # load the plugin
39              
40             use Catalyst qw( RedirectAndDetach );
41              
42             # in your controller ...
43              
44             sub admin
45             {
46             my $self = shift;
47             my $c = shift;
48              
49             $c->redirect_and_detach('/')
50             unless $c->stash()->{user}->is_admin();
51              
52             ...
53             }
54              
55             =head1 DESCRIPTION
56              
57             I generally find that if I want to issue a redirect in my web app, I
58             want to stop processing right there. This plugin adds a ridiculously
59             simply method to your Catalyst objects to do just that.
60              
61             The reason to use C<detach()> instead of simply returning from the
62             current sub is that C<detach()> throws an exception that effectively
63             aborts all execution, rather than simply exiting the current method.
64              
65             =head1 METHODS
66              
67             This class provides one method:
68              
69             =head2 $c->redirect_and_detach( $uri, $status )
70              
71             The C<$uri> parameter is required, and C<$status> is
72             optional. Internally, this just calls C<redirect()> on the Response
73             object, followed by C<detach()>.
74              
75             =head1 AUTHOR
76              
77             Dave Rolsky, C<< <autarch@urth.org> >>
78              
79             =head1 BUGS
80              
81             Please report any bugs or feature requests to
82             C<bug-catalyst-plugin-redirectanddetach@rt.cpan.org>, or through the
83             web interface at L<http://rt.cpan.org>. I will be notified, and then
84             you'll automatically be notified of progress on your bug as I make
85             changes.
86              
87             =head1 COPYRIGHT & LICENSE
88              
89             Copyright 2008 Dave Rolsky, All Rights Reserved.
90              
91             This program is free software; you can redistribute it and/or modify
92             it under the same terms as Perl itself.
93              
94             =cut