Skip to content

Commit

Permalink
added direct array access for path parts to Mojo::Path
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 9, 2013
1 parent 2271df8 commit 388fb4b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.90 2013-03-10
- Added direct array access for path parts to Mojo::Path.
- Improved dumper helper to sort hash keys.
- Improved documentation.
- Improved tests.
Expand Down
10 changes: 10 additions & 0 deletions lib/Mojo/Path.pm
@@ -1,6 +1,7 @@
package Mojo::Path;
use Mojo::Base -base;
use overload
'@{}' => sub { shift->parts },
'bool' => sub {1},
'""' => sub { shift->to_string },
fallback => 1;
Expand Down Expand Up @@ -305,6 +306,15 @@ Turn path into a string.
Path has a trailing slash. Note that this method will normalize the path and
that C<%2F> will be treated as C</> for security reasons.
=head1 PATH PARTS
Direct array reference access to path parts is also possible. Note that this
will normalize the path and that C<%2F> will be treated as C</> for security
reasons.
say $path->[0];
say for @$path;
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -249,8 +249,7 @@ incoming requests is also quite common.
# Move first part from path to base path in production mode
app->hook(before_dispatch => sub {
my $self = shift;
push @{$self->req->url->base->path->parts},
shift @{$self->req->url->path->parts};
push @{$self->req->url->base->path}, shift @{$self->req->url->path};
}) if app->mode eq 'production';

=head2 Application embedding
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/path.t
Expand Up @@ -24,8 +24,8 @@ is $path->to_abs_string, '/', 'right absolute path';

# Advanced
$path = Mojo::Path->new('/AZaz09-._~!$&\'()*+,;=:@');
is $path->parts->[0], 'AZaz09-._~!$&\'()*+,;=:@', 'right part';
is $path->parts->[1], undef, 'no part';
is $path->[0], 'AZaz09-._~!$&\'()*+,;=:@', 'right part';
is $path->[1], undef, 'no part';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
is "$path", '/AZaz09-._~!$&\'()*+,;=:@', 'right path';
Expand Down

0 comments on commit 388fb4b

Please sign in to comment.