[Catalyst-commits] r13154 - in Catalyst-Runtime/5.80/branches/captures_and_args_combined: lib lib/Catalyst t/aggregate

khisanth at dev.catalyst.perl.org khisanth at dev.catalyst.perl.org
Mon Apr 12 20:49:01 GMT 2010


Author: khisanth
Date: 2010-04-12 21:49:01 +0100 (Mon, 12 Apr 2010)
New Revision: 13154

Modified:
   Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst.pm
   Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/Action.pm
   Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/ActionChain.pm
   Catalyst-Runtime/5.80/branches/captures_and_args_combined/t/aggregate/unit_core_uri_for_action.t
Log:
allow uri_for_action to be called with captures and args in a single arrayref

Modified: Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/Action.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/Action.pm	2010-04-12 19:54:17 UTC (rev 13153)
+++ Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/Action.pm	2010-04-12 20:49:01 UTC (rev 13154)
@@ -88,6 +88,19 @@
     return $a1_args <=> $a2_args;
 }
 
+sub number_of_args {
+    my ( $self ) = @_;
+    return 0 unless exists $self->attributes->{Args};
+    return $self->attributes->{Args}[0];
+}
+
+sub number_of_captures {
+    my ( $self ) = @_;
+
+    return 0 unless exists $self->attributes->{CaptureArgs};
+    return $self->attributes->{CaptureArgs}[0] || 0;
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
@@ -147,6 +160,14 @@
 
 Returns the sub name of this action.
 
+=head2 number_of_args
+
+Returns the number of args this action expects. This is 0 if the action doesn't take any arguments and undef if it will take any number of arguments.
+
+=head2 number_of_captures
+
+Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
+
 =head2 meta
 
 Provided by Moose.

Modified: Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/ActionChain.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/ActionChain.pm	2010-04-12 19:54:17 UTC (rev 13153)
+++ Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst/ActionChain.pm	2010-04-12 20:49:01 UTC (rev 13154)
@@ -4,7 +4,6 @@
 extends qw(Catalyst::Action);
 
 has chain => (is => 'rw');
-
 no Moose;
 
 =head1 NAME
@@ -30,8 +29,8 @@
     my $last = pop(@chain);
     foreach my $action ( @chain ) {
         my @args;
-        if (my $cap = $action->attributes->{CaptureArgs}) {
-          @args = splice(@captures, 0, $cap->[0]);
+        if (my $cap = $action->number_of_captures) {
+          @args = splice(@captures, 0, $cap);
         }
         local $c->request->{arguments} = \@args;
         $action->dispatch( $c );
@@ -45,6 +44,15 @@
     return $self->new({ %$final, chain => $actions });
 }
 
+sub number_of_captures {
+    my ( $self ) = @_;
+    my $chain = $self->chain;
+    my $captures = 0;
+
+    $captures += $_->number_of_captures for @$chain;
+    return $captures;
+}
+
 __PACKAGE__->meta->make_immutable;
 1;
 
@@ -67,6 +75,10 @@
 Takes a list of Catalyst::Action objects and constructs and returns a
 Catalyst::ActionChain object representing a chain of these actions
 
+=head2 number_of_captures
+
+Returns the total number of captures for the entire chain of actions.
+
 =head2 meta
 
 Provided by Moose

Modified: Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst.pm	2010-04-12 19:54:17 UTC (rev 13153)
+++ Catalyst-Runtime/5.80/branches/captures_and_args_combined/lib/Catalyst.pm	2010-04-12 20:49:01 UTC (rev 13154)
@@ -1297,7 +1297,15 @@
         }
 
         my $action = $path;
-        $path = $c->dispatcher->uri_for_action($action, $captures);
+        # ->uri_for( $action, \@captures_and_args, \%query_values? )
+        if( !@args && $action->number_of_args ) {
+            my $expanded_action = $c->dispatcher->expand_action( $action );
+
+            my $num_captures = $expanded_action->number_of_captures;
+            unshift @args, splice @$captures, $num_captures;
+        }
+
+       $path = $c->dispatcher->uri_for_action($action, $captures);
         if (not defined $path) {
             $c->log->debug(qq/Can't find uri_for action '$action' @$captures/)
                 if $c->debug;

Modified: Catalyst-Runtime/5.80/branches/captures_and_args_combined/t/aggregate/unit_core_uri_for_action.t
===================================================================
--- Catalyst-Runtime/5.80/branches/captures_and_args_combined/t/aggregate/unit_core_uri_for_action.t	2010-04-12 19:54:17 UTC (rev 13153)
+++ Catalyst-Runtime/5.80/branches/captures_and_args_combined/t/aggregate/unit_core_uri_for_action.t	2010-04-12 20:49:01 UTC (rev 13154)
@@ -8,7 +8,7 @@
 
 use Test::More;
 
-plan tests => 30;
+plan tests => 39;
 
 use_ok('TestApp');
 
@@ -130,10 +130,18 @@
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
         'uri_for_action correct for chained with multiple captures and args' );
 
+    is( $context->uri_for_action( '/action/chained/endpoint2', [1,2,3,4], { x => 5 } ),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4?x=5',
+        'uri_for_action correct for chained with multiple captures and args combined' );
+
     is( $context->uri_for_action( '/action/chained/three_end', [1,2,3], (4,5,6) ),
         'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
         'uri_for_action correct for chained with multiple capturing actions' );
 
+    is( $context->uri_for_action( '/action/chained/three_end', [1,2,3,4,5,6] ),
+        'http://127.0.0.1/foo/chained/one/1/two/2/3/three/4/5/6',
+        'uri_for_action correct for chained with multiple capturing actions and args combined' );
+
     my $action_needs_two = '/action/chained/endpoint2';
     
     ok( ! defined( $context->uri_for_action($action_needs_two, [1],     (2,3)) ),
@@ -142,7 +150,11 @@
     is( $context->uri_for_action($action_needs_two,            [1,2],   (2,3)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
         'uri_for_action returns correct uri for correct captures' );
-        
+
+    is( $context->uri_for_action($action_needs_two,            [1,2,2,3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/2/3',
+        'uri_for_action returns correct uri for correct captures and args combined' );
+
     ok( ! defined( $context->uri_for_action($action_needs_two, [1,2,3], (2,3)) ),
         'uri_for_action returns undef for too many captures' );
     
@@ -150,26 +162,49 @@
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
         'uri_for_action returns uri with lesser args than specified on action' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3',
+        'uri_for_action returns uri with lesser args than specified on action with captures combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2],   (3,4,5)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
         'uri_for_action returns uri with more args than specified on action' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3,4,5]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/4/5',
+        'uri_for_action returns uri with more args than specified on action with captures combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,''], (3,4)),
         'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
         'uri_for_action returns uri with empty capture on undef capture' );
 
+    is( $context->uri_for_action($action_needs_two, [1,'',3,4]),
+        'http://127.0.0.1/foo/chained/foo2/1//end2/3/4',
+        'uri_for_action returns uri with empty capture on undef capture and args combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2], ('',3)),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
         'uri_for_action returns uri with empty arg on undef argument' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,'',3]),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2//3',
+        'uri_for_action returns uri with empty arg on undef argument and args combined' );
+
     is( $context->uri_for_action($action_needs_two, [1,2], (3,'')),
         'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
         'uri_for_action returns uri with empty arg on undef last argument' );
 
+    is( $context->uri_for_action($action_needs_two, [1,2,3,'']),
+        'http://127.0.0.1/foo/chained/foo2/1/2/end2/3/',
+        'uri_for_action returns uri with empty arg on undef last argument with captures combined' );
+
     my $complex_chained = '/action/chained/empty_chain_f';
     is( $context->uri_for_action( $complex_chained, [23], (13), {q => 3} ),
         'http://127.0.0.1/foo/chained/empty/23/13?q=3',
         'uri_for_action returns correct uri for chain with many empty path parts' );
+    is( $context->uri_for_action( $complex_chained, [23,13], {q => 3} ),
+        'http://127.0.0.1/foo/chained/empty/23/13?q=3',
+        'uri_for_action returns correct uri for chain with many empty path parts with captures and args combined' );
 
     eval { $context->uri_for_action( '/does/not/exist' ) };
     like $@, qr{^Can't find action for path '/does/not/exist'},




More information about the Catalyst-commits mailing list