no :PathPart -> :PathPart('subname')
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / ChildOf.pm
index 4be4967..2ab537e 100644 (file)
@@ -41,6 +41,7 @@ sub list {
                   ) {
         my $args = $endpoint->attributes->{Args}->[0];
         my @parts = (defined($args) ? (("*") x $args) : '...');
+        my @parents = ();
         my $parent = "DUMMY";
         my $curr = $endpoint;
         while ($curr) {
@@ -53,9 +54,23 @@ sub list {
             }
             $parent = $curr->attributes->{ChildOf}->[0];
             $curr = $self->{actions}{$parent};
+            unshift(@parents, $curr) if $curr;
         }
         next ENDPOINT unless $parent eq '/'; # skip dangling action
-        $paths->row(join('/', '', @parts), "/$endpoint");
+        my @rows;
+        foreach my $p (@parents) {
+            my $name = "/${p}";
+            if (my $cap = $p->attributes->{Captures}) {
+                $name .= ' ('.$cap->[0].')';
+            }
+            unless ($p eq $parents[0]) {
+                $name = "-> ${name}";
+            }
+            push(@rows, [ '', $name ]);
+        }
+        push(@rows, [ '', (@rows ? "=> " : '')."/${endpoint}" ]);
+        $rows[0][0] = join('/', '', @parts);
+        $paths->row(@$_) for @rows;
     }
 
     $c->log->debug( "Loaded Path Part actions:\n" . $paths->draw );
@@ -100,7 +115,8 @@ sub recurse_match {
     my $children = $self->{children_of}{$parent};
     return () unless $children;
     my @captures;
-    TRY: foreach my $try_part (sort length, keys %$children) {
+    TRY: foreach my $try_part (sort { length($a) <=> length($b) }
+                                   keys %$children) {
         my @parts = @$path_parts;
         if (length $try_part) { # test and strip PathPart
             next TRY unless
@@ -111,21 +127,13 @@ sub recurse_match {
         }
         my @try_actions = @{$children->{$try_part}};
         TRY_ACTION: foreach my $action (@try_actions) {
-            if (my $args_attr = $action->attributes->{Args}) {
-                # XXX alternative non-Args way to identify an endpoint?
-                {
-                    local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
-                    next TRY_ACTION unless $action->match($c);
-                }
-                push(@{$c->req->args}, @parts);
-                return [ $action ], [ ];
-            } else {
+            if (my $capture_attr = $action->attributes->{Captures}) {
                 my @captures;
                 my @parts = @parts; # localise
-                if (my $capture_attr = $action->attributes->{Captures}) {
-                    # strip Captures into list
-                    push(@captures, splice(@parts, 0, $capture_attr->[0]));
-                }
+
+                # strip Captures into list
+                push(@captures, splice(@parts, 0, $capture_attr->[0]));
+
                 # try the remaining parts against children of this action
                 my ($actions, $captures) = $self->recurse_match(
                                              $c, '/'.$action->reverse, \@parts
@@ -133,6 +141,13 @@ sub recurse_match {
                 if ($actions) {
                     return [ $action, @$actions ], [ @captures, @$captures ];
                 }
+            } else {
+                {
+                    local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
+                    next TRY_ACTION unless $action->match($c);
+                }
+                push(@{$c->req->args}, @parts);
+                return [ $action ], [ ];
             }
         }
     }
@@ -174,10 +189,10 @@ sub register {
 
     my @path_part = @{ $action->attributes->{PathPart} || [] };
 
-    my $part = '';
+    my $part = $action->name;
 
-    if (@path_part == 1) {
-        $part = (defined $path_part[0] ? $path_part[0] : $action->name);
+    if (@path_part == 1 && defined $path_part[0]) {
+        $part = $path_part[0];
     } elsif (@path_part > 1) {
         Catalyst::Exception->throw(
           "Multiple PathPart attributes not supported registering ${action}"
@@ -190,7 +205,7 @@ sub register {
 
     ($self->{actions} ||= {})->{'/'.$action->reverse} = $action;
 
-    if ($action->attributes->{Args}) {
+    unless ($action->attributes->{Captures}) {
         unshift(@{ $self->{endpoints} ||= [] }, $action);
     }