Skip to content

Commit

Permalink
renamed shape_match method in Mojolicious::Routes::Pattern to match_p…
Browse files Browse the repository at this point in the history
…artial
  • Loading branch information
kraih committed May 11, 2013
1 parent ab9195e commit ec14f4a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -22,6 +22,8 @@
- Removed deprecated post_form_ok and post_json_ok methods from Test::Mojo.
- Removed deprecated f and n functions from ojo.
- Removed deprecated after_static_dispatch hook.
- Renamed shape_match method in Mojolicious::Routes::Pattern to
match_partial.
- Reduced idle CPU usage of Mojo::IOLoop.
- Increased default lock_timeout from 0.5 to 1 second in
Mojo::Server::Prefork and Mojo::Server::Hypnotoad.
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Routes/Match.pm
Expand Up @@ -19,7 +19,8 @@ sub match {
# Pattern
my $path = $self->{path};
my $pattern = $r->pattern;
return unless my $captures = $pattern->shape_match(\$path, $r->is_endpoint);
return
unless my $captures = $pattern->match_partial(\$path, $r->is_endpoint);
local $self->{path} = $path;
$captures = {%{$self->captures}, %$captures};

Expand Down
76 changes: 38 additions & 38 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -14,10 +14,39 @@ sub new { shift->SUPER::new->parse(@_) }

sub match {
my ($self, $path, $detect) = @_;
my $result = $self->shape_match(\$path, $detect);
my $result = $self->match_partial(\$path, $detect);
return !$path || $path eq '/' ? $result : undef;
}

sub match_partial {
my ($self, $pathref, $detect) = @_;

# Compile on demand
my $regex = $self->regex || $self->_compile;
my $format
= $detect ? ($self->format_regex || $self->_compile_format) : undef;

# Match
return undef unless my @captures = $$pathref =~ $regex;
$$pathref =~ s/$regex//;

# Merge captures
my $result = {%{$self->defaults}};
for my $placeholder (@{$self->placeholders}) {
last unless @captures;
my $capture = shift @captures;
$result->{$placeholder} = $capture if defined $capture;
}

# Format
my $constraint = $self->constraints->{format};
return $result if !$detect || defined $constraint && !$constraint;
if ($$pathref =~ s!^/?$format!!) { $result->{format} = $1 }
elsif ($constraint) { return undef unless $result->{format} }

return $result;
}

sub parse {
my $self = shift;

Expand Down Expand Up @@ -70,35 +99,6 @@ sub render {
return $render && $format ? "$string.$format" : $string;
}

sub shape_match {
my ($self, $pathref, $detect) = @_;

# Compile on demand
my $regex = $self->regex || $self->_compile;
my $format
= $detect ? ($self->format_regex || $self->_compile_format) : undef;

# Match
return undef unless my @captures = $$pathref =~ $regex;
$$pathref =~ s/$regex//;

# Merge captures
my $result = {%{$self->defaults}};
for my $placeholder (@{$self->placeholders}) {
last unless @captures;
my $capture = shift @captures;
$result->{$placeholder} = $capture if defined $capture;
}

# Format
my $constraint = $self->constraints->{format};
return $result if !$detect || defined $constraint && !$constraint;
if ($$pathref =~ s!^/?$format!!) { $result->{format} = $1 }
elsif ($constraint) { return undef unless $result->{format} }

return $result;
}

sub _compile {
my $self = shift;

Expand Down Expand Up @@ -374,6 +374,14 @@ necessary.
Match pattern against entire path, format detection is disabled by default.
=head2 match_partial
my $result = $pattern->match_partial(\$path);
my $result = $pattern->match_partial(\$path, 1);
Match pattern against path and remove matching parts, format detection is
disabled by default.
=head2 parse
$pattern = $pattern->parse('/:action');
Expand All @@ -390,14 +398,6 @@ Parse pattern.
Render pattern into a path with parameters, format rendering is disabled by
default.
=head2 shape_match
my $result = $pattern->shape_match(\$path);
my $result = $pattern->shape_match(\$path, 1);
Match pattern against path and remove matching parts, format detection is
disabled by default.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down

0 comments on commit ec14f4a

Please sign in to comment.