Skip to content

Commit

Permalink
deprecated waypoint support in Mojolicious
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 16, 2012
1 parent b1492d9 commit 7249100
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 198 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.82 2012-04-16
- Deprecated Mojolicious::Routes::Route->waypoint.
- Deprecated Mojolicious::Routes::Route->block.
- Improved Mojolicious::Routes::Pattern to render formats.
- Improved regex formatting in routes command.
- Improved documentation.
Expand Down
12 changes: 0 additions & 12 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -517,18 +517,6 @@ will be broken, this makes bridges a very powerful tool for authentication.
});
$foo->route('/bar')->to(controller => 'foo', action => 'bar');

=head2 Waypoints

Waypoints are very similar to normal nested routes but can match even if they
have children.

# /foo -> {controller => 'foo', action => 'baz'}
# /foo/bar -> {controller => 'foo', action => 'bar'}
my $foo = $r->waypoint('/foo')->to(controller => 'foo', action => 'baz');
$foo->route('/bar')->to(action => 'bar');

All children will be ignored if a waypoint matches.

=head2 More convenient routes

From the tutorial you should already know L<Mojolicious::Lite> routes, which
Expand Down
5 changes: 0 additions & 5 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -265,11 +265,6 @@ Mojolicious::Routes - Always find your destination with routes
$r->bridge->to(controller => 'foo', action =>'auth')
->route('/blog')->to(action => 'list');
# Waypoints are similar to bridges and nested routes but can also match
# if they are not the actual endpoint of the whole route
my $b = $r->waypoint('/books')->to(controller => 'books', action => 'list');
$b->route('/:id', id => qr/\d+/)->to(action => 'view');
# Simplified Mojolicious::Lite style route generation is also possible
$r->get('/')->to(controller => 'blog', action => 'welcome');
my $blog = $r->under('/blog');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes/Match.pm
Expand Up @@ -72,7 +72,7 @@ sub match {
delete $captures->{app};
}

# Waypoint
# DEPRECATED in Leaf Fluttering In Wind!
return $self->endpoint($r) if $r->block && $empty;

# Endpoint
Expand Down
57 changes: 20 additions & 37 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -2,7 +2,6 @@ package Mojolicious::Routes::Route;
use Mojo::Base -base;

use Carp 'croak';
use List::Util 'first';
use Mojolicious::Routes::Pattern;
use Scalar::Util qw/blessed weaken/;

Expand Down Expand Up @@ -32,16 +31,10 @@ sub new { shift->SUPER::new->parse(@_) }

sub add_child {
my ($self, $route) = @_;

# Connect child
weaken $route->parent($self)->{parent};
push @{$self->children}, $route;

# Inherit format detection from parents
$route->pattern->reqs->{format} //= 0
if defined first { defined $_ && !$_ }
map { $_->pattern->reqs->{format} } @{$self->_stack};

my $format = $self->pattern->reqs->{format};
$route->pattern->reqs->{format} //= 0 if defined $format && !$format;
return $self;
}

