Skip to content

Commit

Permalink
added remove method to Mojolicious::Routes::Route
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 3, 2012
1 parent a557f84 commit 0fb81d9
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 69 deletions.
8 changes: 5 additions & 3 deletions Changes
@@ -1,8 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.93 2012-05-03
- Added remove method to Mojolicious::Routes::Route.
- Improved 32bit Perl support of Mojo::Transaction::WebSocket.
(mikemagowan, sri)
- Improved documentation browser to have a route name.
- Improved documentation.
- Improved tests.
- Fixed bug that prevented helper names from starting with "end". (Akron)
Expand Down Expand Up @@ -485,7 +487,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Removed experimental status from Mojo::IOLoop::Delay.
- Removed defer method from Mojo::IOLoop.
- Improved exception page for development mode.
- Improved syntax highlighting in perldoc browser slightly.
- Improved syntax highlighting in documentation browser slightly.
- Improved Mojo::Base tests.
- Improved documentation.
- Fixed Mojo::Command->app to be an attribute and not a method.
Expand Down Expand Up @@ -1432,7 +1434,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Fixed small Mojo::DOM namespace detection bug. (Akron)
- Fixed small route pattern escaping bug.
- Fixed small reload bug.
- Fixed small perldoc browser bug. (kberov)
- Fixed small documentation browser bug. (kberov)
- Fixed cookbook recipe. (moritz)

