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

sartak at bestpractical.com sartak at bestpractical.com
Sun Oct 19 07:40:50 EDT 2008


Author: sartak
Date: Sun Oct 19 07:40:40 2008
New Revision: 16374

Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Declarative.pm
   Path-Dispatcher/trunk/t/016-more-under.t

Log:
 r74152 at onn:  sartak | 2008-10-19 07:39:51 -0400
 Figure out a syntax for under rules that works well enough for v1


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	Sun Oct 19 07:40:40 2008
@@ -8,6 +8,7 @@
 
 our $CALLER; # Sub::Exporter doesn't make this available
 our $OUTERMOST_DISPATCHER;
+our $UNDER_RULE;
 
 my $exporter = Sub::Exporter::build_exporter({
     into_level => 1,
@@ -83,15 +84,21 @@
             _add_rule($dispatcher, 'after_on', @_);
         },
         under => sub {
-            my $predicate = _create_rule('on', shift);
+            my ($matcher, $rules) = @_;
+
+            my $predicate = _create_rule('on', $matcher);
             $predicate->prefix(1);
-            my @rules = @_;
 
             my $under = Path::Dispatcher::Rule::Under->new(
                 predicate => $predicate,
-                rules     => \@rules,
             );
-            $dispatcher->add_rule($under);
+
+            do {
+                local $UNDER_RULE = $under;
+                $rules->();
+            };
+
+            _add_rule($dispatcher, $under, @_);
         },
         next_rule => sub { die "Path::Dispatcher next rule\n" },
         last_rule => sub { die "Path::Dispatcher abort\n" },

Modified: Path-Dispatcher/trunk/t/016-more-under.t
==============================================================================
--- Path-Dispatcher/trunk/t/016-more-under.t	(original)
+++ Path-Dispatcher/trunk/t/016-more-under.t	Sun Oct 19 07:40:40 2008
@@ -1,7 +1,7 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
-use Test::More tests => 3;
+use Test::More tests => 8;
 
 my @calls;
 
@@ -9,10 +9,21 @@
     package Under::Where;
     use Path::Dispatcher::Declarative -base;
 
-    under [ 'ticket' ] => (
-        on 'create' => sub { push @calls, "ticket create" },
-        on 'update' => sub { push @calls, "ticket update" },
-    );
+    under 'ticket' => sub {
+        on 'create' => sub { push @calls, "ticket create" };
+        on 'update' => sub { push @calls, "ticket update" };
+    };
+
+    under 'blog' => sub {
+        under 'post' => sub {
+            on 'create' => sub { push @calls, "create blog post" };
+            on 'delete' => sub { push @calls, "delete blog post" };
+        };
+        under 'comment' => sub {
+            on 'create' => sub { push @calls, "create blog comment" };
+            on 'delete' => sub { push @calls, "delete blog comment" };
+        };
+    };
 };
 
 Under::Where->run('ticket create');
@@ -24,3 +35,18 @@
 Under::Where->run('ticket foo');
 is_deeply([splice @calls], []);
 
+Under::Where->run('blog');
+is_deeply([splice @calls], []);
+
+Under::Where->run('blog post');
+is_deeply([splice @calls], []);
+
+Under::Where->run('blog post create');
+is_deeply([splice @calls], ['create blog post']);
+
+Under::Where->run('blog comment');
+is_deeply([splice @calls], []);
+
+Under::Where->run('blog comment create');
+is_deeply([splice @calls], ['create blog comment']);
+



More information about the Bps-public-commit mailing list