From 485fa5f4cd06415a52650701fe9407d09f4a5c97 Mon Sep 17 00:00:00 2001 From: sshaw Date: Sun, 11 Mar 2012 20:02:05 -0700 Subject: [PATCH] v0.01 prep --- Makefile.PL | 3 +- README.pod | 12 ++++- lib/Mojolicious/Plugin/ParamExpand.pm | 12 ++++- t/param_expand.t | 67 +++++++++++++++++++++++++-- 4 files changed, 86 insertions(+), 8 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 4b5f7f1..eb089e8 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -8,8 +8,7 @@ WriteMakefile( VERSION_FROM => 'lib/Mojolicious/Plugin/ParamExpand.pm', ABSTRACT_FROM => 'lib/Mojolicious/Plugin/ParamExpand.pm', AUTHOR => 'Skye Shaw ', - #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) } ? diff --git a/README.pod b/README.pod index a23f5c7..9208119 100644 --- a/README.pod +++ b/README.pod @@ -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}; # ... } @@ -33,6 +33,12 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms L turns request parameters into nested data structures using L and helps you use these values in a form. +=head1 MOJOLICIOUS VERSION + +Due to the old way C handled multi-valued request parameters +versions of Mojolicious B 2.52 will not work with this plugin. If this is a problem for +you try L. + =head1 OPTIONS Options must be specified when loading the plugin. @@ -51,6 +57,8 @@ flattened parameter. Defaults to C<'.'>. Maximum number of array elements C 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 @@ -94,6 +102,8 @@ is equivlent to users=userA&users=userB +If this is undesirable you could L<< set C to zero|/max_array >>. + =head2 named Helps to create form fields suitable for parameter expansion. diff --git a/lib/Mojolicious/Plugin/ParamExpand.pm b/lib/Mojolicious/Plugin/ParamExpand.pm index 7c2822b..bac0a8f 100644 --- a/lib/Mojolicious/Plugin/ParamExpand.pm +++ b/lib/Mojolicious/Plugin/ParamExpand.pm @@ -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}; # ... } @@ -122,6 +122,12 @@ Mojolicious::Plugin::ParamExpand - Use objects and data structures in your forms L turns request parameters into nested data structures using L and helps you use these values in a form. +=head1 MOJOLICIOUS VERSION + +Due to the old way C handled multi-valued request parameters +versions of Mojolicious B 2.52 will not work with this plugin. If this is a problem for +you try L. + =head1 OPTIONS Options must be specified when loading the plugin. @@ -140,6 +146,8 @@ flattened parameter. Defaults to C<'.'>. Maximum number of array elements C 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 @@ -183,6 +191,8 @@ is equivlent to users=userA&users=userB +If this is undesirable you could L<< set C to zero|/max_array >>. + =head2 named Helps to create form fields suitable for parameter expansion. diff --git a/t/param_expand.t b/t/param_expand.t index 9fe229b..98e865f 100644 --- a/t/param_expand.t +++ b/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; @@ -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'; @@ -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); @@ -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', @@ -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') %>