Skip to content

Commit

Permalink
Add insert_route
Browse files Browse the repository at this point in the history
Signed-off-by: Stevan Little <stevan.little@iinteractive.com>
  • Loading branch information
lestrrat authored and Stevan Little committed Mar 11, 2009
1 parent 6142e0e commit 80fccf3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
47 changes: 47 additions & 0 deletions lib/Path/Router.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ sub add_route {
);
}

sub insert_route {
my ($self, $path, %options) = @_;
my $at = delete $options{at} || 0;

my $route = Path::Router::Route->new(
path => $path,
%options
);
my $routes = $self->routes;

if (! $at) {
unshift @$routes, $route;
} elsif ($#{$routes} < $at) {
push @$routes, $route;
} else {
splice @$routes, $at, 0, $route;
}
}

sub match {
my ($self, $url) = @_;

Expand Down Expand Up @@ -260,6 +279,34 @@ It is in Perl :)
=item B<add_route ($path, ?%options)>
Adds a new route to the I<end> of the routes list.
=item B<insert_route ($path, %options)>
Adds a new route to the routes list. You may specify an C<at> parameter, which would indicate the position where you want to insert your newly created route. The C<at> parameter is the C<index> position in the list, so it starts at 0.
Examples:
# You have more than three paths, insert a new route at
# the 4th item
$router->insert_route($path => (
at => 3,
%options
) );
# If you have less items than the index, then it's the same as
# as add_route -- it's just appended to the end of the list
$router->insert_route($path => (
at => 1_000_000,
%options
) );
# If you want to prepend, omit "at", or specify 0
$router->insert_Route($path => (
at => 0,
%optiosn
) );
=item B<routes>
=item B<match ($path)>
Expand Down
32 changes: 22 additions & 10 deletions t/001_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 14;
use Test::More tests => 17;
use Test::Path::Router;

use Moose::Util::TypeConstraints;
Expand All @@ -25,13 +25,6 @@ can_ok($router, 'uri_for');

# create some routes

$router->add_route('blog' => (
defaults => {
controller => 'blog',
action => 'index',
}
));

$router->add_route('blog/:year/:month/:day' => (
defaults => {
controller => 'blog',
Expand All @@ -44,7 +37,18 @@ $router->add_route('blog/:year/:month/:day' => (
}
));

$router->add_route('blog/:action/:id' => (
# This used to be added at the very beginning, but we're putting it here
# to test insert_route
$router->insert_route('blog' => (
defaults => {
controller => 'blog',
action => 'index',
}
));

# This used to be added as the second argument, but we're... see above.
$router->insert_route( 'blog/:action/:id' => (
at => 2,
defaults => {
controller => 'blog',
},
Expand All @@ -54,16 +58,24 @@ $router->add_route('blog/:action/:id' => (
}
));

$router->add_route('test/?:x/?:y' => (
# This used to be added as the last argument, but we're... see above.
$router->insert_route('test/?:x/?:y' => (
at => 1_000_000,
defaults => {
controller => 'test',
x => 'x',
y => 'y',
},
));


# create some tests

# check to make sure that "blog" is at the front
is( $router->routes->[0]->path, 'blog', "first route is 'blog'");
is( $router->routes->[2]->path, 'blog/:action/:id', "3rd route is 'blog/:action/:id'");
is( $router->routes->[3]->path, 'test/?:x/?:y', "4th route is 'test/?:x/?:y'");

routes_ok($router, {
# blog
'blog' => {
Expand Down

0 comments on commit 80fccf3

Please sign in to comment.