Skip to content

Commit

Permalink
improved memory usage of Mojolicious::Routes by moving conditions and…
Browse files Browse the repository at this point in the history
… shortcuts back
  • Loading branch information
kraih committed Mar 20, 2012
1 parent 65c3355 commit c80c51a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 58 deletions.
3 changes: 2 additions & 1 deletion Changes
Expand Up @@ -2,9 +2,10 @@ This file documents the revision history for Perl extension Mojolicious.

2.63 2012-03-20 00:00:00
- 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 memory usage of Mojolicious::Routes.
- Improved Mojolicious::Renderer performance.
- Improved documentation.
- Improved tests.
Expand Down
46 changes: 43 additions & 3 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -9,9 +9,22 @@ use Scalar::Util 'weaken';

has 'namespace';
has cache => sub { Mojo::Cache->new };
has [qw/conditions shortcuts/] => sub { {} };
has controller_base_class => 'Mojolicious::Controller';
has hidden => sub { [qw/new attr has/] };

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

sub add_shortcut {
my ($self, $name, $cb) = @_;
$self->shortcuts->{$name} = $cb;
return $self;
}

# "Hey. What kind of party is this? There's no booze and only one hooker."
sub auto_render {
my ($self, $c) = @_;
Expand Down Expand Up @@ -287,6 +300,13 @@ L<Mojolicious::Routes::Route> and implements the following new ones.
Routing cache, defaults to a L<Mojo::Cache> object.
=head2 C<conditions>
my $conditions = $r->conditions;
$r = $r->conditions({foo => sub {...}});
Contains all available conditions.
=head2 C<controller_base_class>
my $base = $r->controller_base_class;
Expand All @@ -300,7 +320,8 @@ L<Mojolicious::Controller>.
my $hidden = $r->hidden;
$r = $r->hidden([qw/new attr tx render req res stash/]);
Controller methods and attributes that are hidden from routes.
Controller methods and attributes that are hidden from routes, defaults to
C<new>, C<attr> and C<has>.
=head2 C<namespace>
Expand All @@ -309,11 +330,30 @@ Controller methods and attributes that are hidden from routes.
Namespace used by C<dispatch> to search for controllers.
=head2 C<shortcuts>
my $shortcuts = $r->shortcuts;
$r = $r->shortcuts({foo => sub {...}});
Contains all available shortcuts.
=head1 METHODS
L<Mojolicious::Routes> inherits all methods from
L<Mojolicious::Routes::Route> and implements the following ones.
=head2 C<add_condition>
$r = $r->add_condition(foo => sub {...});
Add a new condition.
=head2 C<add_shortcut>
$r = $r->add_shortcut(foo => sub {...});
Add a new shortcut.
=head2 C<auto_render>
$r->auto_render(Mojolicious::Controller->new);
Expand All @@ -324,7 +364,7 @@ Automatic rendering.
my $success = $r->dispatch(Mojolicious::Controller->new);
Match routes and dispatch.
Match routes with L<Mojolicious::Routes::Match> and dispatch.
=head2 C<hide>
Expand All @@ -336,7 +376,7 @@ Hide controller method or attribute from routes.
my $route = $r->route('/:c/:a', a => qr/\w+/);
Add a new nested L<Mojolicious::Routes::Route> child to this route.
Add a new nested L<Mojolicious::Routes::Route> child.
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes/Match.pm
Expand Up @@ -45,7 +45,7 @@ sub match {

# Conditions
my $over = $r->over || [];
my $conditions = $r->conditions;
my $conditions = $self->{conditions} ||= $r->root->conditions;
for (my $i = 0; $i < @$over; $i += 2) {
return unless my $condition = $conditions->{$over->[$i]};
return if !$condition->($r, $c, $captures, $over->[$i + 1]);
Expand Down
72 changes: 21 additions & 51 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -7,8 +7,7 @@ use Scalar::Util qw/blessed weaken/;

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

# "Yet thanks to my trusty safety sphere,
# I sublibed with only tribial brain dablage."
Expand All @@ -22,7 +21,7 @@ sub AUTOLOAD {

# Call shortcut
croak qq/Can't locate object method "$method" via package "$package"/
unless my $shortcut = $self->shortcuts->{$method};
unless my $shortcut = $self->root->shortcuts->{$method};
return $self->$shortcut(@_);
}

Expand All @@ -33,23 +32,10 @@ sub new { shift->SUPER::new->parse(@_) }
sub add_child {
my ($self, $route) = @_;
weaken $route->parent($self)->{parent};
$route->conditions($self->conditions)->shortcuts($self->shortcuts);
push @{$self->children}, $route;
return $self;
}

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

sub add_shortcut {
my ($self, $name, $cb) = @_;
$self->shortcuts->{$name} = $cb;
return $self;
}

sub any { shift->_generate_route(ref $_[0] eq 'ARRAY' ? shift : [], @_) }

sub bridge { shift->route(@_)->inline(1) }
Expand Down Expand Up @@ -129,9 +115,7 @@ sub over {
my $conditions = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
return $self unless @$conditions;
$self->{over} = $conditions;
my $root = my $parent = $self;
while ($parent = $parent->parent) { $root = $parent }
$root->cache(0);
$self->root->cache(0);

return $self;
}
Expand Down Expand Up @@ -167,6 +151,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 @@ -334,13 +324,6 @@ Allow this route to match even if it's not an endpoint, used for waypoints.
The children of this routes object, used for nesting routes.
=head2 C<conditions>
my $conditions = $r->conditions;
$r = $r->conditions({foo => sub {...}});
Contains all available conditions for this route.
=head2 C<inline>
my $inline = $r->inline;
Expand Down Expand Up @@ -369,13 +352,6 @@ Route has no specific end, remaining characters will be captured in C<path>.
Pattern for this route, defaults to a L<Mojolicious::Routes::Pattern> object.
=head2 C<shortcuts>
my $shortcuts = $r->shortcuts;
$r = $r->shortcuts({foo => sub {...}});
Contains all additional route shortcuts available for this route.
=head1 METHODS
L<Mojolicious::Routes::Route> inherits all methods from L<Mojo::Base> and
Expand All @@ -386,26 +362,14 @@ implements the following ones.
my $r = Mojolicious::Routes::Route->new;
my $r = Mojolicious::Routes::Route->new('/:controller/:action');
Construct a new route object.
Construct a new L<Mojolicious::Routes::Route> object.
=head2 C<add_child>
$r = $r->add_child(Mojolicious::Route->new);
Add a new child to this route.
=head2 C<add_condition>
$r = $r->add_condition(foo => sub {...});
Add a new condition for this route.
=head2 C<add_shortcut>
$r = $r->add_shortcut(foo => sub {...});
Add a new shortcut for this route.
=head2 C<any>
my $route = $r->any('/:foo' => sub {...});
Expand Down Expand Up @@ -463,7 +427,7 @@ L<Mojolicious::Lite> tutorial for more argument variations.
my $success = $r->has_conditions;
Check if this route contains conditions.
Check if this route has active conditions.
=head2 C<has_custom_name>
Expand Down Expand Up @@ -507,10 +471,10 @@ L<Mojolicious::Lite> tutorial for more argument variations.
=head2 C<over>
my $conditions = $r->over;
$r = $r->over(foo => qr/\w+/);
my $over = $r->over;
$r = $r->over(foo => qr/\w+/);
Apply condition parameters to this route and disable routing cache.
Activate conditions for this route and disable routing cache.
=head2 C<parse>
Expand Down Expand Up @@ -546,6 +510,12 @@ L<Mojolicious::Lite> tutorial for more argument variations.
Render route with parameters into a path.
=head2 C<root>
my $root = $r->root;
The L<Mojolicious::Routes> object this route is an ancestor of.
=head2 C<route>
my $route = $r->route('/:c/:a', a => qr/\w+/);
Expand Down Expand Up @@ -573,7 +543,7 @@ Set default parameters for this route.
my $string = $r->to_string;
Stringifies the whole route.
Stringify the whole route.
=head2 C<under>
Expand Down
3 changes: 2 additions & 1 deletion t/mojolicious/app.t
Expand Up @@ -33,7 +33,8 @@ is $t->app->routes->find('authenticated')->pattern->defaults->{controller},
'right controller';
is ref $t->app->routes->find('something'), 'Mojolicious::Routes::Route',
'right class';
is ref $t->app->routes, 'Mojolicious::Routes', '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
3 changes: 2 additions & 1 deletion t/mojolicious/production_app.t
Expand Up @@ -28,7 +28,8 @@ is $t->app->routes->find('authenticated')->pattern->defaults->{controller},
'right controller';
is ref $t->app->routes->find('something'), 'Mojolicious::Routes::Route',
'right class';
is ref $t->app->routes, 'Mojolicious::Routes', '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

0 comments on commit c80c51a

Please sign in to comment.