Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
renamed remove method to detach
  • Loading branch information
kraih committed May 3, 2012
1 parent 0fb81d9 commit facfd70
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.93 2012-05-03
- Added remove method to Mojolicious::Routes::Route.
- Added detach method to Mojolicious::Routes::Route.
- Improved 32bit Perl support of Mojo::Transaction::WebSocket.
(mikemagowan, sri)
- Improved documentation browser to have a route name.
Expand Down
34 changes: 17 additions & 17 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -31,7 +31,7 @@ sub new { shift->SUPER::new->parse(@_) }

sub add_child {
my ($self, $route) = @_;
weaken $route->remove->parent($self)->{parent};
weaken $route->detach->parent($self)->{parent};
push @{$self->children}, $route;
return $self;
}
Expand All @@ -42,6 +42,13 @@ sub bridge { shift->route(@_)->inline(1) }

sub delete { shift->_generate_route(DELETE => @_) }

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

sub detour { shift->partial(1)->to(@_) }

sub find {
Expand Down Expand Up @@ -135,13 +142,6 @@ 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 Down Expand Up @@ -397,6 +397,15 @@ L<Mojolicious::Lite> tutorial for more argument variations.
$r->delete('/user')->to('user#remove');
=head2 C<detach>
$r = $r->detach;
Detach route from C<parent>.
# Remove route "perldoc"
$r->find('perldoc')->detach;
=head2 C<detour>
$r = $r->detour(action => 'foo');
Expand Down Expand Up @@ -527,15 +536,6 @@ 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
90 changes: 45 additions & 45 deletions t/mojolicious/routes.t
Expand Up @@ -173,22 +173,22 @@ 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;
# /detached/first
# /detached/second
# /detached/second.xml
# /attached/third
# /attached/third.xml
my $attached = $r->route('/attached')->to('attached#');
my $first = $attached->route(format => 0)->route('/first')->to('#first');
$attached->route('/second')->to('#second');
my $third = $attached->route('/third')->to('#third');
my $detached = $r->detach->route('/detached')->to('detached#');
my $second = $r->find('second');
is $second->render('', {}), '/attached/second', 'right result';
$second->detach;
is $second->render('', {}), '/second', 'right result';
$removed->add_child($first)->add_child($second);
is $second->render('', {}), '/removed/second', 'right result';
$detached->add_child($first)->add_child($second);
is $second->render('', {}), '/detached/second', 'right result';

# Make sure stash stays clean
my $m = Mojolicious::Routes::Match->new(GET => '/clean')->match($r);
Expand Down Expand Up @@ -730,42 +730,42 @@ 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';
# Detached routes
$m = Mojolicious::Routes::Match->new(GET => '/detached/first')->match($r);
is $m->stack->[0]{controller}, 'detached', '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->path_for, '/detached/first', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/detached/first.xml')->match($r);
is $m->stack->[0], undef, 'no value';
$m = Mojolicious::Routes::Match->new(GET => '/source/first')->match($r);
$m = Mojolicious::Routes::Match->new(GET => '/attached/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';
$m = Mojolicious::Routes::Match->new(GET => '/detached/second')->match($r);
is $m->stack->[0]{controller}, 'detached', '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->path_for, '/detached/second', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/detached/second.xml')->match($r);
is $m->stack->[0]{controller}, 'detached', '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->path_for, '/detached/second', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/attached/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';
$m = Mojolicious::Routes::Match->new(GET => '/attached/third')->match($r);
is $m->stack->[0]{controller}, 'attached', '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->path_for, '/attached/third', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/attached/third.xml')->match($r);
is $m->stack->[0]{controller}, 'attached', '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->path_for, '/attached/third', 'right path';
$m = Mojolicious::Routes::Match->new(GET => '/detached/third')->match($r);
is $m->stack->[0], undef, 'no value';

0 comments on commit facfd70

Please sign in to comment.