Skip to content

Commit

Permalink
a little more code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 27, 2014
1 parent db9c7a5 commit 0c24ec2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
17 changes: 4 additions & 13 deletions lib/Mojolicious/Routes/Match.pm
@@ -1,14 +1,17 @@
package Mojolicious::Routes::Match;
use Mojo::Base -base;

use Mojolicious::Routes::Route;

has current => 0;
has [qw(endpoint root)];
has stack => sub { [] };

sub match { $_[0]->_match($_[0]->root, $_[1], $_[2]) }

sub path_for {
my ($self, $name, %values) = (shift, _values(@_));
my ($self, $name, %values)
= (shift, Mojolicious::Routes::Route::_values(@_));

# Current route
my $endpoint;
Expand Down Expand Up @@ -93,18 +96,6 @@ sub _match {
}
}
sub _values {
# Hash or name (one)
return ref $_[0] eq 'HASH' ? (undef, %{shift()}) : @_ if @_ == 1;
# Name and values (odd)
return shift, @_ if @_ % 2;
# Name and hash or just values (even)
return ref $_[1] eq 'HASH' ? (shift, %{shift()}) : (undef, @_);
}
1;
=encoding utf8
Expand Down
26 changes: 13 additions & 13 deletions lib/Mojolicious/Routes/Route.pm
Expand Up @@ -150,7 +150,7 @@ sub to {

my $pattern = $self->pattern;
return $pattern->defaults unless @_;
my ($shortcut, %defaults) = _defaults(@_);
my ($shortcut, %defaults) = _values(@_);

if ($shortcut) {

Expand Down Expand Up @@ -194,18 +194,6 @@ 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 Expand Up @@ -241,6 +229,18 @@ sub _generate_route {
return defined $name ? $route->name($name) : $route;
}

sub _values {

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

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

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

1;

=encoding utf8
Expand Down

0 comments on commit 0c24ec2

Please sign in to comment.