Skip to content

Commit

Permalink
added merge method to Mojo::Path
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 12, 2012
1 parent 6a2b29f commit b750b7d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 26 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.96 2012-05-12
- Added merge method to Mojo::Path.
- Improved documentation.
- Improved tests.

Expand Down
29 changes: 29 additions & 0 deletions lib/Mojo/Path.pm
Expand Up @@ -61,6 +61,19 @@ sub contains {
return @$parts ? undef : 1;
}

sub merge {
my ($self, $path) = @_;

# Replace
return $self->parse($path) if $path =~ qr|^/|;

# Merge
pop @{$self->parts} unless $self->trailing_slash;
$path = $self->new($path);
push @{$self->parts}, @{$path->parts};
return $self->leading_slash(1)->trailing_slash($path->trailing_slash);
}

sub parse {
my ($self, $path) = @_;

Expand Down Expand Up @@ -186,6 +199,22 @@ Check if path contains given prefix.
Mojo::Path->new('/foo/bar')->contains('/bar');
Mojo::Path->new('/foo/bar')->contains('/whatever');
=head2 C<merge>
$path = $path->merge('/foo/bar');
$path = $path->merge('foo/bar');
Merge paths.
# "/baz/yada"
Mojo::Path->new('/foo/bar')->merge('/baz/yada');
# "/foo/baz/yada"
Mojo::Path->new('/foo/bar')->merge('baz/yada');
# "/foo/bar/baz/yada"
Mojo::Path->new('/foo/bar/')->merge('baz/yada');
=head2 C<parse>
$path = $path->parse('/foo%2Fbar%3B/baz.html');
Expand Down
29 changes: 4 additions & 25 deletions lib/Mojo/URL.pm
Expand Up @@ -109,24 +109,11 @@ sub path {
my ($self, $path) = @_;

# Old path
return $self->{path} ||= Mojo::Path->new unless $path;
$self->{path} ||= Mojo::Path->new;
return $self->{path} unless $path;

# New path
if (!ref $path) {

# Absolute path
if ($path =~ m#^/#) { $path = Mojo::Path->new($path) }

# Relative path
else {
my $new = Mojo::Path->new($path);
$path = $self->{path} || Mojo::Path->new;
pop @{$path->parts} unless $path->trailing_slash;
push @{$path->parts}, @{$new->parts};
$path->leading_slash(1)->trailing_slash($new->trailing_slash);
}
}
$self->{path} = $path;
$self->{path} = ref $path ? $path : $self->{path}->merge($path);

return $self;
}
Expand Down Expand Up @@ -189,15 +176,7 @@ sub to_abs {
}

# Merge paths
else {
my $new = $base_path->clone->leading_slash(1);

# Characters after the right-most '/' need to go
pop @{$new->parts} if @{$path->parts} && !$new->trailing_slash;
push @{$new->parts}, @{$path->parts};
$new->trailing_slash($path->trailing_slash) if @{$new->parts};
$abs->path($new->canonicalize);
}
else { $abs->path($base_path->clone->merge($path)->canonicalize) }

return $abs;
}
Expand Down
29 changes: 28 additions & 1 deletion t/mojo/path.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 188;
use Test::More tests => 203;

# "This is the greatest case of false advertising I’ve seen since I sued the
# movie 'The Never Ending Story.'"
Expand Down Expand Up @@ -170,6 +170,33 @@ ok !$path->contains('/0/0.html'), 'does not contain path';
ok !$path->contains('/0.html'), 'does not contain path';
ok !$path->contains('/♥.html'), 'does not contain path';

# Merge
$path = Mojo::Path->new('/foo');
$path->merge('bar/baz');
is "$path", '/bar/baz', 'right path';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
$path = Mojo::Path->new('/foo/');
$path->merge('bar/baz');
is "$path", '/foo/bar/baz', 'right path';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
$path = Mojo::Path->new('/foo/');
$path->merge('bar/baz/');
is "$path", '/foo/bar/baz/', 'right path';
ok $path->leading_slash, 'has leading slash';
ok $path->trailing_slash, 'has trailing slash';
$path = Mojo::Path->new('/foo/');
$path->merge('/bar/baz');
is "$path", '/bar/baz', 'right path';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
$path = Mojo::Path->new('/foo/bar');
$path->merge('/bar/baz/');
is "$path", '/bar/baz/', 'right path';
ok $path->leading_slash, 'has leading slash';
ok $path->trailing_slash, 'has trailing slash';

# Empty path elements
$path = Mojo::Path->new('//');
is "$path", '//', 'right result';
Expand Down

0 comments on commit b750b7d

Please sign in to comment.