[Bps-public-commit] Path-Dispatcher branch, master, updated. a658d28a3c6da24bd52ded2ba10dca6f06038bb6

sartak at bestpractical.com sartak at bestpractical.com
Tue Feb 24 15:54:01 EST 2009


The branch, master has been updated
       via  a658d28a3c6da24bd52ded2ba10dca6f06038bb6 (commit)
       via  1f9c5def8061271bc131a73e1796cd0ad7ccf9b8 (commit)
       via  df36c0b25b74253ee3d07e6849a90790ff648416 (commit)
       via  9cf0fb9b151916d6ea664d1db30373a67f42c56b (commit)
       via  06e9d853108a27f771f0b909e6ed78fd31da2c58 (commit)
      from  46f66f1b1a95cf1c9181107c433e7a418459adab (commit)

Summary of changes:
 Changes                            |    3 ++
 lib/Path/Dispatcher/Cookbook.pod   |   53 ++++++++++++++++++++++++++++++++++++
 lib/Path/Dispatcher/Declarative.pm |   47 ++++++++++++++++++++++++++++++-
 t/100-declarative.t                |   46 ++++++++++++++++++++++++++++++-
 t/800-cb-slash-path-delimeter.t    |   33 ++++++++++++++++++++++
 t/801-cb-chaining.t                |   41 +++++++++++++++++++++++++++
 6 files changed, 220 insertions(+), 3 deletions(-)
 create mode 100644 lib/Path/Dispatcher/Cookbook.pod
 create mode 100644 t/800-cb-slash-path-delimeter.t
 create mode 100644 t/801-cb-chaining.t

- Log -----------------------------------------------------------------
commit 06e9d853108a27f771f0b909e6ed78fd31da2c58
Author: robertkrimen <robertkrimen at gmail.com>
Date:   Tue Feb 24 12:41:01 2009 -0800

    Added slash-path-delimeter recipe w/test
    Added chaining recipe w/test
    (tenative) Added 'then' sugar to ::Declarative w/test & dox

diff --git a/Changes b/Changes
index 9873f60..20c29a6 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,9 @@
 Revision history for Path-Dispatcher
 
 0.10
+        Added slash-path-delimeter recipe
+        Added chaining recipe
+        (tenative) Added 'then' sugar to ::Declarative
 
 0.09 Mon Feb 9 21:12:18 2009
         Avoid using method modifiers since it's potentially another dep.
