Skip to content

Commit

Permalink
renamed route_for method to find
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 19, 2012
1 parent f0a3584 commit 6ebea44
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +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.
- Added find method to Mojolicious::Routes.
- Improved Mojolicious::Renderer performance.
- Improved documentation.
- Fixed bug that slowed down Mojolicious::Renderer.
Expand Down
30 changes: 29 additions & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -122,6 +122,27 @@ sub dispatch {
return 1;
}

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

# Check all children
my @children = (@{$self->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;
}

sub get { shift->_generate_route(GET => @_) }

sub has_conditions {
Expand Down Expand Up @@ -716,6 +737,13 @@ application embedding.
Match routes and dispatch.
=head2 C<find>
my $foo = $r->find('foo');
Find child route by name, custom names have precedence over automatically
generated ones.
=head2 C<get>
my $route = $r->get('/:foo' => sub {...});
Expand All @@ -733,7 +761,7 @@ Check if this route contains conditions.
my $success = $r->has_custom_name;
Check if this route has a custom user defined name.
Check if this route has a custom name.
=head2 C<has_websocket>
Expand Down
31 changes: 1 addition & 30 deletions lib/Mojolicious/Routes/Match.pm
Expand Up @@ -138,7 +138,7 @@ sub path_for {
}

# Find endpoint
else { return $name unless $endpoint = $self->route_for($name) }
else { return $name unless $endpoint = $self->root->find($name) }

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

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

# Check all children
return unless my $root = $self->root;
my @children = (@{$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 @@ -272,13 +250,6 @@ 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 6ebea44

Please sign in to comment.