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

sartak at bestpractical.com sartak at bestpractical.com
Fri Aug 1 14:19:39 EDT 2008


Author: sartak
Date: Fri Aug  1 14:19:39 2008
New Revision: 14716

Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm
   Path-Dispatcher/trunk/t/004-stages.t
   Path-Dispatcher/trunk/t/005-multi-rule.t
   Path-Dispatcher/trunk/t/006-abort.t
   Path-Dispatcher/trunk/t/008-super-dispatcher.t

Log:
 r68297 at onn:  sartak | 2008-08-01 14:19:34 -0400
 Support for adding stages (plugins!) and adding rules to before/on/after any stage. Rename the before and after stages to first and last.


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher.pm	Fri Aug  1 14:19:39 2008
@@ -6,7 +6,6 @@
 use Path::Dispatcher::Rule;
 
 sub rule_class { 'Path::Dispatcher::Rule' }
-sub stages { qw/before on after/ }
 
 has _rules => (
     metaclass => 'Collection::Array',
@@ -36,6 +35,23 @@
     },
 );
 
+has _stages => (
+    metaclass  => 'Collection::Array',
+    is         => 'rw',
+    isa        => 'ArrayRef[Str]',
+    default    => sub { [ 'on' ] },
+    provides   => {
+        push     => 'push_stage',
+        unshift  => 'unshift_stage',
+    },
+);
+
+sub stages {
+    my $self = shift;
+
+    return ('first', @{ $self->_stages }, 'last');
+}
+
 sub add_rule {
     my $self = shift;
 
@@ -64,28 +80,37 @@
         for $self->rules;
 
     for my $stage ($self->stages) {
-        $self->begin_stage($stage, \@matches);
+        for my $substage ('before', 'on', 'after') {
+            my $qualified_stage = $substage eq 'on'
+                                ? $stage
+                                : "${substage}_$stage";
+
+            $self->begin_stage($qualified_stage, \@matches);
+
+            for my $rule (@{ delete $rules_for_stage{$qualified_stage}||[] }) {
+                my $vars = $rule->match($path)
+                    or next;
+
+                push @matches, {
+                    stage  => $qualified_stage,
+                    rule   => $rule,
+                    result => $vars,
+                };
 
-        for my $rule (@{ $rules_for_stage{$stage} || [] }) {
-            my $vars = $rule->match($path)
-                or next;
-
-            push @matches, {
-                stage  => $stage,
-                rule   => $rule,
-                result => $vars,
-            };
+                last if !$rule->fallthrough;
+            }
 
-            last if !$rule->fallthrough;
-        }
+            if ($self->defer_to_super_dispatcher($qualified_stage, \@matches)) {
+                push @matches, $self->super_dispatcher->dispatch($path);
+            }
 
-        if ($self->defer_to_super_dispatcher($stage, \@matches)) {
-            push @matches, $self->super_dispatcher->dispatch($path);
+            $self->end_stage($qualified_stage, \@matches);
         }
-
-        $self->end_stage($stage, \@matches);
     }
 
+    warn "Unhandled stages: " . join(', ', keys %rules_for_stage)
+        if keys %rules_for_stage;
+
     return if !@matches;
 
     return $self->build_runner(

Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm	Fri Aug  1 14:19:39 2008
@@ -59,14 +59,14 @@
         },
         before => sub {
             $dispatcher->add_rule(
-                stage => 'before',
+                stage => 'first',
                 regex => $_[0],
                 block => $_[1],
             );
         },
         after => sub {
             $dispatcher->add_rule(
-                stage => 'after',
+                stage => 'last',
                 regex => $_[0],
                 block => $_[1],
             );

Modified: Path-Dispatcher/trunk/t/004-stages.t
==============================================================================
--- Path-Dispatcher/trunk/t/004-stages.t	(original)
+++ Path-Dispatcher/trunk/t/004-stages.t	Fri Aug  1 14:19:39 2008
@@ -7,14 +7,24 @@
 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 },
-    );
+for my $stage (qw/first on last/) {
+    for my $substage (qw/before on after/) {
+        my $qualified_stage = $substage eq 'on'
+                            ? $stage
+                            : "${substage}_$stage";
+
+        $dispatcher->add_rule(
+            stage => $qualified_stage,
+            regex => qr/foo/,
+            block => sub { push @calls, $qualified_stage },
+        );
+    }
 }
 
 $dispatcher->run('foo');
-is_deeply(\@calls, ['before', 'on', 'after']);
+is_deeply(\@calls, [
+    'before_first', 'first', 'after_first',
+    'before_on',    'on',    'after_on',
+    'before_last',  'last',  'after_last',
+]);
 

Modified: Path-Dispatcher/trunk/t/005-multi-rule.t
==============================================================================
--- Path-Dispatcher/trunk/t/005-multi-rule.t	(original)
+++ Path-Dispatcher/trunk/t/005-multi-rule.t	Fri Aug  1 14:19:39 2008
@@ -7,7 +7,7 @@
 my @calls;
 
 my $dispatcher = Path::Dispatcher->new;
-for my $stage (qw/before on after/) {
+for my $stage (qw/first on last/) {
     for my $number (qw/first second/) {
         $dispatcher->add_rule(
             stage => $stage,
@@ -19,10 +19,10 @@
 
 $dispatcher->run('foo');
 is_deeply(\@calls, [
-    'before: first',
-    'before: second',
+    'first: first',
+    'first: second',
     'on: first',
-    'after: first',
-    'after: second',
+    'last: first',
+    'last: second',
 ]);
 

Modified: Path-Dispatcher/trunk/t/006-abort.t
==============================================================================
--- Path-Dispatcher/trunk/t/006-abort.t	(original)
+++ Path-Dispatcher/trunk/t/006-abort.t	Fri Aug  1 14:19:39 2008
@@ -17,10 +17,10 @@
 );
 
 $dispatcher->add_rule(
-    stage => 'after',
+    stage => 'last',
     regex => qr/foo/,
     block => sub {
-        push @calls, "after";
+        push @calls, "last";
     },
 );
 
@@ -40,7 +40,7 @@
     block => sub {
         push @calls, "bar: before";
         my $x = {}->();
-        push @calls, "bar: after";
+        push @calls, "bar: last";
     },
 );
 

Modified: Path-Dispatcher/trunk/t/008-super-dispatcher.t
==============================================================================
--- Path-Dispatcher/trunk/t/008-super-dispatcher.t	(original)
+++ Path-Dispatcher/trunk/t/008-super-dispatcher.t	Fri Aug  1 14:19:39 2008
@@ -17,7 +17,7 @@
 ok($sub_dispatcher->has_super_dispatcher, "sub dispatcher has a super");
 is($sub_dispatcher->super_dispatcher, $super_dispatcher, "the super dispatcher is correct");
 
-for my $stage (qw/before on after/) {
+for my $stage (qw/before_on on after_on/) {
     $super_dispatcher->add_rule(
         stage => $stage,
         regex => qr/foo/,
@@ -25,7 +25,7 @@
     );
 }
 
-for my $stage (qw/before after/) {
+for my $stage (qw/before_on after_on/) {
     $sub_dispatcher->add_rule(
         stage => $stage,
         regex => qr/foo/,
@@ -35,18 +35,18 @@
 
 $super_dispatcher->run('foo');
 is_deeply([splice @calls], [
-    'super before',
+    'super before_on',
     'super on',
-    'super after',
+    'super after_on',
 ]);
 
 $sub_dispatcher->run('foo');
 is_deeply([splice @calls], [
-    'sub before',
-    'super before',
+    'sub before_on',
+    'super before_on',
     'super on',
-    'super after',
-    'sub after',
+    'super after_on',
+    'sub after_on',
 ]);
 
 $sub_dispatcher->add_rule(
@@ -57,8 +57,8 @@
 
 $sub_dispatcher->run('foo');
 is_deeply([splice @calls], [
-    'sub before',
+    'sub before_on',
     'sub on',
-    'sub after',
+    'sub after_on',
 ]);
 



More information about the Bps-public-commit mailing list