Expand Down Expand Up @@ -92,8 +85,15 @@ sub has_websocket {

sub is_endpoint {
my $self = shift;

# Bridge
return if $self->inline;
return $self->block ? 1 : !@{$self->children};

# DEPRECATED in Leaf Fluttering In Wind!
return 1 if $self->block;

# Check number of children
return !@{$self->children};
}

sub is_websocket { shift->{websocket} }
Expand Down Expand Up @@ -149,7 +149,11 @@ sub render {
return $parent ? $parent->render($path, $values) : $path;
}

sub root { shift->_stack->[-1] }
sub root {
my $root = my $parent = shift;
while ($parent = $parent->parent) { $root = $parent }
return $root;
}

sub route {
my $self = shift;
Expand Down Expand Up @@ -224,7 +228,11 @@ sub via {
return $self;
}

sub waypoint { shift->route(@_)->block(1) }
# DEPRECATED in Leaf Fluttering In Wind!
sub waypoint {
warn "Mojolicious::Static->waypoint is DEPRECATED!\n";
shift->route(@_)->block(1);
}

sub websocket {
my $self = shift;
Expand Down Expand Up @@ -272,12 +280,6 @@ sub _generate_route {
->via($methods)->to(\%defaults)->name($name);
}

sub _stack {
my @parents = (shift);
while (my $parent = $parents[-1]->parent) { push @parents, $parent }
return \@parents;
}

1;
__END__
Expand All @@ -300,13 +302,6 @@ L<Mojolicious::Routes>.
L<Mojolicious::Routes::Route> implements the following attributes.
=head2 C<block>
my $block = $r->block;
$r = $r->block(1);
Allow this route to match even if it's not an endpoint, used for waypoints.
=head2 C<children>
my $children = $r->children;
Expand Down Expand Up @@ -592,18 +587,6 @@ restrictions.
$r->route('/foo')->via(qw/GET POST/)->to('foo#bar');
=head2 C<waypoint>
my $waypoint = $r->waypoint;
my $waypoint = $r->waypoint('/:action');
my $waypoint = $r->waypoint('/:action', action => qr/\w+/);
my $waypoint = $r->waypoint(format => 0);
Add a waypoint to this route as nested child.
my $foo = $r->waypoint('/foo')->to('example#foo');
$foo->get('/bar')->to('#bar');
=head2 C<websocket>
my $websocket = $r->websocket('/:foo' => sub {...});
Expand Down
29 changes: 1 addition & 28 deletions t/mojolicious/lite_app.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 703;
use Test::More tests => 685;

# "Wait you're the only friend I have...
# You really want a robot for a friend?
Expand Down Expand Up @@ -122,11 +122,6 @@ get '/data/exception' => 'dies';
# GET /template/exception
get '/template/exception' => 'dies_too';

# GET /waypoint
# GET /waypoint/foo
app->routes->waypoint('/waypoint')->to(text => 'waypoints rule!')
->get('/foo' => {text => 'waypoints work!'});

# GET /with-format
get '/with-format' => {format => 'html'} => 'with-format';

Expand Down Expand Up @@ -717,28 +712,6 @@ $t->get_ok('/template/exception')->status_is(500)
->content_is(
qq/Died at template "dies_too.html.ep" line 2, near "321".\n\n/);

# GET /waypoint
$t->get_ok('/waypoint')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('waypoints rule!');

# GET /waypoint/foo
$t->get_ok('/waypoint/foo')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_is('waypoints work!');

# POST /waypoint/foo
$t->post_ok('/waypoint/foo')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)');

# GET /waypoint/bar
$t->get_ok('/waypoint/bar')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')
->header_is('X-Powered-By' => 'Mojolicious (Perl)');

# GET /with-format
$t->get_ok('/with-format')->content_is("/without-format\n");

Expand Down
118 changes: 5 additions & 113 deletions t/mojolicious/routes.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 413;
use Test::More tests => 360;

# "They're not very heavy, but you don't hear me not complaining."
use Mojolicious::Routes;
Expand Down Expand Up @@ -62,12 +62,6 @@ $test4->route('/bar')->to(controller => 'lalala');
# /test2/baz
$test2->route('/baz')->to('just#works');

# /test3
my $test3 = $r->waypoint('/test3')->to(controller => 's', action => 'l');

# /test3/edit
$test3->route('/edit')->to(action => 'edit');

# /
$r->route('/')->to(controller => 'hello', action => 'world');

Expand Down Expand Up @@ -117,23 +111,9 @@ $r->route('/format6', format => 0)
# /format7.foobar
$r->route('/format7', format => [qw/foo foobar/])->to('perl#rocks');

# /articles
# /articles.html
# /articles/1
# /articles/1.html
# /articles/1/edit
# /articles/1/delete
my $articles = $r->waypoint('/articles')->to(
controller => 'articles',
action => 'index',
format => 'html'
);
my $wp = $articles->waypoint('/:id')->to(
controller => 'articles',
action => 'load',
format => 'html'
);
my $bridge = $wp->bridge->to(
my $bridge = $r->bridge('/articles/:id')->to(
controller => 'articles',
action => 'load',
format => 'html'
Expand Down Expand Up @@ -195,21 +175,9 @@ $multi->route('/bar.baz')->to('works#too', format => 'xml');
# /nodetect
# /nodetect2.txt
# /nodetect2.html
# /nodetect3.xml
# /nodetect3/rly
# /nodetect4
# /nodetect4.txt
# /nodetect4/ya
# /nodetect4.txt/ya
my $inactive = $r->route(format => 0);
$inactive->route('/nodetect')->to('foo#none');
$inactive->route('/nodetect2', format => ['txt', 'html'])->to('bar#hyper');
my $some = $inactive->waypoint('/nodetect3', format => 'xml')->to('baz#some');
$some->route('/rly')->to('#rly');
my $more =
$inactive->waypoint('/nodetect4', format => 'txt')
->to('baz#more', format => 'json');
$more->route('/ya')->to('#ya');

# Make sure stash stays clean
my $m = Mojolicious::Routes::Match->new(GET => '/clean')->match($r);
Expand All @@ -230,7 +198,7 @@ is $r->find('test_edit')->to_string, '/:controller/test/edit',
'right pattern';
is $r->find('articles_delete')->to_string, '/articles/:id/delete',
'right pattern';
is $r->find('rly')->pattern->reqs->{format}, 0, 'right value';
is $r->find('nodetect')->pattern->reqs->{format}, 0, 'right value';

# Null route
$m = Mojolicious::Routes::Match->new(GET => '/0')->match($r);
Expand Down Expand Up @@ -318,19 +286,6 @@ $m =
is @{$m->stack}, 0, 'right number of elements';

# Real world example using most features at once
$m = Mojolicious::Routes::Match->new(GET => '/articles.html')->match($r);
is $m->stack->[0]{controller}, 'articles', 'right value';
is $m->stack->[0]{action}, 'index', 'right value';
is $m->stack->[0]{format}, 'html', 'right value';
is $m->path_for, '/articles', 'right path';
is @{$m->stack}, 1, 'right number of elements';
$m = Mojolicious::Routes::Match->new(GET => '/articles/1.html')->match($r);
is $m->stack->[0]{controller}, 'articles', 'right value';
is $m->stack->[0]{action}, 'load', 'right value';
is $m->stack->[0]{id}, '1', 'right value';
is $m->stack->[0]{format}, 'html', 'right value';
is $m->path_for(format => 'html'), '/articles/1.html', 'right path';
is @{$m->stack}, 1, 'right number of elements';
$m = Mojolicious::Routes::Match->new(GET => '/articles/1/edit')->match($r);
is $m->stack->[1]{controller}, 'articles', 'right value';
is $m->stack->[1]{action}, 'edit', 'right value';
Expand Down Expand Up @@ -421,26 +376,9 @@ is $m->captures->{controller}, 'just', 'right value';
is $m->path_for, '/test2/baz', 'right path';
is @{$m->stack}, 2, 'right number of elements';

# Waypoints
$m = Mojolicious::Routes::Match->new(GET => '/test3')->match($r);
is $m->stack->[0]{controller}, 's', 'right value';
is $m->stack->[0]{action}, 'l', 'right value';
is $m->path_for, '/test3', 'right path';
is @{$m->stack}, 1, 'right number of elements';
$m = Mojolicious::Routes::Match->new(GET => '/test3/')->match($r);
is $m->stack->[0]{controller}, 's', 'right value';
is $m->stack->[0]{action}, 'l', 'right value';
is $m->path_for, '/test3', 'right path';
is @{$m->stack}, 1, 'right number of elements';
$m = Mojolicious::Routes::Match->new(GET => '/test3/edit')->match($r);
is $m->stack->[0]{controller}, 's', 'right value';
is $m->stack->[0]{action}, 'edit', 'right value';
is $m->path_for, '/test3/edit', 'right path';
is @{$m->stack}, 1, 'right number of elements';

# Named path_for
$m = Mojolicious::Routes::Match->new(GET => '/test3')->match($r);
is $m->path_for, '/test3', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/alternatives/test')->match($r);
is $m->path_for, '/alternatives/test', 'right path';
is $m->path_for('test_edit', controller => 'foo'), '/foo/test/edit',
'right path';
is $m->path_for('test_edit', {controller => 'foo'}), '/foo/test/edit',
Expand Down Expand Up @@ -793,49 +731,3 @@ $m = Mojolicious::Routes::Match->new(GET => '/nodetect2')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect2.xml')->match($r);
is $m->stack->[0], undef, 'no value';

# Disabled format detection inheritance with waypoint
$m = Mojolicious::Routes::Match->new(GET => '/nodetect3.xml')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'some', 'right value';
is $m->stack->[0]{format}, 'xml', 'right value';
is $m->stack->[1], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect3')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect3.txt')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect3.xml/rly')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'rly', 'right value';
is $m->stack->[0]{format}, 'xml', 'no value';
is $m->stack->[1], undef, 'no value';
$m =
Mojolicious::Routes::Match->new(GET => '/nodetect3.xml/rly.txt')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'more', 'right value';
is $m->stack->[0]{format}, 'json', 'right value';
is $m->stack->[1], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4.txt')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'more', 'right value';
is $m->stack->[0]{format}, 'txt', 'right value';
is $m->stack->[1], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4.xml')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4/ya')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'ya', 'right value';
is $m->stack->[0]{format}, 'json', 'right value';
is $m->stack->[1], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4.txt/ya')->match($r);
is $m->stack->[0]{controller}, 'baz', 'right value';
is $m->stack->[0]{action}, 'ya', 'right value';
is $m->stack->[0]{format}, 'txt', 'right value';
is $m->stack->[1], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/nodetect4/ya.xml')->match($r);
is $m->stack->[0], undef, 'no value';
$m =
Mojolicious::Routes::Match->new(GET => '/nodetect4.txt/ya.xml')->match($r);
is $m->stack->[0], undef, 'no value';

0 comments on commit 7249100

Please sign in to comment.