Skip to content

Commit

Permalink
renamed params method in Mojo::Parameters to pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 23, 2015
1 parent adb21cd commit 4d592b7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -23,6 +23,7 @@
- Removed deprecated render_exception and render_not_found methods from
Mojolicious::Controller.
- Removed deprecated keep_alive_requests setting from Hypnotoad.
- Renamed params method in Mojo::Parameters to pairs.
- Added names method to Mojo::Parameters.
- Added failed and passed methods to Mojolicious::Validator::Validation.

Expand Down
87 changes: 43 additions & 44 deletions lib/Mojo/Parameters.pm
@@ -1,7 +1,7 @@
package Mojo::Parameters;
use Mojo::Base -base;
use overload
'@{}' => sub { shift->params },
'@{}' => sub { shift->pairs },
bool => sub {1},
'""' => sub { shift->to_string },
fallback => 1;
Expand All @@ -13,15 +13,15 @@ has charset => 'UTF-8';
sub append {
my $self = shift;

my $params = $self->params;
my @pairs = @_ == 1 ? @{shift->params} : @_;
while (my ($name, $value) = splice @pairs, 0, 2) {
my $old = $self->pairs;
my @new = @_ == 1 ? @{shift->pairs} : @_;
while (my ($name, $value) = splice @new, 0, 2) {

# Multiple values
if (ref $value eq 'ARRAY') { push @$params, $name => $_ // '' for @$value }
if (ref $value eq 'ARRAY') { push @$old, $name => $_ // '' for @$value }

# Single value
else { push @$params, $name => $value }
else { push @$old, $name => $value }
}

return $self;
Expand All @@ -33,7 +33,7 @@ sub clone {
my $clone = $self->new;
if (exists $self->{charset}) { $clone->{charset} = $self->{charset} }
if (defined $self->{string}) { $clone->{string} = $self->{string} }
else { $clone->{params} = [@{$self->params}] }
else { $clone->{pairs} = [@{$self->pairs}] }

return $clone;
}
Expand All @@ -42,9 +42,9 @@ sub every_param {
my ($self, $name) = @_;

my @values;
my $params = $self->params;
for (my $i = 0; $i < @$params; $i += 2) {
push @values, $params->[$i + 1] if $params->[$i] eq $name;
my $pairs = $self->pairs;
for (my $i = 0; $i < @$pairs; $i += 2) {
push @values, $pairs->[$i + 1] if $pairs->[$i] eq $name;
}

return \@values;
Expand All @@ -53,7 +53,7 @@ sub every_param {
sub merge {
my $self = shift;

my @pairs = @_ == 1 ? @{shift->params} : @_;
my @pairs = @_ == 1 ? @{shift->pairs} : @_;
while (my ($name, $value) = splice @pairs, 0, 2) {
defined $value ? $self->param($name => $value) : $self->remove($name);
}
Expand All @@ -65,27 +65,20 @@ sub names { [sort keys %{shift->to_hash}] }

sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }

sub param {
my ($self, $name) = (shift, shift);
return $self->every_param($name)->[-1] unless @_;
$self->remove($name);
return $self->append($name => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]);
}

sub params {
sub pairs {
my $self = shift;

# Replace parameters
if (@_) {
$self->{params} = shift;
$self->{pairs} = shift;
delete $self->{string};
return $self;
}

# Parse string
if (defined(my $str = delete $self->{string})) {
my $params = $self->{params} = [];
return $params unless length $str;
my $pairs = $self->{pairs} = [];
return $pairs unless length $str;

my $charset = $self->charset;
for my $pair (split '&', $str) {
Expand All @@ -99,11 +92,18 @@ sub params {
$value = url_unescape $value;
$value = decode($charset, $value) // $value if $charset;

push @$params, $name, $value;
push @$pairs, $name, $value;
}
}

return $self->{params} ||= [];
return $self->{pairs} ||= [];
}

sub param {
my ($self, $name) = (shift, shift);
return $self->every_param($name)->[-1] unless @_;
$self->remove($name);
return $self->append($name => ref $_[0] eq 'ARRAY' ? $_[0] : [@_]);
}

sub parse {
Expand All @@ -120,10 +120,9 @@ sub parse {
sub remove {
my ($self, $name) = @_;

my $params = $self->params;
my $i = 0;
$params->[$i] eq $name ? splice @$params, $i, 2 : ($i += 2)
while $i < @$params;
my $pairs = $self->pairs;
my $i = 0;
$pairs->[$i] eq $name ? splice @$pairs, $i, 2 : ($i += 2) while $i < @$pairs;

return $self;
}
Expand All @@ -132,9 +131,9 @@ sub to_hash {
my $self = shift;

my %hash;
my $params = $self->params;
for (my $i = 0; $i < @$params; $i += 2) {
my ($name, $value) = @{$params}[$i, $i + 1];
my $pairs = $self->pairs;
for (my $i = 0; $i < @$pairs; $i += 2) {
my ($name, $value) = @{$pairs}[$i, $i + 1];

# Array
if (exists $hash{$name}) {
Expand All @@ -160,11 +159,11 @@ sub to_string {
}

# Build pairs
my $params = $self->params;
return '' unless @$params;
my $pairs = $self->pairs;
return '' unless @$pairs;
my @pairs;
for (my $i = 0; $i < @$params; $i += 2) {
my ($name, $value) = @{$params}[$i, $i + 1];
for (my $i = 0; $i < @$pairs; $i += 2) {
my ($name, $value) = @{$pairs}[$i, $i + 1];

# Escape and replace whitespace with "+"
$name = encode $charset, $name if $charset;
Expand Down Expand Up @@ -300,6 +299,13 @@ Return a list of all parameter names.
Construct a new L<Mojo::Parameters> object and L</"parse"> parameters if
necessary.
=head2 pairs
my $array = $params->pairs;
$params = $params->pairs([foo => 'b&ar', baz => 23]);
Parsed parameter pairs. Note that this method will normalize the parameters.
=head2 param
my @names = $params->param;
Expand All @@ -312,13 +318,6 @@ Access parameter values. If there are multiple values sharing the same name,
and you want to access more than just the last one, you can use
L</"every_param">. Note that this method will normalize the parameters.
=head2 params
my $array = $params->params;
$params = $params->params([foo => 'b&ar', baz => 23]);
Parsed parameters. Note that this method will normalize the parameters.
=head2 parse
$params = $params->parse('foo=b%3Bar&baz=23');
Expand Down Expand Up @@ -356,9 +355,9 @@ L<Mojo::Parameters> overloads the following operators.
=head2 array
my @params = @$params;
my @pairs = @$params;
Alias for L</"params">. Note that this will normalize the parameters.
Alias for L</"pairs">. Note that this will normalize the parameters.
say $params->[0];
say for @$params;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Daemon.pm
Expand Up @@ -179,7 +179,7 @@ sub _listen {

return if $self->silent;
$self->app->log->info(qq{Listening at "$url"});
$query->params([]);
$query->pairs([]);
$url->host('127.0.0.1') if $url->host eq '*';
say "Server available at $url";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/URL.pm
Expand Up @@ -114,7 +114,7 @@ sub query {
return $q unless @_;

# Replace with list
if (@_ > 1) { $q->params([])->parse(@_) }
if (@_ > 1) { $q->pairs([])->parse(@_) }

# Merge with array
elsif (ref $_[0] eq 'ARRAY') { $q->merge(@{$_[0]}) }
Expand Down
14 changes: 7 additions & 7 deletions t/mojo/parameters.t
Expand Up @@ -9,7 +9,7 @@ my $params2 = Mojo::Parameters->new('x', 1, 'y', 2);
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is $params2->to_string, 'x=1&y=2', 'right format';
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is_deeply $params->params, ['foo', 'b;ar', 'baz', 23], 'right structure';
is_deeply $params->pairs, ['foo', 'b;ar', 'baz', 23], 'right structure';
$params->append(a => 4, a => 5, b => 6, b => 7);
is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7', 'right format';
push @$params, c => 'f;oo';
Expand Down Expand Up @@ -79,11 +79,11 @@ is $params->to_string, '0=0', 'right format';
# Semicolon
$params = Mojo::Parameters->new('foo=bar;baz');
is $params->to_string, 'foo=bar;baz', 'right format';
is_deeply $params->params, [foo => 'bar;baz'], 'right structure';
is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure';
is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure';
is $params->to_string, 'foo=bar%3Bbaz', 'right format';
$params = Mojo::Parameters->new($params->to_string);
is_deeply $params->params, [foo => 'bar;baz'], 'right structure';
is_deeply $params->pairs, [foo => 'bar;baz'], 'right structure';
is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure';
is $params->to_string, 'foo=bar%3Bbaz', 'right format';

Expand Down Expand Up @@ -152,15 +152,15 @@ is $params->param(foo => ['ba;r', 'baz'])->to_string, 'foo=ba%3Br&foo=baz',
# Unicode
$params = Mojo::Parameters->new;
$params->parse('input=say%20%22%C2%AB~%22;');
is_deeply $params->params, ['input', 'say "«~";'], 'right structure';
is_deeply $params->pairs, ['input', 'say "«~";'], 'right structure';
is $params->param('input'), 'say "«~";', 'right value';
is "$params", 'input=say+%22%C2%AB~%22%3B', 'right result';
$params = Mojo::Parameters->new('♥=☃');
is_deeply $params->params, ['', ''], 'right structure';
is_deeply $params->pairs, ['', ''], 'right structure';
is $params->param(''), '', 'right value';
is "$params", '%E2%99%A5=%E2%98%83', 'right result';
$params = Mojo::Parameters->new('%E2%99%A5=%E2%98%83');
is_deeply $params->params, ['', ''], 'right structure';
is_deeply $params->pairs, ['', ''], 'right structure';
is $params->param(''), '', 'right value';
is "$params", '%E2%99%A5=%E2%98%83', 'right result';

Expand All @@ -171,7 +171,7 @@ is "$params", 'foo=bar&baz=23', 'right result';

# Replace
$params = Mojo::Parameters->new('a=1&b=2');
$params->params([a => 2, b => 3]);
$params->pairs([a => 2, b => 3]);
is $params->to_string, 'a=2&b=3', 'right result';

# Query string
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/request.t
Expand Up @@ -2066,7 +2066,7 @@ ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/perldoc?Mojo::Message::Request', 'right URL';
is $req->url->query->params->[0], 'Mojo::Message::Request', 'right value';
is $req->url->query->pairs->[0], 'Mojo::Message::Request', 'right value';

# Parse lots of special characters in URL
$req = Mojo::Message::Request->new;
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/url.t
Expand Up @@ -96,7 +96,7 @@ is $url->host, 'example.com', 'right host';
is $url->port, '8080', 'right port';
is $url->path, '', 'no path';
is $url->query, '_monkeybiz%3B&_monkey;23', 'right query';
is_deeply $url->query->params, ['_monkeybiz;', '', '_monkey;23', ''],
is_deeply $url->query->pairs, ['_monkeybiz;', '', '_monkey;23', ''],
'right structure';
is $url->query, '_monkeybiz%3B=&_monkey%3B23=', 'right query';
is $url->fragment, '23', 'right fragment';
Expand Down

0 comments on commit 4d592b7

Please sign in to comment.