[Bps-public-commit] Path-Dispatcher branch, complete, updated. d9822a3ca5065679dab760f0fedb686d4996e2e4

sartak at bestpractical.com sartak at bestpractical.com
Fri Dec 18 18:20:49 EST 2009


The branch, complete has been updated
       via  d9822a3ca5065679dab760f0fedb686d4996e2e4 (commit)
      from  b030e4d46e9ec28153a6c928585c94227eeda2a2 (commit)

Summary of changes:
 lib/Path/Dispatcher/Rule/Tokens.pm |   35 ++++++++++++++++++++++++++---------
 1 files changed, 26 insertions(+), 9 deletions(-)

- Log -----------------------------------------------------------------
commit d9822a3ca5065679dab760f0fedb686d4996e2e4
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Dec 18 18:20:38 2009 -0500

    Refactor Token matching into something with a callback

diff --git a/lib/Path/Dispatcher/Rule/Tokens.pm b/lib/Path/Dispatcher/Rule/Tokens.pm
index e9c6059..34c53ba 100644
--- a/lib/Path/Dispatcher/Rule/Tokens.pm
+++ b/lib/Path/Dispatcher/Rule/Tokens.pm
@@ -60,28 +60,45 @@ sub _match {
     return \@matched, $leftover;
 }
 
-sub _match_token {
+sub _each_token {
     my $self     = shift;
     my $got      = shift;
     my $expected = shift;
+    my $callback = shift;
 
-    if (!ref($expected)) {
-        ($got, $expected) = (lc $got, lc $expected) if !$self->case_sensitive;
-        return $got eq $expected;
-    }
-    elsif (ref($expected) eq 'ARRAY') {
+    if (ref($expected) eq 'ARRAY') {
         for my $alternative (@$expected) {
-            return 1 if $self->_match_token($got, $alternative);
+            $self->_each_token($got, $alternative, $callback);
         }
     }
-    elsif (ref($expected) eq 'Regexp') {
-        return $got =~ $expected;
+    elsif (!ref($expected) || ref($expected) eq 'Regexp') {
+        $callback->($got, $expected);
     }
     else {
         die "Unexpected token '$expected'"; # the irony is not lost on me :)
     }
 }
 
+sub _match_token {
+    my $self     = shift;
+    my $got      = shift;
+    my $expected = shift;
+
+    my $matched = 0;
+    $self->_each_token($got, $expected, sub {
+        my ($g, $e) = @_;
+        if (!ref($e)) {
+            ($g, $e) = (lc $g, lc $e) if !$self->case_sensitive;
+            $matched ||= $g eq $e;
+        }
+        elsif (ref($e) eq 'Regexp') {
+            $matched ||= $g =~ $e;
+        }
+    });
+
+    return $matched;
+}
+
 sub tokenize {
     my $self = shift;
     my $path = shift;

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



More information about the Bps-public-commit mailing list