Skip to content

Commit

Permalink
v0.01 prep
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaw committed Mar 12, 2012
1 parent edb44bf commit 485fa5f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Makefile.PL
Expand Up @@ -8,8 +8,7 @@ WriteMakefile(
VERSION_FROM => 'lib/Mojolicious/Plugin/ParamExpand.pm',
ABSTRACT_FROM => 'lib/Mojolicious/Plugin/ParamExpand.pm',
AUTHOR => 'Skye Shaw <sshaw AT lucas.cis.temple.edu>',
#TODO test #1 fails somewhere between here and 2.5
PREREQ_PM => {'Mojolicious' => '2.57'},
PREREQ_PM => { 'Mojolicious' => '2.52' },
LICENSE => 'perl',
test => {TESTS => 't/*.t'},
(eval { ExtUtils::MakeMaker->VERSION(6.46) } ?
Expand Down
12 changes: 11 additions & 1 deletion README.pod
Expand Up @@ -19,7 +19,7 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms
my $order = $self->param('order');
$order->{address};
$order->{items}->[0]->{id};
$order->{items}->[1]->{price};
$order->{items}->[0]->{price};
# ...
}

Expand All @@ -33,6 +33,12 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms
L<Mojolicious::Plugin::ParamExpand> turns request parameters into nested data
structures using L<CGI::Expand> and helps you use these values in a form.

=head1 MOJOLICIOUS VERSION

Due to the old way C<Mojolicious::Controller> handled multi-valued request parameters
versions of Mojolicious B<less than> 2.52 will not work with this plugin. If this is a problem for
you try L<Mojolicious::Plugin::GroupedParams>.

=head1 OPTIONS

Options must be specified when loading the plugin.
Expand All @@ -51,6 +57,8 @@ flattened parameter. Defaults to C<'.'>.
Maximum number of array elements C<CGI::Expand> will create.
Defaults to C<100>.

To force the array into a hash keyed by its indexes set this to C<0>.

=head1 Methods

=head2 param
Expand Down Expand Up @@ -94,6 +102,8 @@ is equivlent to

users=userA&users=userB

If this is undesirable you could L<< set C<max_array> to zero|/max_array >>.

=head2 named

Helps to create form fields suitable for parameter expansion.
Expand Down
12 changes: 11 additions & 1 deletion lib/Mojolicious/Plugin/ParamExpand.pm
Expand Up @@ -108,7 +108,7 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms
my $order = $self->param('order');
$order->{address};
$order->{items}->[0]->{id};
$order->{items}->[1]->{price};
$order->{items}->[0]->{price};
# ...
}
Expand All @@ -122,6 +122,12 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms
L<Mojolicious::Plugin::ParamExpand> turns request parameters into nested data
structures using L<CGI::Expand> and helps you use these values in a form.
=head1 MOJOLICIOUS VERSION
Due to the old way C<Mojolicious::Controller> handled multi-valued request parameters
versions of Mojolicious B<less than> 2.52 will not work with this plugin. If this is a problem for
you try L<Mojolicious::Plugin::GroupedParams>.
=head1 OPTIONS
Options must be specified when loading the plugin.
Expand All @@ -140,6 +146,8 @@ flattened parameter. Defaults to C<'.'>.
Maximum number of array elements C<CGI::Expand> will create.
Defaults to C<100>.
To force the array into a hash keyed by its indexes set this to C<0>.
=head1 Methods
=head2 param
Expand Down Expand Up @@ -183,6 +191,8 @@ is equivlent to
users=userA&users=userB
If this is undesirable you could L<< set C<max_array> to zero|/max_array >>.
=head2 named
Helps to create form fields suitable for parameter expansion.
Expand Down
67 changes: 63 additions & 4 deletions t/param_expand.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;
use Mojolicious::Lite;
use Test::More tests => 45;
use Test::More tests => 49;
use Test::Mojo;

package Model;
Expand All @@ -15,8 +15,30 @@ package main;

sub qs { join '&', @_ }
sub user { Model->new(id => 1, name => 'sshaw') }
sub select_id { sprintf 'input[name="user.id"][value="%s"]', shift }
sub select_name { sprintf 'input[name="user.name"][value="%s"]', shift }

sub selector
{
my $val = shift;
my $fx = pop;
my $sep = shift || '.';
$fx->($sep, $val);
}

sub select_id
{
selector(@_, sub {
sprintf 'input[name="user%sid"][value="%s"]', shift, shift
});
}

sub select_name
{
selector(@_, sub {
sprintf 'input[name="user%sname"][value="%s"]', shift, shift
});
}

{

plugin 'ParamExpand';

Expand All @@ -25,7 +47,7 @@ get '/params_are_expanded' => sub {
my $hash = $self->param('hash');
my @array = $self->param('array');
my $scalar = $self->param('scalar');

$self->render(hash => $hash,
array => \@array,
scalar => $scalar);
Expand Down Expand Up @@ -71,6 +93,7 @@ get '/named_array_with_non_numeric_index' => sub { shift->named('array.x', []) }
get '/named_with_a_non_reference'=> sub { shift->named('x.y', 123) };
get '/named_with_a_non_existant_accessor' => sub { shift->named('user.x', Model->new) };


my $qs = qs 'hash.a=a',
'hash.b.c=b',
'array.0=0',
Expand Down Expand Up @@ -137,12 +160,48 @@ $t->get_ok('/named_with_a_non_reference')
$t->get_ok('/named_with_a_non_existant_accessor')
->status_is(500)
->content_like(qr/access a Model/);

}

{
plugin 'ParamExpand', separator => '->';
get '/user_defined_separator' => sub {
my $self = shift;
my $user = $self->param('user');
$self->render('alt_form', user => $user);
};

my $qs = qs 'user->id=x', 'user->name=skye';
my $t = Test::Mojo->new;
$t->get_ok("/user_defined_separator?$qs")
->status_is(200)
->element_exists(select_id('x', '->'))
->element_exists(select_name('skye', '->'));
}

# {
# plugin 'ParamExpand', max_array => 1;

# get '/user_defined_max_array' => sub {
# shift->render(text => 'success!')
# };

# my $qs = qs 'users.0=a', 'users.1=b';
# my $t = Test::Mojo->new;
# $t->get_ok("/user_defined_max_array?$qs")
# ->status_is(500)
# ->content_like(qr/limit exceeded/);
# }

__DATA__
@@ form.html.ep
<%= text_field named('user.name') %>
<%= hidden_field named('user.id') %>
@@ alt_form.html.ep
<%= text_field named('user->name') %>
<%= hidden_field named('user->id') %>
@@ form_for_array_with_hash.html.ep
<%= text_field named('users.0.name') %>
<%= text_field named('users.1.name') %>
Expand Down

0 comments on commit 485fa5f

Please sign in to comment.