Skip to content

Commit

Permalink
added root method to Mojolicious::Routes::Route and deprecated dictio…
Browse files Browse the repository at this point in the history
…nary in favor of conditions
  • Loading branch information
kraih committed Mar 20, 2012
1 parent 45232f4 commit fae16fa
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,8 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.63 2012-03-20 00:00:00
- Deprecated Mojolicious::Routes::Route->dictionary in favor of
Mojolicious::Routes::Route->conditions.
- Added Mojolicious::Routes::Route.
- Added find method to Mojolicious::Routes::Route.
- Added find and root methods to Mojolicious::Routes::Route.
- Improved form_for helper to automatically add method="POST"
attributes when necessary.
- Improved Mojolicious::Renderer performance.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Routes/Match.pm
Expand Up @@ -44,11 +44,11 @@ sub match {
}

# Conditions
my $over = $r->over || [];
my $conditions = $r->conditions;
my $dictionary = $r->dictionary;
for (my $i = 0; $i < @$conditions; $i += 2) {
return unless my $condition = $dictionary->{$conditions->[$i]};
return if !$condition->($r, $c, $captures, $conditions->[$i + 1]);
for (my $i = 0; $i < @$over; $i += 2) {
return unless my $condition = $conditions->{$over->[$i]};
return if !$condition->($r, $c, $captures, $over->[$i + 1]);
}

# WebSocket
Expand Down
56 changes: 35 additions & 21 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -6,8 +6,8 @@ use Mojolicious::Routes::Pattern;
use Scalar::Util qw/blessed weaken/;

has [qw/block inline parent partial/];
has [qw/children conditions/] => sub { [] };
has [qw/dictionary shortcuts/] => sub { {} };
has 'children' => sub { [] };
has [qw/conditions shortcuts/] => sub { {} };
has pattern => sub { Mojolicious::Routes::Pattern->new };

# "Yet thanks to my trusty safety sphere,
Expand All @@ -33,14 +33,14 @@ sub new { shift->SUPER::new->parse(@_) }
sub add_child {
my ($self, $route) = @_;
weaken $route->parent($self)->{parent};
$route->dictionary($self->dictionary)->shortcuts($self->shortcuts);
$route->conditions($self->conditions)->shortcuts($self->shortcuts);
push @{$self->children}, $route;
return $self;
}

sub add_condition {
my ($self, $name, $cb) = @_;
$self->dictionary->{$name} = $cb;
$self->conditions->{$name} = $cb;
return $self;
}

Expand All @@ -58,6 +58,15 @@ sub delete { shift->_generate_route(DELETE => @_) }

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

# DEPRECATED in Leaf Fluttering In Wind!
sub dictionary {
warn <<EOF;
Mojolicious::Routes::Route->dictionary is DEPRECATED in favor of
Mojolicious::Routes::Route->conditions!
EOF
return shift->conditions(@_);
}

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

Expand All @@ -83,7 +92,7 @@ sub get { shift->_generate_route(GET => @_) }

sub has_conditions {
my $self = shift;
return 1 if @{$self->conditions};
return 1 if @{$self->over || []};
return unless my $parent = $self->parent;
return $parent->has_conditions;
}
Expand Down Expand Up @@ -123,14 +132,13 @@ sub options { shift->_generate_route(OPTIONS => @_) }

sub over {
my $self = shift;
my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
return $self unless @$conditions;

# Routes with conditions can't be cached
push @{$self->conditions}, @$conditions;
my $root = my $parent = $self;
while ($parent = $parent->parent) { $root = $parent }
$root->cache(0);
return $self->{over} unless @_;
my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
return $self unless @$conditions;
$self->{over} = $conditions;
$self->root->cache(0);

return $self;
}
Expand Down Expand Up @@ -166,6 +174,12 @@ sub render {
return $parent ? $parent->render($path, $values) : $path;
}

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

sub route {
my $self = shift;
my $route = $self->new(@_);
Expand Down Expand Up @@ -335,15 +349,8 @@ The children of this routes object, used for nesting routes.
=head2 C<conditions>
my $conditions = $r->conditions;
$r = $r->conditions([foo => qr/\w+/]);
Contains condition parameters for this route, used for C<over>.
=head2 C<dictionary>
my $dictionary = $r->dictionary;
$r = $r->dictionary({foo => sub {...}});
my $conditions = $r->conditions;
$r = $r->conditions({foo => sub {...}});
Contains all available conditions for this route.
Expand Down Expand Up @@ -513,7 +520,8 @@ L<Mojolicious::Lite> tutorial for more argument variations.
=head2 C<over>
$r = $r->over(foo => qr/\w+/);
my $conditions = $r->over;
$r = $r->over(foo => qr/\w+/);
Apply condition parameters to this route and disable routing cache.
Expand Down Expand Up @@ -551,6 +559,12 @@ L<Mojolicious::Lite> tutorial for more argument variations.
Render route with parameters into a path.
=head2 C<root>
my $root = $r->root;
The root of the routes tree.
=head2 C<route>
my $route = $r->route('/:c/:a', a => qr/\w+/);
Expand Down
6 changes: 5 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'development';
}

use Test::More tests => 282;
use Test::More tests => 284;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand All @@ -31,6 +31,10 @@ is $t->app->routes->find('test3')->pattern->defaults->{namespace},
is $t->app->routes->find('authenticated')->pattern->defaults->{controller},
'foo',
'right controller';
is ref $t->app->routes->find('something'), 'Mojolicious::Routes::Route',
'right class';
is ref $t->app->routes->find('something')->root, 'Mojolicious::Routes',
'right class';
is $t->app->sessions->cookie_domain, '.example.com', 'right domain';
is $t->app->sessions->cookie_path, '/bar', 'right path';

Expand Down
6 changes: 5 additions & 1 deletion t/mojolicious/production_app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_MODE} = 'production';
}

use Test::More tests => 67;
use Test::More tests => 69;

use FindBin;
use lib "$FindBin::Bin/lib";
Expand All @@ -26,6 +26,10 @@ is $t->app->routes->find('test3')->pattern->defaults->{namespace},
is $t->app->routes->find('authenticated')->pattern->defaults->{controller},
'foo',
'right controller';
is ref $t->app->routes->find('something'), 'Mojolicious::Routes::Route',
'right class';
is ref $t->app->routes->find('something')->root, 'Mojolicious::Routes',
'right class';
is $t->app->sessions->cookie_domain, '.example.com', 'right domain';
is $t->app->sessions->cookie_path, '/bar', 'right path';

Expand Down
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/default_static_class default_template_class max_redirects/,
qw/default_static_class default_template_class dictionary max_redirects/,
qw/prepare_ioloop root unsubscribe x_forwarded_for/
);

Expand Down

0 comments on commit fae16fa

Please sign in to comment.