[Catalyst-commits] r10161 - in
Catalyst-Plugin-RunAfterRequest/trunk: .
lib/Catalyst/Model/Role lib/Catalyst/Plugin t
t/lib/TestApp/Controller t/lib/TestApp/Model
evdb at dev.catalyst.perl.org
evdb at dev.catalyst.perl.org
Thu May 14 20:00:21 GMT 2009
Author: evdb
Date: 2009-05-14 20:00:21 +0000 (Thu, 14 May 2009)
New Revision: 10161
Modified:
Catalyst-Plugin-RunAfterRequest/trunk/
Catalyst-Plugin-RunAfterRequest/trunk/Makefile.PL
Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Model/Role/RunAfterRequest.pm
Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Plugin/RunAfterRequest.pm
Catalyst-Plugin-RunAfterRequest/trunk/t/01basic.t
Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Controller/Foo.pm
Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Model/Foo.pm
Log:
POD and some renaming of methods for consistency
Property changes on: Catalyst-Plugin-RunAfterRequest/trunk
___________________________________________________________________
Name: svn:ignore
- META.yml
Makefile
inc
+ META.yml
Makefile
inc
blib
pm_to_blib
Modified: Catalyst-Plugin-RunAfterRequest/trunk/Makefile.PL
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/Makefile.PL 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/Makefile.PL 2009-05-14 20:00:21 UTC (rev 10161)
@@ -6,8 +6,14 @@
all_from('lib/Catalyst/Plugin/RunAfterRequest.pm');
+requires 'Catalyst::Component::InstancePerContext';
+requires 'Catalyst::Controller';
requires 'Catalyst::Runtime';
-requires 'Catalyst::Component::InstancePerContext';
+requires 'Catalyst::Test';
requires 'Moose';
+requires 'Moose::Role';
+requires 'MRO::Compat';
+requires 'Test::More';
WriteAll;
+
Modified: Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Model/Role/RunAfterRequest.pm
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Model/Role/RunAfterRequest.pm 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Model/Role/RunAfterRequest.pm 2009-05-14 20:00:21 UTC (rev 10161)
@@ -5,16 +5,31 @@
with 'Catalyst::Component::InstancePerContext';
-has '_context' => (is => 'ro', weak_ref => 1);
+has '_context' => ( is => 'ro', weak_ref => 1 );
sub build_per_context_instance {
- my ($self, $c) = @_;
- bless({ %$self, _context => $c}, ref($self));
+ my ( $self, $c ) = @_;
+ bless( { %$self, _context => $c }, ref($self) );
}
-sub _run_after_request {
- my $self = shift;
- $self->_context->run_after_request(@_);
+sub run_after_request {
+ my $self = shift;
+ $self->_context->run_after_request(@_);
}
+=head1 NAME
+
+Catalyst::Model::Role::RunAfterRequest - run code after the response has been sent.
+
+=head1 DESCRIPTION
+
+See L<Catalyst::Plugin::RunAfterRequest> for full documentation.
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
1;
Modified: Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Plugin/RunAfterRequest.pm
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Plugin/RunAfterRequest.pm 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/lib/Catalyst/Plugin/RunAfterRequest.pm 2009-05-14 20:00:21 UTC (rev 10161)
@@ -7,31 +7,101 @@
our $VERSION = '1.000000';
sub run_after_request {
- my $self = shift;
- push(@{$self->{run_after_request}||=[]}, @_);
+ my $self = shift;
+ push( @{ $self->{run_after_request} ||= [] }, @_ );
}
sub finalize {
- my $self = shift;
- $self->next::method(@_);
- $self->_run_code_after_request;
+ my $self = shift;
+ $self->next::method(@_);
+ $self->_run_code_after_request;
}
sub _run_code_after_request {
- my $self = shift;
- $_->($self) for @{$self->{run_after_request}||[]};
+ my $self = shift;
+ $_->($self) for @{ $self->{run_after_request} || [] };
}
=head1 NAME
-Catalyst::Plugin::RunAfterRequest - run things after the response has been sent
+Catalyst::Plugin::RunAfterRequest - run code after the response has been sent.
+=head1 SYNOPSIS
+
+ #### In MyApp.pm
+ use Catalyst qw(RunAfterRequest);
+
+ #### In your controller
+ sub my_action : Local {
+ my ( $self, $c ) = @_;
+
+ # do your normal processing...
+
+ # add code that runs after response has been sent to client
+ $c->run_after_request( #
+ sub { $self->do_something_slow(); },
+ sub { $self->do_something_else_as_well(); }
+ );
+
+ # continue handling the request
+ }
+
+
+ #### Or in your Model:
+
+ package MyApp::Model::Foo;
+
+ use Moose;
+ extends 'Catalyst::Model';
+ with 'Catalyst::Model::Role::RunAfterRequest';
+
+ sub some_method {
+ my $self = shift;
+
+ $self->run_after_request(
+ sub { $self->do_something_slow(); },
+ sub { $self->do_something_else_as_well(); }
+ );
+ }
+
+
+=head1 DESCRIPTION
+
+Sometimes you want to run something after you've sent the reponse back to the
+client. For example you might want to send a tweet to Twitter, or do some
+logging, or something that will take a long time and would delay the response.
+
+This module provides a conveniant way to do that by simply calling
+C<run_after_request> and adding a closure to it.
+
+=head1 METHODS
+
+=head2 run_after_request
+
+ $c->run_after_request(
+ sub {
+ # create preview of uploaded file and store to remote server
+ # etc, etc
+ },
+ sub {
+ # another closure...
+ }
+ );
+
+Takes one or more anonymous subs and adds them to a list to be run after the
+response has been sent back to the client.
+
=head1 AUTHOR
Matt S Trout (mst) <mst at shadowcat.co.uk>
-Edmund Von Der Burg (evdb) <email here>
+Edmund von der Burg (evdb) <evdb at ecclestoad.co.uk>
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
=cut
1;
Modified: Catalyst-Plugin-RunAfterRequest/trunk/t/01basic.t
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/t/01basic.t 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/t/01basic.t 2009-05-14 20:00:21 UTC (rev 10161)
@@ -1,15 +1,29 @@
use strict;
use warnings;
use lib 't/lib';
-use Test::More qw(no_plan);
+use Test::More tests => 4;
use Catalyst::Test 'TestApp';
-my $res = request('/foo/demonstrate');
+{
+ my $res = request('/foo/demonstrate_model');
-ok($res->is_success, 'Test request is a success');
+ ok( $res->is_success, 'Test request is a success' );
-is_deeply(
- \@TestApp::Model::Foo::data,
- [ qw(one two) ],
- 'Data saved ok'
-);
+ is_deeply(
+ \@TestApp::Model::Foo::data, #
+ [qw( one two TestApp )],
+ 'Data saved ok from model'
+ );
+}
+
+{
+ my $res = request('/foo/demonstrate_plugin');
+
+ ok( $res->is_success, 'Test request is a success' );
+
+ is_deeply(
+ \@TestApp::Controller::Foo::data,
+ [qw( alpha beta TestApp )], #
+ 'Data saved ok from controller'
+ );
+}
Modified: Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Controller/Foo.pm
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Controller/Foo.pm 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Controller/Foo.pm 2009-05-14 20:00:21 UTC (rev 10161)
@@ -1,12 +1,27 @@
package TestApp::Controller::Foo;
-
use base qw(Catalyst::Controller);
-use Moose;
-sub demonstrate :Local {
- my ($self, $c) = @_;
- $c->res->body('YAY');
- $c->model('Foo')->demonstrate;
+use strict;
+use warnings;
+
+our @data;
+
+sub demonstrate_model : Local {
+ my ( $self, $c ) = @_;
+ $c->res->body('YAY');
+ $c->model('Foo')->demonstrate;
}
+sub demonstrate_plugin : Local {
+ my ( $self, $c ) = @_;
+ $c->res->body('YAY');
+
+ $c->run_after_request(
+ sub { push @data, 'alpha' },
+ sub { push @data, 'beta' },
+ sub { push @data, ref shift },
+ );
+
+}
+
1;
Modified: Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Model/Foo.pm
===================================================================
--- Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Model/Foo.pm 2009-05-14 19:03:03 UTC (rev 10160)
+++ Catalyst-Plugin-RunAfterRequest/trunk/t/lib/TestApp/Model/Foo.pm 2009-05-14 20:00:21 UTC (rev 10161)
@@ -9,11 +9,12 @@
with 'Catalyst::Model::Role::RunAfterRequest';
sub demonstrate {
- my $self = shift;
- $self->_run_after_request(
- sub { push(@data, "one"); },
- sub { push(@data, "two"); },
- );
+ my $self = shift;
+ $self->run_after_request(
+ sub { push( @data, "one" ); },
+ sub { push( @data, "two" ); },
+ sub { push @data, ref shift },
+ );
}
1;
More information about the Catalyst-commits
mailing list