Skip to content
Browse files

[URI::Dispatcher] send along capture info

When matching '/hello/:name' against '/hello/world', the params
hash now gets an entry C<name => 'world'>.
  • Loading branch information...
1 parent 37b042d commit cd4683af9a7c58ed3b7f36f1218ed2bcd37bd5e6 @masak committed
Showing with 8 additions and 4 deletions.
  1. +5 −2 lib/URI/Dispatcher.pm
  2. +3 −2 t/uri-dispatcher/02-named.t
View
7 lib/URI/Dispatcher.pm
@@ -11,7 +11,9 @@ class URI::Dispatcher {
if $matcher ~~ / ':' \w+ / {
my $remainder = $matcher;
my $index = 0;
- while $remainder ~~ / ':' \w+ / {
+ my %names;
+ while $remainder ~~ / ':' (\w+) / {
+ my $key = $0;
my $constant_part = $remainder.substr(0, $/.from);
return False
unless $url.substr($index, $/.from) eq $constant_part;
@@ -20,9 +22,10 @@ class URI::Dispatcher {
$url.substr($index) ~~ / <-[/]>+ /;
my $value = ~$/;
$index += $value.chars;
+ %names{$key} = $value;
}
if $url.substr($index) eq $remainder {
- return { url => $url };
+ return { url => $url, %names };
}
}
elsif $matcher eq $url {
View
5 t/uri-dispatcher/02-named.t
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 1;
+plan 2;
use URI::Dispatcher;
@@ -10,8 +10,9 @@ use URI::Dispatcher;
my $name;
my $d = URI::Dispatcher.new(
- '/hello/:name' => { $name = .<name> }
+ '/hello/:name' => { $name = .<name> // 'not set' }
);
ok $d.dispatch('/hello/world'), 'named parameters work';
+ is $name, 'world', 'the name was captured properly';
}

0 comments on commit cd4683a

Please sign in to comment.
Something went wrong with that request. Please try again.