[Bps-public-commit] r14621 - in Path-Dispatcher/trunk: lib/Path t

sartak at bestpractical.com sartak at bestpractical.com
Tue Jul 29 16:09:38 EDT 2008


Author: sartak
Date: Tue Jul 29 16:09:34 2008
New Revision: 14621

Added:
   Path-Dispatcher/trunk/t/004-stages.t
Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher.pm

Log:
 r67911 at onn:  sartak | 2008-07-29 16:09:21 -0400
 Run rules in stages


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	Tue Jul 29 16:09:34 2008
@@ -6,6 +6,7 @@
 use Path::Dispatcher::Rule;
 
 sub rule_class { 'Path::Dispatcher::Rule' }
+sub stages { qw/before on after/ }
 
 has _rules => (
     metaclass => 'Collection::Array',
@@ -40,15 +41,25 @@
     my $path = shift;
 
     my @matches;
+    my %rules_for_stage;
 
-    for my $rule ($self->rules) {
-        my $vars = $rule->match($path)
-            or next;
-
-        push @matches, {
-            rule => $rule,
-            vars => $vars,
-        };
+    push @{ $rules_for_stage{$_->stage} }, $_
+        for $self->rules;
+
+    for my $stage ($self->stages) {
+        $self->begin_stage($stage);
+
+        for my $rule (@{ $rules_for_stage{$stage} || [] }) {
+            my $vars = $rule->match($path)
+                or next;
+
+            push @matches, {
+                rule => $rule,
+                vars => $vars,
+            };
+        }
+
+        $self->end_stage($stage);
     }
 
     return if !@matches;
@@ -98,6 +109,9 @@
     return $code->();
 }
 
+sub begin_stage {}
+sub end_stage {}
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Added: Path-Dispatcher/trunk/t/004-stages.t
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/t/004-stages.t	Tue Jul 29 16:09:34 2008
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 1;
+use Path::Dispatcher;
+
+my @calls;
+
+my $dispatcher = Path::Dispatcher->new;
+for my $stage (qw/after on before/) {
+    $dispatcher->add_rule(
+        stage => $stage,
+        regex => qr/foo/,
+        block => sub { push @calls, $stage },
+    );
+}
+
+$dispatcher->run('foo');
+is_deeply(\@calls, ['before', 'on', 'after']);
+



More information about the Bps-public-commit mailing list