[Jifty-commit] r725 - in jifty/trunk: lib/Jifty t/TestApp/lib/TestApp t/TestApp/t t/TestApp/web/templates/dispatch

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Mar 21 16:11:51 EST 2006


Author: alexmv
Date: Tue Mar 21 16:11:51 2006
New Revision: 725

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Dispatcher.pm
   jifty/trunk/t/TestApp/lib/TestApp/Dispatcher.pm
   jifty/trunk/t/TestApp/t/02-dispatch.t
   jifty/trunk/t/TestApp/web/templates/dispatch/basic
   jifty/trunk/t/TestApp/web/templates/dispatch/basic-show

Log:
 r11758 at zoq-fot-pik:  chmrr | 2006-03-21 16:11:42 -0500
  * 'return if already_run' in after rules so they run only once


Modified: jifty/trunk/lib/Jifty/Dispatcher.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Dispatcher.pm	(original)
+++ jifty/trunk/lib/Jifty/Dispatcher.pm	Tue Mar 21 16:11:51 2006
@@ -231,6 +231,8 @@
 
     get next_rule last_rule
 
+    already_run
+
     $Dispatcher
 >;
 
@@ -586,9 +588,34 @@
     }
 }
 
+=head2 already_run
+
+Returns true if the code block has run once already in this request.
+This can be useful for 'after' rules to ensure that they only run
+once, even if there is a sub-dispatch which would cause it to run more
+than once.  The idiom is:
+
+    after '/some/path/*' => run {
+        return if already_run;
+        # ...
+    };
+
+=cut
+
+sub already_run {
+    my $id = $Dispatcher->{call_rule};
+    return 1 if get "__seen_$id";
+    set "__seen_$id" => 1;
+    return 0;
+}
+
 sub _do_run {
     my ( $self, $code ) = @_;
 
+    # Keep track of the coderef being run, so we can know about
+    # already_run
+    local $self->{call_rule} = $code;
+
     # establish void context and make a call
     ( $self->can($code) || $code )->();
 
@@ -729,16 +756,23 @@
     $self->{path} =~ s{/+}{/}g;
 
     $self->log->debug("Dispatching request to ".$self->{path});
+
     eval {
         $self->_handle_rules( [ $self->rules('SETUP') ] );
         HANDLE_WEB: { Jifty->web->handle_request(); }
         $self->_handle_rules( [ $self->rules('RUN'), 'show' ] );
-        $self->_handle_rules( [ $self->rules('CLEANUP') ] );
     };
     if ( my $err = $@ ) {
         $self->log->warn(ref($err) . " " ."'$err'") if ( $err !~ /^LAST RULE/);
+    }
 
+    eval {
+        $self->_handle_rules( [ $self->rules('CLEANUP') ] );
+    };
+    if ( my $err = $@ ) {
+        $self->log->warn(ref($err) . " " ."'$err'") if ( $err !~ /^LAST RULE/);
     }
+
     last_rule;
 }
 

Modified: jifty/trunk/t/TestApp/lib/TestApp/Dispatcher.pm
==============================================================================
--- jifty/trunk/t/TestApp/lib/TestApp/Dispatcher.pm	(original)
+++ jifty/trunk/t/TestApp/lib/TestApp/Dispatcher.pm	Tue Mar 21 16:11:51 2006
@@ -13,6 +13,7 @@
 my $count = 0;
 my $before = 0;
 my $after = 0;
+my $after_once = 0;
 
 on '/dispatch/basic' => run {
     set count => $count++;
@@ -24,8 +25,14 @@
 };
 
 before '/dispatch/*' => run {
-    set before => $before++;
-    set after => $after;
+    set before     => $before++;
+    set after      => $after;
+    set after_once => $after_once;
+};
+
+after '/dispatch/*' => run {
+    return if already_run;
+    $after_once++;
 };
 
 after '/dispatch/*' => run {

Modified: jifty/trunk/t/TestApp/t/02-dispatch.t
==============================================================================
--- jifty/trunk/t/TestApp/t/02-dispatch.t	(original)
+++ jifty/trunk/t/TestApp/t/02-dispatch.t	Tue Mar 21 16:11:51 2006
@@ -4,7 +4,7 @@
 
 BEGIN {chdir "t/TestApp"}
 use lib '../../lib';
-use Jifty::Test tests => 24;
+use Jifty::Test tests => 28;
 use Jifty::Test::WWW::Mechanize;
 
 my $server  = Jifty::Test->make_server;
@@ -16,29 +16,33 @@
 
 $mech->get_ok("$URL/dispatch/basic", "Got /dispatch/basic");
 $mech->content_contains("Basic test.");
-$mech->content_contains("Count 0");
-$mech->content_contains("before 0");
-$mech->content_contains("after 0");
+$mech->content_contains("count: 0");
+$mech->content_contains("before: 0");
+$mech->content_contains("after: 0");
+$mech->content_contains("after_once: 0");
 
 $mech->get_ok("$URL/dispatch/basic-show", "Got /dispatch/basic-show");
 $mech->content_contains("Basic test with forced show.");
-$mech->content_contains("Count 1");
-$mech->content_contains("before 1");
-$mech->content_contains("after 1");
+$mech->content_contains("count: 1");
+$mech->content_contains("before: 1");
+$mech->content_contains("after: 1");
+$mech->content_contains("after_once: 1");
 
 $mech->get_ok("$URL/dispatch/show/", "Got /dispatch/show");
 $mech->content_contains("Basic test with forced show.");
-$mech->content_contains("Count 2");
-$mech->content_lacks("Count 3");
-$mech->content_contains("before 3");
-$mech->content_contains("after 2");
+$mech->content_contains("count: 2");
+$mech->content_lacks("count: 3");
+$mech->content_contains("before: 3");
+$mech->content_contains("after: 2");
+$mech->content_contains("after_once: 2");
 
 $mech->get_ok("$URL/dispatch/", "Got /dispatch/");
 $mech->content_contains("Basic test.");
-$mech->content_contains("Count 3");
-$mech->content_lacks("Count 4");
-$mech->content_contains("before 4");
-$mech->content_contains("after 3");
+$mech->content_contains("count: 3");
+$mech->content_lacks("count: 4");
+$mech->content_contains("before: 4");
+$mech->content_contains("after: 4");
+$mech->content_contains("after_once: 3");
 
 
 

Modified: jifty/trunk/t/TestApp/web/templates/dispatch/basic
==============================================================================
--- jifty/trunk/t/TestApp/web/templates/dispatch/basic	(original)
+++ jifty/trunk/t/TestApp/web/templates/dispatch/basic	Tue Mar 21 16:11:51 2006
@@ -1,3 +1,4 @@
 <&| /_elements/wrapper, title => "Test of dispatch" &>
-  Basic test.  (Count <% $ARGS{count} %>, before <% $ARGS{before} %>, after <% $ARGS{after} %>)
+  Basic test.
+<% YAML::Dump(\%ARGS) %>
 </&>

Modified: jifty/trunk/t/TestApp/web/templates/dispatch/basic-show
==============================================================================
--- jifty/trunk/t/TestApp/web/templates/dispatch/basic-show	(original)
+++ jifty/trunk/t/TestApp/web/templates/dispatch/basic-show	Tue Mar 21 16:11:51 2006
@@ -1,3 +1,4 @@
 <&| /_elements/wrapper, title => "Test of dispatch" &>
-  Basic test with forced show.  (Count <% $ARGS{count} %>, before <% $ARGS{before} %>, after <% $ARGS{after} %>)
+  Basic test with forced show.
+<% YAML::Dump(\%ARGS) %>
 </&>


More information about the Jifty-commit mailing list