Skip to content

Commit

Permalink
improved Mojo::Parameters to consistently accept arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 16, 2014
1 parent 831ad37 commit 2f52d22
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -14,6 +14,7 @@
- Added append method to Mojo::Log.
- Added check method to Mojo::Server::Morbo.
- Updated jQuery to version 2.1.1.
- Improved Mojo::Parameters to consistently accept arrays.
- Fixed Mojo::IOLoop::Server to work correctly with newer versions of
IO::Socket::SSL. (noxxi)
- Fixed warnings in Mojo::IOLoop::Delay.
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -57,7 +57,7 @@ sub param {

# Replace values
$self->remove($name) if defined $_[0];
return $self->append($name => [@_]) if @_;
return $self->append($name => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]) if @_;

# List values
my @values;
Expand Down Expand Up @@ -274,6 +274,7 @@ necessary.
my ($foo, $bar) = $params->param(['foo', 'bar']);
$params = $params->param(foo => 'ba&r');
$params = $params->param(foo => qw(ba&r baz));
$params = $params->param(foo => ['ba;r', 'baz']);
Check and replace parameter value. Be aware that if you request a parameter by
name in scalar context, you will receive only the I<first> value for that
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -583,7 +583,8 @@ status.
my @foo = $c->param('foo');
my ($foo, $bar) = $c->param(['foo', 'bar']);
$c = $c->param(foo => 'ba;r');
$c = $c->param(foo => qw(ba;r ba;z));
$c = $c->param(foo => qw(ba;r baz));
$c = $c->param(foo => ['ba;r', 'baz']);
Access route placeholder values that are not reserved stash values, file
uploads and C<GET>/C<POST> parameters, in that order. Note that this method is
Expand Down
3 changes: 3 additions & 0 deletions t/mojo/parameters.t
Expand Up @@ -141,6 +141,9 @@ is_deeply $params->to_hash, {foo => ['ba;r', 'b;az'], bar => 23},
'right structure';
is $params->param('foo'), 'ba;r', 'right value';
is_deeply [$params->param('foo')], [qw(ba;r b;az)], 'right values';
$params = Mojo::Parameters->new;
is $params->param(foo => ['ba;r', 'baz'])->to_string, 'foo=ba%3Br&foo=baz',
'right format';

# Unicode
$params = Mojo::Parameters->new;
Expand Down
3 changes: 3 additions & 0 deletions t/mojolicious/dispatch.t
Expand Up @@ -75,6 +75,9 @@ is_deeply [$c->param('foo')], [qw(just works)], 'right values';
is $c->param(foo => undef)->param('foo'), undef, 'no value';
is $c->param(foo => Mojo::Upload->new(name => 'bar'))->param('foo')->name,
'bar', 'right value';
is scalar $c->param(foo => ['ba;r', 'baz'])->param('foo'), 'ba;r',
'right value';
is_deeply [$c->param('foo')], ['ba;r', 'baz'], 'right values';

# Reserved stash values are hidden
$c = Mojolicious::Controller->new;
Expand Down

0 comments on commit 2f52d22

Please sign in to comment.