Permalink
Browse files

[URI::Dispatcher] implemented multisplat

the '**' form will capture things, including slashes.
  • Loading branch information...
masak committed Dec 4, 2010
1 parent a14e8f3 commit 06692cfdea3d503a1a9b4ad32b648085dc2af447
Showing with 37 additions and 5 deletions.
  1. +20 −4 lib/URI/Dispatcher.pm
  2. +17 −1 t/uri-dispatcher/03-splat.t
View
@@ -8,7 +8,7 @@ class URI::Dispatcher {
}
sub binds(Str $matcher, Str $url) {
- my $params_regex = / [':' (\w+) || ('*')] /;
+ my $params_regex = / [':' (\w+) || ('**') || ('*')] /;
if $matcher ~~ $params_regex {
my $remainder = $matcher;
@@ -21,10 +21,26 @@ class URI::Dispatcher {
unless $url.substr($index, $/.from) eq $constant_part;
$remainder = $remainder.substr($/.to);
$index += $/.from;
- $url.substr($index) ~~ / <-[/]>+ /;
- my $value = ~$/;
+ my $value;
+ if $key eq '**' {
+ my $next_constant_part = $remainder;
+ if $remainder ~~ $params_regex {
+ $next_constant_part = $remainder.substr(0, $/.from);
+ }
+ # RAKUDO: Wanted to use .rindex here, but got non-isolable
+ # confusing results when doing so.
+ my $next_index = $url.index($next_constant_part, $index);
+ return False
+ unless defined $next_index;
+ $value
+ = $url.substr($index, $next_index - $index);
+ }
+ else {
+ $url.substr($index) ~~ / <-[/]>+ /;
+ $value = ~$/;
+ }
$index += $value.chars;
- if $key eq '*' {
+ if $key eq '*' | '**' {
%names<splat>.push($value);
}
else {
@@ -2,7 +2,7 @@ use v6;
use Test;
-plan 2;
+plan 5;
use URI::Dispatcher;
@@ -16,3 +16,19 @@ use URI::Dispatcher;
ok $d.dispatch('/hello/world'), 'splat works';
is $name, 'world', '...and it saves the parameter';
}
+
+{
+ my $path;
+ my $extension;
+
+ my $d = URI::Dispatcher.new(
+ '/download/**.*' => {
+ $path = .<splat>[0] // 'not set';
+ $extension = .<splat>[1] // 'not set';
+ }
+ );
+
+ ok $d.dispatch('/download/path/to/file.xml'), 'multisplat works';
+ is $path, 'path/to/file', '...and saves the first parameter';
+ is $extension, 'xml', '...and the second';
+}

0 comments on commit 06692cf

Please sign in to comment.