Refactored path dispatch
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
index a8cc036..addc3ae 100644 (file)
@@ -80,7 +80,8 @@ Catalyst Base Class
 
 sub action_namespace {
     my ( $self, $c ) = @_;
-    return Catalyst::Utils::class2prefix( ref $self,
+    return $self->config->{namespace} if exists $self->config->{namespace};
+    return Catalyst::Utils::class2prefix( ref($self) || $self,
         $c->config->{case_sensitive} )
       || '';
 }
@@ -111,7 +112,7 @@ sub register_actions {
         my $code   = $cache->[0];
         my $method = $methods{$code};
         next unless $method;
-        my $attrs = $self->_parse_attrs( @{ $cache->[1] } );
+        my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
         if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
             $c->log->debug( 'Bad action definition "'
                   . join( ' ', @{ $cache->[1] } )
@@ -135,7 +136,7 @@ sub register_actions {
 }
 
 sub _parse_attrs {
-    my ( $self, @attrs ) = @_;
+    my ( $self, $c, $name, @attrs ) = @_;
     my %attributes;
     foreach my $attr (@attrs) {
 
@@ -147,12 +148,42 @@ sub _parse_attrs {
             if ( defined $value ) {
                 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
             }
+            my $meth = "_parse_${key}_attr";
+            if ( $self->can($meth) ) {
+                ( $key, $value ) = $self->$meth( $c, $name, $value );
+            }
             push( @{ $attributes{$key} }, $value );
         }
     }
     return \%attributes;
 }
 
+sub _parse_Global_attr {
+    my ( $self, $c, $name, $value ) = @_;
+    return $self->_parse_Path_attr( $c, $name, "/$name" );
+}
+
+*_parse_Absolute_attr = \&_parse_Global_attr;
+
+sub _parse_Local_attr {
+    my ( $self, $c, $name, $value ) = @_;
+    return $self->_parse_Path_attr( $c, $name, $name );
+}
+
+*_parse_Relative_attr = \&_parse_Local_attr;
+
+sub _parse_Path_attr {
+    my ( $self, $c, $name, $value ) = @_;
+    $value ||= '';
+    if ( $value =~ m!^/! ) {
+        return ( 'Path', $value );
+    } elsif ( length $value ) {
+        return ( 'Path', join( '/', $self->action_namespace($c), $value ) );
+    } else {
+        return ( 'Path', $self->action_namespace($c) );
+    }
+}
+
 =head1 SEE ALSO
 
 L<Catalyst>, L<Catalyst::Controller>.