Added index action and fixed get_action
Sebastian Riedel [Sat, 22 Oct 2005 11:06:25 +0000 (11:06 +0000)]
Changes
lib/Catalyst/DispatchType/Index.pm [new file with mode: 0644]
lib/Catalyst/Dispatcher.pm

diff --git a/Changes b/Changes
index 81d3220..37efc42 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 Tis file documents the revision history for Perl extension Catalyst.
 
 5.50
+        - Whole new dispatcher!
+        - Added index action
         - Added support for passing an IO::Handle object to $c->res->body.
           (Andrew Bramble)
         - Added a new welcome screen.
diff --git a/lib/Catalyst/DispatchType/Index.pm b/lib/Catalyst/DispatchType/Index.pm
new file mode 100644 (file)
index 0000000..3caeb5c
--- /dev/null
@@ -0,0 +1,54 @@
+package Catalyst::DispatchType::Index;
+
+use strict;
+use base qw/Catalyst::DispatchType/;
+
+=head1 NAME
+
+Catalyst::DispatchType::Index - Index DispatchType
+
+=head1 SYNOPSIS
+
+See L<Catalyst>.
+
+=head1 DESCRIPTION
+
+=head1 METHODS
+
+=over 4
+
+=item $self->match( $c, $path )
+
+=cut
+
+sub match {
+    my ( $self, $c, $path ) = @_;
+    return if $path =~ m!/!;
+    return if @{ $c->req->args };
+    my $result = @{ $c->get_action( 'index', $c->req->path ) || [] }[-1];
+
+    # Find default on namespace or super
+    if ($result) {
+        $c->action( $result->[0] );
+        $c->namespace( $c->req->path );
+        $c->req->action('index');
+        $c->req->match( $c->req->path );
+        return 1;
+    }
+    return 0;
+}
+
+=back
+
+=head1 AUTHOR
+
+Sebastian Riedel, C<sri@cpan.org>
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
+1;
index 1a1acbd..a9ad5a4 100644 (file)
@@ -7,6 +7,7 @@ use Catalyst::Utils;
 use Catalyst::Action;
 use Catalyst::ActionContainer;
 use Catalyst::DispatchType::Default;
+use Catalyst::DispatchType::Index;
 use Text::ASCIITable;
 use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
@@ -249,7 +250,7 @@ sub prepare_action {
 sub get_action {
     my ( $self, $c, $action, $namespace, $inherit ) = @_;
     return [] unless $action;
-    $namespace ||= '/';
+    $namespace ||= '';
     $inherit   ||= 0;
 
     my @match = $self->get_containers($namespace);
@@ -258,6 +259,14 @@ sub get_action {
 
     foreach my $child ( $inherit ? @match : $match[-1] ) {
         my $node = $child->actions;
+        unless ($inherit) {
+            $namespace = '' if $namespace eq '/';
+            my $reverse = $node->{$action}->reverse;
+            my $name    = $namespace
+              ? $namespace =~ /\/$/ ? "$namespace$action" : "$namespace/$action"
+              : $action;
+            last unless $name eq $reverse;
+        }
         push( @results, [ $node->{$action} ] ) if defined $node->{$action};
     }
     return \@results;
@@ -456,6 +465,7 @@ sub setup_actions {
     }
 
     # Default actions are always last in the chain
+    push @{ $self->dispatch_types }, Catalyst::DispatchType::Index->new;
     push @{ $self->dispatch_types }, Catalyst::DispatchType::Default->new;
 
     return unless $class->debug;