[Catalyst-commits] r10741 - in Catalyst-Runtime/5.80/trunk: . lib/Catalyst t

gphat at dev.catalyst.perl.org gphat at dev.catalyst.perl.org
Tue Jun 30 15:24:38 GMT 2009


Author: gphat
Date: 2009-06-30 15:24:37 +0000 (Tue, 30 Jun 2009)
New Revision: 10741

Modified:
   Catalyst-Runtime/5.80/trunk/Changes
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Request.pm
   Catalyst-Runtime/5.80/trunk/t/unit_core_uri_with.t
Log:
Add new uri_with mode for appending.


Modified: Catalyst-Runtime/5.80/trunk/Changes
===================================================================
--- Catalyst-Runtime/5.80/trunk/Changes	2009-06-30 15:21:22 UTC (rev 10740)
+++ Catalyst-Runtime/5.80/trunk/Changes	2009-06-30 15:24:37 UTC (rev 10741)
@@ -6,6 +6,12 @@
        - Inherited controller methods can now be specified in config->{action(s)}
          (edenc)
 
+  New features:
+       - Add optional second argument to uri_with which appends to existing
+         params rather than replacing them. (foo=1 becomes foo=1&foo=2 when
+         uri_with({ foo => 2 }, { mode => 'append' }) is called on a foo=1
+         URI.
+
 5.80006 2009-06-29 23:37:47
 
   Bug fixes:

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Request.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Request.pm	2009-06-30 15:21:22 UTC (rev 10740)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Request.pm	2009-06-30 15:24:37 UTC (rev 10741)
@@ -571,19 +571,35 @@
 
 Returns a URI object for the current request. Stringifies to the URI text.
 
-=head2 $req->uri_with( { key => 'value' } );
+=head2 $req->mangle_params( { key => 'value' }, $appendmode);
 
-Returns a rewritten URI object for the current request. Key/value pairs
-passed in will override existing parameters. You can remove an existing
-parameter by passing in an undef value. Unmodified pairs will be
-preserved.
+Returns a hashref of parameters stemming from the current request's params,
+plus the ones supplied.  Keys for which no current param exists will be
+added, keys with undefined values will be removed and keys with existing
+params will be replaced.  Note that you can supply a true value as the final
+argument to change behavior with regards to existing parameters, appending
+values rather than replacing them.
 
+A quick example:
+
+  # URI query params foo=1
+  my $hashref = $req->mangle_params({ foo => 2 });
+  # Result is query params of foo=2
+
+versus append mode:
+
+  # URI query params foo=1
+  my $hashref = $req->mangle_params({ foo => 2 }, 1);
+  # Result is query params of foo=1&foo=2
+
+This is the code behind C<uri_with>.
+
 =cut
 
-sub uri_with {
-    my( $self, $args ) = @_;
+sub mangle_params {
+    my ($self, $args, $append) = @_;
 
-    carp( 'No arguments passed to uri_with()' ) unless $args;
+    carp('No arguments passed to mangle_params()') unless $args;
 
     foreach my $value ( values %$args ) {
         next unless defined $value;
@@ -593,13 +609,66 @@
         }
     };
 
-    my $uri   = $self->uri->clone;
-    my %query = ( %{ $uri->query_form_hash }, %$args );
+    my %params = %{ $self->uri->query_form_hash };
+    foreach my $key (keys %{ $args }) {
+        my $val = $args->{$key};
+        if(defined($val)) {
 
-    $uri->query_form( {
-        # remove undef values
-        map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
-    } );
+            if($append && exists($params{$key})) {
+
+                # This little bit of heaven handles appending a new value onto
+                # an existing one regardless if the existing value is an array
+                # or not, and regardless if the new value is an array or not
+                $params{$key} = [
+                    ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key},
+                    ref($val) eq 'ARRAY' ? @{ $val } : $val
+                ];
+
+            } else {
+                $params{$key} = $val;
+            }
+        } else {
+
+            # If the param wasn't defined then we delete it.
+            delete($params{$key});
+        }
+    }
+
+
+    return \%params;
+}
+
+=head2 $req->uri_with( { key => 'value' } );
+
+Returns a rewritten URI object for the current request. Key/value pairs
+passed in will override existing parameters. You can remove an existing
+parameter by passing in an undef value. Unmodified pairs will be
+preserved.
+
+You may also pass an optional second parameter that puts C<uri_with> into
+append mode:
+
+  $req->uri_with( { key => 'value' }, { mode => 'append' } );
+  
+See C<mangle_params> for an explanation of this behavior.
+
+=cut
+
+sub uri_with {
+    my( $self, $args, $behavior) = @_;
+
+    carp( 'No arguments passed to uri_with()' ) unless $args;
+
+    my $append = 0;
+    if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
+        $append = 1;
+    }
+
+    my $params = $self->mangle_params($args, $append);
+
+    my $uri = $self->uri->clone;
+    $uri->query_form($params);
+
     return $uri;
 }
 

Modified: Catalyst-Runtime/5.80/trunk/t/unit_core_uri_with.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/unit_core_uri_with.t	2009-06-30 15:21:22 UTC (rev 10740)
+++ Catalyst-Runtime/5.80/trunk/t/unit_core_uri_with.t	2009-06-30 15:24:37 UTC (rev 10741)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 7;
+use Test::More tests => 10;
 use URI;
 
 use_ok('Catalyst::Request');
@@ -48,3 +48,22 @@
     'http://127.0.0.1/foo/bar/baz?bar=snort',
     'URI changes param'
 );
+
+is(
+    $request2->uri_with({ bar => [ 'snort', 'ewok' ] }),
+    'http://127.0.0.1/foo/bar/baz?bar=snort&bar=ewok',
+    'overwrite mode URI appends arrayref param'
+);
+
+is(
+    $request2->uri_with({ bar => 'snort' }, { mode => 'append' }),
+    'http://127.0.0.1/foo/bar/baz?bar=gorch&bar=snort',
+    'append mode URI appends param'
+);
+
+is(
+    $request2->uri_with({ bar => [ 'snort', 'ewok' ] }, { mode => 'append' }),
+    'http://127.0.0.1/foo/bar/baz?bar=gorch&bar=snort&bar=ewok',
+    'append mode URI appends arrayref param'
+);
+




More information about the Catalyst-commits mailing list