diff --git a/lib/Path/Dispatcher/Cookbook.pod b/lib/Path/Dispatcher/Cookbook.pod
new file mode 100644
index 0000000..0649adf
--- /dev/null
+++ b/lib/Path/Dispatcher/Cookbook.pod
@@ -0,0 +1,53 @@
+=pod
+
+=head1 NAME
+
+Path::Dispatcher::Cookbook - A cookbook for Path::Dispatcher
+
+=head1 RECIPES
+
+=head2 How can I change the path delimiter from a space ' ' to a slash '/'?
+
+In your Dispatcher object, define the C<token_delimiter> subroutine to return a slash '/':
+
+    package MyDispatcher;
+    use Path::Dispatcher::Declarative -base;
+
+    sub token_delimiter { '/' } # Or whatever delimiter you want to use
+
+=head2 How can I do rule chaining (like in Catalyst)?
+
+You can use a L<Path::Dispatcher::Rule::Always> rule in combination with C<next_rule> to get chaining behavior:
+
+    package MyDispatcher;
+    use Path::Dispatcher::Declarative -base;
+
+    under show => sub {
+        $Path::Dispatcher::Declarative::UNDER_RULE->add_rule(
+            Path::Dispatcher::Rule::Always->new(
+                stage => 'on',
+                block  => sub {
+                    print "Displaying ";
+                    next_rule;
+                },
+            ),
+        );
+        on inventory => sub {
+            print "inventory:\n";
+            ...
+        };
+        on score => sub {
+            print "score:\n";
+            ...
+        };
+    };
+
+    package main;
+
+    MyDispatcher->run("display inventory"); # "Displaying inventory\n ..."
+
+    MyDispatcher->run("display score"); # "Displaying score\n ..."
+
+It's a little bit ugly, but it works
+
+=cut
diff --git a/lib/Path/Dispatcher/Declarative.pm b/lib/Path/Dispatcher/Declarative.pm
index 705f409..b3b4792 100644
--- a/lib/Path/Dispatcher/Declarative.pm
+++ b/lib/Path/Dispatcher/Declarative.pm
@@ -19,6 +19,14 @@ my $exporter = Sub::Exporter::build_exporter({
 sub token_delimiter { ' ' }
 sub case_sensitive_tokens { undef }
 
+sub _next_rule() {
+    die "Path::Dispatcher next rule\n";
+}
+
+sub _last_rule() {
+    die "Path::Dispatcher abort rule\n";
+}
+
 sub import {
     my $self = shift;
     my $pkg  = caller;
@@ -88,6 +96,17 @@ sub build_sugar {
         after => sub {
             $into->_add_rule('after_on', @_);
         },
+        then => sub (;&) {
+            my $block = shift;
+            my $rule = Path::Dispatcher::Rule::Always->new(
+                stage => 'on',
+                block  => sub {
+                    $block->(@_);
+                    _next_rule;
+                },
+            );
+            $into->_add_rule($rule);
+        },
         under => sub {
             my ($matcher, $rules) = @_;
 
@@ -119,8 +138,8 @@ sub build_sugar {
 
             $into->_add_rule($redispatch);
         },
-        next_rule => sub { die "Path::Dispatcher next rule\n" },
-        last_rule => sub { die "Path::Dispatcher abort\n" },
+        next_rule => \&_next_rule,
+        last_rule => \&_last_rule,
     };
 }
 
@@ -314,5 +333,29 @@ This is creates a L<Path::Dispatcher::Rule::CodeRef> rule.
 Creates a L<Path::Dispatcher::Rule::Under> rule. The contents of the coderef
 should be nothing other L</on> and C<under> calls.
 
+#=head2 then sub { }
+
+#Creates a L<Path::Dispatcher::Rule::Always> rule that will continue on to the
+#next rule via C<next_rule>
+
+#The only argument is a coderef that processes normally (like L<on>)
+
+#NOTE: You *can* avoid running a following rule by uysing C<abort_rule>
+
+#An example:
+
+#    under show => sub {
+#        then {
+#            print "Displaying ";
+#        };
+#        on inventory => sub {
+#            print "inventory:\n";
+#            ...
+#        };
+#        on score => sub {
+#            print "score:\n";
+#            ...
+#        };
+
 =cut
 
diff --git a/t/100-declarative.t b/t/100-declarative.t
index dc059a2..779fa17 100644
--- a/t/100-declarative.t
+++ b/t/100-declarative.t
@@ -1,7 +1,8 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
-use Test::More tests => 7;
+use Test::More tests => 11;
+#use Test::More plan => qw/no_plan/;
 
 my @calls;
 
@@ -19,6 +20,38 @@ do {
 
     rewrite quux => 'bar';
     rewrite qr/^quux-(.*)/ => sub { "bar:$1" };
+
+    on alpha => sub {
+        push @calls, "alpha"
+    };
+
+    under alpha => sub {
+    #    $Path::Dispatcher::Declarative::UNDER_RULE->add_rule(
+    #        Path::Dispatcher::Rule::Always->new(
+    #            stage => 'on',
+    #            block  => sub {
+    #                print "alpha (chain) ";
+    #                next_rule;
+    #            },
+    #        ),
+    #    );
+        then {
+            push @calls, "alpha (chain) "; 
+        };
+        on one => sub {
+            push @calls, "one";
+        };
+
+        then {
+            push @calls, "(before two or three) ";
+        };
+        on two => sub {
+            push @calls, "two";
+        };
+        on three => sub {
+            push @calls, "three";
+        };
+    };
 };
 
 ok(MyApp::Dispatcher->isa('Path::Dispatcher::Declarative'), "use Path::Dispatcher::Declarative sets up ISA");
@@ -47,3 +80,14 @@ is_deeply([splice @calls], [
 MyApp::Dispatcher->run('Token Matching');
 is_deeply([splice @calls], [], "token matching is by default case sensitive");
 
+MyApp::Dispatcher->run('alpha');
+is_deeply([splice @calls], ['alpha']);
+
+MyApp::Dispatcher->run('alpha one');
+is_deeply([splice @calls], ['alpha (chain) ', 'one']);
+
+MyApp::Dispatcher->run('alpha two');
+is_deeply([splice @calls], ['alpha (chain) ', '(before two or three) ', 'two']);
+
+MyApp::Dispatcher->run('alpha three');
+is_deeply([splice @calls], ['alpha (chain) ', '(before two or three) ', 'three']);
diff --git a/t/800-cb-slash-path-delimeter.t b/t/800-cb-slash-path-delimeter.t
new file mode 100644
index 0000000..de87290
--- /dev/null
+++ b/t/800-cb-slash-path-delimeter.t
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+#use Test::More tests => 11;
+use Test::More; plan qw/no_plan/;
+
+my @result;
+
+do {
+    package MyDispatcher;
+    use Path::Dispatcher::Declarative -base;
+
+    sub token_delimiter { '/' }
+
+    under show => sub {
+        on inventory => sub {
+            push @result, "inventory";
+        };
+        on score => sub {
+            push @result, "score";
+        };
+    };
+};
+
+MyDispatcher->run('show/inventory');
+is_deeply([splice @result], ['inventory']);
+
+MyDispatcher->run('show/score');
+is_deeply([splice @result], ['score']);
+
+MyDispatcher->run('show inventory');
+is_deeply([splice @result], []);
+
diff --git a/t/801-cb-chaining.t b/t/801-cb-chaining.t
new file mode 100644
index 0000000..d9b991f
--- /dev/null
+++ b/t/801-cb-chaining.t
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+#use Test::More tests => 11;
+use Test::More; plan qw/no_plan/;
+
+my @result;
+
+do {
+    package MyDispatcher;
+    use Path::Dispatcher::Declarative -base;
+
+    under show => sub {
+        $Path::Dispatcher::Declarative::UNDER_RULE->add_rule(
+            Path::Dispatcher::Rule::Always->new(
+                stage => 'on',
+                block  => sub {
+                    push @result, "Displaying";
+                    next_rule;
+                },
+            ),
+        );
+        on inventory => sub {
+            push @result, "inventory";
+        };
+        on score => sub {
+            push @result, "score";
+        };
+    };
+};
+
+MyDispatcher->run('show inventory');
+is_deeply([splice @result], ['Displaying', 'inventory']);
+
+MyDispatcher->run('show score');
+is_deeply([splice @result], ['Displaying', 'score']);
+
+MyDispatcher->run('show');
+is_deeply([splice @result], ['Displaying']); # This is kinda weird
+
+

commit 9cf0fb9b151916d6ea664d1db30373a67f42c56b
Merge: 46f66f1... 06e9d85...
Author: Shawn M Moore <sartak at gmail.com>
Date:   Tue Feb 24 15:45:30 2009 -0500

    Merge branch 'master' of git://github.com/robertkrimen/path-dispatcher


commit df36c0b25b74253ee3d07e6849a90790ff648416
Author: robertkrimen <robertkrimen at gmail.com>
Date:   Tue Feb 24 12:47:56 2009 -0800

    Don't display 'then' dox

diff --git a/lib/Path/Dispatcher/Declarative.pm b/lib/Path/Dispatcher/Declarative.pm
index b3b4792..03d5fa7 100644
--- a/lib/Path/Dispatcher/Declarative.pm
+++ b/lib/Path/Dispatcher/Declarative.pm
@@ -333,6 +333,8 @@ This is creates a L<Path::Dispatcher::Rule::CodeRef> rule.
 Creates a L<Path::Dispatcher::Rule::Under> rule. The contents of the coderef
 should be nothing other L</on> and C<under> calls.
 
+=cut
+
 #=head2 then sub { }
 
 #Creates a L<Path::Dispatcher::Rule::Always> rule that will continue on to the
@@ -357,5 +359,3 @@ should be nothing other L</on> and C<under> calls.
 #            ...
 #        };
 
-=cut
-

commit 1f9c5def8061271bc131a73e1796cd0ad7ccf9b8
Merge: 9cf0fb9... df36c0b...
Author: Shawn M Moore <sartak at gmail.com>
Date:   Tue Feb 24 15:51:03 2009 -0500

    Merge branch 'master' of git://github.com/robertkrimen/path-dispatcher


commit a658d28a3c6da24bd52ded2ba10dca6f06038bb6
Author: Shawn M Moore <sartak at gmail.com>
Date:   Tue Feb 24 15:53:53 2009 -0500

    Minor fixes

diff --git a/lib/Path/Dispatcher/Declarative.pm b/lib/Path/Dispatcher/Declarative.pm
index 03d5fa7..e09fa8d 100644
--- a/lib/Path/Dispatcher/Declarative.pm
+++ b/lib/Path/Dispatcher/Declarative.pm
@@ -19,11 +19,11 @@ my $exporter = Sub::Exporter::build_exporter({
 sub token_delimiter { ' ' }
 sub case_sensitive_tokens { undef }
 
-sub _next_rule() {
+sub _next_rule () {
     die "Path::Dispatcher next rule\n";
 }
 
-sub _last_rule() {
+sub _last_rule () {
     die "Path::Dispatcher abort rule\n";
 }
 
@@ -96,11 +96,11 @@ sub build_sugar {
         after => sub {
             $into->_add_rule('after_on', @_);
         },
-        then => sub (;&) {
+        then => sub (&) {
             my $block = shift;
             my $rule = Path::Dispatcher::Rule::Always->new(
                 stage => 'on',
-                block  => sub {
+                block => sub {
                     $block->(@_);
                     _next_rule;
                 },

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list