[Bps-public-commit] Path-Dispatcher branch, pass-match, updated. 78eb561817c71070f283f7b2295f2ab33b35f951

Shawn Moore sartak at bestpractical.com
Sat Aug 21 20:56:30 EDT 2010


The branch, pass-match has been updated
       via  78eb561817c71070f283f7b2295f2ab33b35f951 (commit)
       via  114946c9332ce8d183959b25746a5530a6096ca5 (commit)
       via  3f2cea6da2460738b90b5705ffacbc78f304c0d2 (commit)
       via  6cd4c3b588e89bab787f12fc89b7826c4cecf429 (commit)
      from  fab0d44046ec5f9534eed1dd2c0a4f75fb02788d (commit)

Summary of changes:
 lib/Path/Dispatcher/Match.pm |   43 +++++++++++++++++++++++++++--------------
 t/100-match-object.t         |   31 ++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 15 deletions(-)
 create mode 100644 t/100-match-object.t

- Log -----------------------------------------------------------------
commit 6cd4c3b588e89bab787f12fc89b7826c4cecf429
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 20:44:16 2010 -0400

    Add a ->pos method to Match

diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index c5fe2fa..7bc79fe 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -34,6 +34,13 @@ sub run {
     return $self->rule->run($self, @_);
 }
 
+sub pos {
+    my $self = shift;
+    my $index = shift;
+
+    return $self->positional_captures->[$index];
+}
+
 __PACKAGE__->meta->make_immutable;
 no Any::Moose;
 

commit 3f2cea6da2460738b90b5705ffacbc78f304c0d2
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 20:44:31 2010 -0400

    Match documentation update

diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 7bc79fe..75db0aa 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -56,18 +56,23 @@ Path::Dispatcher::Match - the result of a successful rule match
 
     my $rule = Path::Dispatcher::Rule::Tokens->new(
         tokens => [ 'attack', qr/^\w+$/ ],
-        block  => sub { attack($2) },
+        block  => sub {
+            my $match = shift;
+            attack($match->pos(2))
+        },
     );
 
     my $match = $rule->match("attack dragon");
 
-    $match->path            # "attack dragon"
-    $match->leftover        # empty string (populated with prefix rules)
-    $match->rule            # $rule
-    $match->result          # ["attack", "dragon"] (decided by the rule)
+    # introspection
+    $match->path                # "attack dragon"
+    $match->leftover            # empty string (populated with prefix rules)
+    $match->rule                # $rule
+    $match->positional_captures # ["attack", "dragon"] (decided by the rule)
+    $match->pos(1)              # "attack"
+    $match->pos(2)              # "dragon"
 
-    $match->run                         # causes the player to attack the dragon
-    $match->run_with_number_vars($code) # runs $code with $1=attack $2=dragon
+    $match->run                 # causes the player to attack the dragon
 
 =head1 DESCRIPTION
 
@@ -89,9 +94,10 @@ The path that the rule matched.
 The rest of the path. This is populated when the rule matches a prefix of the
 path.
 
-=head2 result
+=head2 positional_captures
 
-Arbitrary results generated by the rule. For example, L<Path::Dispatcher::Rule::Regex> rules' result is an array reference of capture variables.
+Any positional captures generated by the rule. For example,
+L<Path::Dispatcher::Rule::Regex> populates this with the capture variables.
 
 =head1 METHODS
 
@@ -99,13 +105,9 @@ Arbitrary results generated by the rule. For example, L<Path::Dispatcher::Rule::
 
 Executes the rule's codeblock with the same arguments.
 
-=head2 run_with_number_vars coderef, $1, $2, ...
+=head2 pos($i)
 
-Populates the number variables C<$1>, C<$2>, ... then executes the coderef.
-
-Unfortunately, the only way to achieve this (pre-5.10 anyway) is to match a
-regular expression. Both a string and a regex are constructed such that
-matching will produce the correct capture variables.
+Returns the C<$i>th positional capture.
 
 =cut
 

commit 114946c9332ce8d183959b25746a5530a6096ca5
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 20:58:26 2010 -0400

    Make pos 1-indexed like $1 $2..

diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 75db0aa..fea532d 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -38,6 +38,10 @@ sub pos {
     my $self = shift;
     my $index = shift;
 
+    return undef if $index == 0;
+
+    $index-- if $index > 0;
+
     return $self->positional_captures->[$index];
 }
 
@@ -107,7 +111,7 @@ Executes the rule's codeblock with the same arguments.
 
 =head2 pos($i)
 
-Returns the C<$i>th positional capture.
+Returns the C<$i>th positional capture, 1-indexed.
 
 =cut
 

commit 78eb561817c71070f283f7b2295f2ab33b35f951
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Sat Aug 21 20:58:41 2010 -0400

    Add tests for the match object itself

diff --git a/t/100-match-object.t b/t/100-match-object.t
new file mode 100644
index 0000000..d5a98a2
--- /dev/null
+++ b/t/100-match-object.t
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 8;
+use Path::Dispatcher;
+
+my $match;
+
+my $dispatcher = Path::Dispatcher->new(
+    rules => [
+        Path::Dispatcher::Rule::Regex->new(
+            regex => qr/^(\w+) (Q)(Q) (\w+)$/,
+            block => sub {
+                $match = shift;
+            },
+        ),
+    ],
+);
+
+$dispatcher->run("chewy QQ cute");
+is_deeply($match->positional_captures, ["chewy", "Q", "Q", "cute"]);
+is_deeply($match->pos(1), "chewy");
+is_deeply($match->pos(2), "Q");
+is_deeply($match->pos(3), "Q");
+is_deeply($match->pos(4), "cute");
+
+is_deeply($match->pos(0), undef);
+is_deeply($match->pos(5), undef);
+
+is_deeply($match->pos(-1), "cute");
+

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



More information about the Bps-public-commit mailing list