1.16 2011-04-15
Expand Down Expand Up @@ -1573,7 +1575,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Deprecated session method in Mojolicious.
- Deprecated handler and helper attributes in Mojolicious::Renderer.
- Added new exception and not_found templates.
- Added POD browser to Mojolicious::Plugin::PodRenderer.
- Added documentation browser to Mojolicious::Plugin::PodRenderer.
- Added EXPERIMENTAL content_for helper.
- Disabled debug log messages for static files.
- Improved Hypnotoad web server to restart workers regularly.
Expand Down
21 changes: 2 additions & 19 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -38,17 +38,14 @@ sub DESTROY {
# Uhhh, dad, Lisa's the one you're not talking to.
# Bart, go to your room."
sub run {
my ($self, $path, $config) = @_;
my ($self, $path) = @_;

# No windows support
_exit('Hypnotoad not available for Windows.') if $^O eq 'MSWin32';

# Application
$ENV{HYPNOTOAD_APP} ||= abs_path $path;

# DEPRECATED in Leaf Fluttering In Wind!
$ENV{HYPNOTOAD_CONFIG} ||= abs_path $config;

# This is a production server
$ENV{MOJO_MODE} ||= 'production';

Expand Down Expand Up @@ -118,22 +115,8 @@ sub run {
sub _config {
my ($self, $app) = @_;

# Load configuration from application
my $c = $app->config('hypnotoad') || {};

# DEPRECATED in Leaf Fluttering In Wind!
if (-r (my $file = $ENV{HYPNOTOAD_CONFIG})) {
warn "Hypnotoad config files are DEPRECATED!\n";
unless ($c = do $file) {
die qq/Can't load config file "$file": $@/ if $@;
die qq/Can't load config file "$file": $!/ unless defined $c;
die qq/Config file "$file" did not return a hash reference.\n/
unless ref $c eq 'HASH';
}
}

# Hypnotoad settings
$self->{config} = $c;
my $c = $self->{config} = $app->config('hypnotoad') || {};
$c->{graceful_timeout} ||= 30;
$c->{heartbeat_interval} ||= 5;
$c->{heartbeat_timeout} ||= 20;
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -39,6 +39,7 @@ sub register {
$app->helper(pod_to_html => sub { shift; b(_pod_to_html(@_)) });

# Perldoc
return if $conf->{no_perldoc};
$app->routes->any(
'/perldoc/*module' => {module => 'Mojolicious/Guides'} => sub {
my $self = shift;
Expand Down Expand Up @@ -111,7 +112,7 @@ sub register {
$self->render(inline => $PERLDOC, title => $title, parts => \@parts);
$self->res->headers->content_type('text/html;charset="UTF-8"');
}
) unless $conf->{no_perldoc};
)->name('perldoc');
}

sub _pod_to_html {
Expand Down
12 changes: 0 additions & 12 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -164,18 +164,6 @@ sub render {
return $output, $c->app->types->type($format) || 'text/plain';
}

# DEPRECATED in Leaf Fluttering In Wind!
sub root {
warn <<EOF;
Mojolicious::Renderer->root is DEPRECATED in favor of
Mojolicious::Renderer->paths!
EOF
my $self = shift;
return $self->paths->[0] unless @_;
$self->paths->[0] = shift;
return $self;
}

sub template_name {
my ($self, $options) = @_;
return unless my $template = $options->{template} || '';
Expand Down
32 changes: 26 additions & 6 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -31,10 +31,8 @@ sub new { shift->SUPER::new->parse(@_) }

sub add_child {
my ($self, $route) = @_;
weaken $route->parent($self)->{parent};
weaken $route->remove->parent($self)->{parent};
push @{$self->children}, $route;
my $format = $self->pattern->reqs->{format};
$route->pattern->reqs->{format} //= 0 if defined $format && !$format;
return $self;
}

Expand Down Expand Up @@ -137,6 +135,13 @@ sub patch { shift->_generate_route(PATCH => @_) }
sub post { shift->_generate_route(POST => @_) }
sub put { shift->_generate_route(PUT => @_) }

sub remove {
my $self = shift;
return $self unless my $parent = $self->parent;
@{$parent->children} = grep { $_ ne $self } @{$parent->children};
return $self->parent(undef);
}

sub render {
my ($self, $path, $values) = @_;

Expand All @@ -156,8 +161,11 @@ sub root {
}

sub route {
my $self = shift;
return $self->add_child($self->new(@_))->children->[-1];
my $self = shift;
my $route = $self->add_child($self->new(@_))->children->[-1];
my $format = $self->pattern->reqs->{format};
$route->pattern->reqs->{format} //= 0 if defined $format && !$format;
return $route;
}

sub to {
Expand Down Expand Up @@ -223,7 +231,7 @@ sub under { shift->_generate_route(under => @_) }
sub via {
my $self = shift;
return $self->{via} unless @_;
my $methods = [map { uc $_ } @{ref $_[0] ? $_[0] : [@_]}];
my $methods = [map uc($_), @{ref $_[0] ? $_[0] : [@_]}];
$self->{via} = $methods if @$methods;
return $self;
}
Expand Down Expand Up @@ -354,6 +362,9 @@ Construct a new L<Mojolicious::Routes::Route> object.
Add a new child to this route.
# Make this route the new parent of route "perldoc"
$r->add_child($r->find('perldoc'));
=head2 C<any>
my $route = $r->any('/:foo' => sub {...});
Expand Down Expand Up @@ -516,6 +527,15 @@ L<Mojolicious::Lite> tutorial for more argument variations.
$r->put('/user')->to('user#replace');
=head2 C<remove>
$r = $r->remove;
Remove route from C<parent>.
# Remove route "perldoc"
$r->find('perldoc')->remove;
=head2 C<render>
my $path = $r->render($suffix);
Expand Down
12 changes: 0 additions & 12 deletions lib/Mojolicious/Static.pm
Expand Up @@ -49,18 +49,6 @@ sub dispatch {
return $c->rendered;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub root {
warn <<EOF;
Mojolicious::Static->root is DEPRECATED in favor of
Mojolicious::Static->paths!
EOF
my $self = shift;
return $self->paths->[0] unless @_;
$self->paths->[0] = shift;
return $self;
}

sub serve {
my ($self, $c, $rel) = @_;

Expand Down
9 changes: 2 additions & 7 deletions script/hypnotoad
Expand Up @@ -20,13 +20,8 @@ Please visit http://mojolicio.us for detailed installation instructions.
EOF

# "Hey sexy mama, wanna kill all humans?"
my $config = 'hypnotoad.conf';
my $help;
GetOptions(

# DEPRECATED in Leaf Fluttering In Wind!
'c|config=s' => sub { $config = $_[1] },

'f|foreground' => sub { $ENV{HYPNOTOAD_FOREGROUND} = 1 },
'h|help' => sub { $help = 1 },
's|stop' => sub { $ENV{HYPNOTOAD_STOP} = 1 },
Expand All @@ -46,12 +41,12 @@ These options are available:
-f, --foreground Keep manager process in foreground.
-h, --help Show this message.
-s, --stop Stop server gracefully.
-t, --test Test application/configuration and exit.
-t, --test Test application and exit.
EOF

# "This is it.
# The moment we should've trained for."
Mojo::Server::Hypnotoad->new->run($app, $config);
Mojo::Server::Hypnotoad->new->run($app);

=head1 NAME
Expand Down
12 changes: 6 additions & 6 deletions t/mojo/user_agent_online.t
Expand Up @@ -34,7 +34,7 @@ my $loop = Mojo::IOLoop->singleton;
my $ua = Mojo::UserAgent->new;
my ($id, $code);
$ua->get(
'http://cpan.org' => sub {
'http://metacpan.org' => sub {
my $tx = pop;
$id = $tx->connection;
$code = $tx->res->code;
Expand Down Expand Up @@ -136,7 +136,7 @@ $ua = Mojo::UserAgent->new;
# Custom non keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse('http://cpan.org');
$tx->req->url->parse('http://metacpan.org');
$tx->req->headers->connection('close');
$ua->start($tx);
ok $tx->is_finished, 'transaction is finished';
Expand All @@ -154,10 +154,10 @@ like $res->body, qr/Mojolicious/, 'right content';
is $res->code, 200, 'right status';

# Simple request
$tx = $ua->get('cpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'http://cpan.org', 'right url';
is $tx->res->code, 301, 'right status';
$tx = $ua->get('metacpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'http://metacpan.org', 'right url';
is $tx->res->code, 301, 'right status';

# HTTPS request that requires SNI
$tx = $ua->get('https://sni.velox.ch/');
Expand Down
3 changes: 2 additions & 1 deletion t/mojolicious/pod_renderer_lite_app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 31;
use Test::More tests => 32;

# "Amy get your pants back on and get to work.
# They think were making out.
Expand All @@ -16,6 +16,7 @@ use Test::Mojo;

# POD renderer plugin
plugin 'PODRenderer';
ok app->routes->find('perldoc'), 'route found';

# Default layout
app->defaults(layout => 'gray');
Expand Down
59 changes: 58 additions & 1 deletion t/mojolicious/routes.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

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

# "They're not very heavy, but you don't hear me not complaining."
use Mojolicious::Routes;
Expand Down Expand Up @@ -173,6 +173,23 @@ my $inactive = $r->route(format => 0);
$inactive->route('/nodetect')->to('foo#none');
$inactive->route('/nodetect2', format => ['txt', 'html'])->to('bar#hyper');

# /removed/first
# /removed/second
# /removed/second.xml
# /source/third
# /source/third.xml
my $source = $r->route('/source')->to('source#');
my $first = $source->route(format => 0)->route('/first')->to('#first');
$source->route('/second')->to('#second');
my $third = $source->route('/third')->to('#third');
my $removed = $r->remove->route('/removed')->to('removed#');
my $second = $r->find('second');
is $second->render('', {}), '/source/second', 'right result';
$second->remove;
is $second->render('', {}), '/second', 'right result';
$removed->add_child($first)->add_child($second);
is $second->render('', {}), '/removed/second', 'right result';

# Make sure stash stays clean
my $m = Mojolicious::Routes::Match->new(GET => '/clean')->match($r);
is $m->stack->[0]{clean}, 1, 'right value';
Expand Down Expand Up @@ -712,3 +729,43 @@ $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';

# Removed routes
$m = Mojolicious::Routes::Match->new(GET => '/removed/first')->match($r);
is $m->stack->[0]{controller}, 'removed', 'right value';
is $m->stack->[0]{action}, 'first', 'right value';
is $m->stack->[0]{format}, undef, 'no value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/removed/first', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/removed/first.xml')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/source/first')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/removed/second')->match($r);
is $m->stack->[0]{controller}, 'removed', 'right value';
is $m->stack->[0]{action}, 'second', 'right value';
is $m->stack->[0]{format}, undef, 'no value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/removed/second', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/removed/second.xml')->match($r);
is $m->stack->[0]{controller}, 'removed', 'right value';
is $m->stack->[0]{action}, 'second', 'right value';
is $m->stack->[0]{format}, 'xml', 'right value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/removed/second', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/source/second')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/source/third')->match($r);
is $m->stack->[0]{controller}, 'source', 'right value';
is $m->stack->[0]{action}, 'third', 'right value';
is $m->stack->[0]{format}, undef, 'no value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/source/third', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/source/third.xml')->match($r);
is $m->stack->[0]{controller}, 'source', 'right value';
is $m->stack->[0]{action}, 'third', 'right value';
is $m->stack->[0]{format}, 'xml', 'right value';
is $m->stack->[1], undef, 'no value';
is $m->path_for, '/source/third', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/removed/third')->match($r);
is $m->stack->[0], undef, 'no value';
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -9,7 +9,7 @@ plan skip_all => 'set TEST_POD to enable this test (developer only!)'
# DEPRECATED in Leaf Fluttering In Wind!
my @leaf = (
qw/block controller_base_class default_static_class default_template_class/,
qw/drop dictionary max_redirects root waypoint/
qw/drop waypoint/
);

# "Marge, I'm going to miss you so much. And it's not just the sex.
Expand Down

0 comments on commit 0fb81d9

Please sign in to comment.