Skip to content

Commit

Permalink
added route_for method to Mojolicious::Routes::Match
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 19, 2012
1 parent eae5a00 commit 9311924
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.63 2012-03-19 00:00:00
- Added route_for method to Mojolicious::Routes::Match.
- Improved Mojolicious::Renderer performance.
- Improved documentation.
- Fixed bug that slowed down Mojolicious::Renderer.
Expand Down
26 changes: 12 additions & 14 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -200,17 +200,15 @@ sub render {
$path = $prefix . $path unless $prefix eq '/';

# Make sure there is always a root
$path = '/' if !$path && !$self->parent;
my $parent = $self->parent;
$path = '/' if !$path && !$parent;

# Format
if ((my $format = $values->{format}) && !$self->parent) {
if ((my $format = $values->{format}) && !$parent) {
$path .= ".$format" unless $path =~ m#\.[^/]+$#;
}

# Parent
$path = $self->parent->render($path, $values) if $self->parent;

return $path;
return $parent ? $parent->render($path, $values) : $path;
}

sub route {
Expand Down Expand Up @@ -669,8 +667,8 @@ Add a new shortcut for this route.
=head2 C<any>
my $route = $route->any('/:foo' => sub {...});
my $route = $route->any(['GET', 'POST'] => '/:foo' => sub {...});
my $route = $r->any('/:foo' => sub {...});
my $route = $r->any(['GET', 'POST'] => '/:foo' => sub {...});
Generate route matching any of the listed HTTP request methods or all. See
also the L<Mojolicious::Lite> tutorial for more argument variations.
Expand All @@ -690,7 +688,7 @@ Add a new bridge to this route as a nested child.
=head2 C<delete>
my $route = $route->delete('/:foo' => sub {...});
my $route = $r->delete('/:foo' => sub {...});
Generate route matching only C<DELETE> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
Expand Down Expand Up @@ -720,7 +718,7 @@ Match routes and dispatch.
=head2 C<get>
my $route = $route->get('/:foo' => sub {...});
my $route = $r->get('/:foo' => sub {...});
Generate route matching only C<GET> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
Expand Down Expand Up @@ -772,7 +770,7 @@ the current route.
=head2 C<options>
my $route = $route->options('/:foo' => sub {...});
my $route = $r->options('/:foo' => sub {...});
Generate route matching only C<OPTIONS> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
Expand All @@ -791,21 +789,21 @@ Parse a pattern.
=head2 C<patch>
my $route = $route->patch('/:foo' => sub {...});
my $route = $r->patch('/:foo' => sub {...});
Generate route matching only C<PATCH> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
=head2 C<post>
my $route = $route->post('/:foo' => sub {...});
my $route = $r->post('/:foo' => sub {...});
Generate route matching only C<POST> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
=head2 C<put>
my $route = $route->put('/:foo' => sub {...});
my $route = $r->put('/:foo' => sub {...});
Generate route matching only C<PUT> requests. See also the
L<Mojolicious::Lite> tutorial for more argument variations.
Expand Down
55 changes: 31 additions & 24 deletions lib/Mojolicious/Routes/Match.pm
Expand Up @@ -103,7 +103,7 @@ sub path_for {

# Single argument
my $values = {};
my $name = undef;
my $name;
if (@_ == 1) {

# Hash
Expand All @@ -117,10 +117,7 @@ sub path_for {
elsif (@_ > 1) {

# Odd
if (@_ % 2) {
$name = shift;
$values = {@_};
}
if (@_ % 2) { ($name, $values) = (shift, {@_}) }

# Even
else {
Expand All @@ -141,25 +138,7 @@ sub path_for {
}

# Find endpoint
else {
my @children = ($self->root);
my $candidate;
while (my $child = shift @children) {

# Match
if ($child->name eq $name) {
$candidate = $child;
last if $child->has_custom_name;
}

# Search children too
push @children, @{$child->children};
}
$endpoint = $candidate;

# Nothing
return $name unless $endpoint;
}
else { return $name unless $endpoint = $self->route_for($name) }

# Merge values
my $captures = $self->captures;
Expand All @@ -177,6 +156,27 @@ sub path_for {
return wantarray ? ($path, $endpoint->has_websocket) : $path;
}

sub route_for {
my ($self, $name) = @_;

# Check all children
my @children = (@{$self->root->children});
my $candidate;
while (my $child = shift @children) {

# Match
if ($child->name eq $name) {
$candidate = $child;
return $candidate if $child->has_custom_name;
}

# Search children too
push @children, @{$child->children};
}

return $candidate;
}

1;
__END__
Expand Down Expand Up @@ -271,6 +271,13 @@ Match against a routes tree.
Render matching route with parameters into path.
=head2 C<route_for>
my $foo = $m->route_for('foo');
Find route by name, custom names have precedence over automatically generated
ones.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down

0 comments on commit 9311924

Please sign in to comment.