Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 14, 2013
1 parent b09719d commit e8ece6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
3 changes: 1 addition & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -148,8 +148,7 @@ sub render {
my $self = shift;

# Template may be first argument
my $template = @_ % 2 && !ref $_[0] ? shift : undef;
my $args = {@_};
my ($template, $args) = (@_ % 2 ? shift : undef, {@_});
$args->{template} = $template if $template;
my $maybe = delete $args->{'mojo.maybe'};

Expand Down
46 changes: 17 additions & 29 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -151,51 +151,27 @@ sub route {
sub to {
my $self = shift;

# No argument
my $pattern = $self->pattern;
return $pattern->defaults unless @_;

# Single argument
my ($shortcut, $defaults);
if (@_ == 1) {
$defaults = shift if ref $_[0] eq 'HASH';
$shortcut = shift if $_[0];
}

# Multiple arguments
else {

# Odd
if (@_ % 2) { ($shortcut, $defaults) = (shift, {@_}) }

# Even
else {

# Shortcut and defaults
if (ref $_[1] eq 'HASH') { ($shortcut, $defaults) = (shift, shift) }

# Just defaults
else { $defaults = {@_} }
}
}
my ($shortcut, %defaults) = _defaults(@_);

# Shortcut
if ($shortcut) {

# App
if (ref $shortcut || $shortcut =~ /^[\w:]+$/) {
$defaults->{app} = $shortcut;
$defaults{app} = $shortcut;
}

# Controller and action
elsif ($shortcut =~ /^([\w\-:]+)?\#(\w+)?$/) {
$defaults->{controller} = $1 if defined $1;
$defaults->{action} = $2 if defined $2;
$defaults{controller} = $1 if defined $1;
$defaults{action} = $2 if defined $2;
}
}

# Merge defaults
$pattern->defaults({%{$pattern->defaults}, %$defaults}) if $defaults;
$pattern->defaults({%{$pattern->defaults}, %defaults}) if %defaults;

return $self;
}
Expand Down Expand Up @@ -223,6 +199,18 @@ sub websocket {
return $route;
}

sub _defaults {

# Hash or shortcut (one)
return ref $_[0] eq 'HASH' ? (undef, %{shift()}) : @_ if @_ == 1;

# Shortcut and values (odd)
return shift, @_ if @_ % 2;

# Shortcut and hash or just values (even)
return ref $_[1] eq 'HASH' ? (shift, %{shift()}) : (undef, @_);
}

sub _generate_route {
my ($self, $methods, @args) = @_;

Expand Down

0 comments on commit e8ece6e

Please sign in to comment.