Refactored path dispatch
Matt S Trout [Mon, 27 Feb 2006 00:37:15 +0000 (00:37 +0000)]
lib/Catalyst/Base.pm
lib/Catalyst/DispatchType/Path.pm

index 42f28be..addc3ae 100644 (file)
@@ -112,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] } )
@@ -136,7 +136,7 @@ sub register_actions {
 }
 
 sub _parse_attrs {
-    my ( $self, @attrs ) = @_;
+    my ( $self, $c, $name, @attrs ) = @_;
     my %attributes;
     foreach my $attr (@attrs) {
 
@@ -148,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>.
index ceea17b..9cd1a15 100644 (file)
@@ -60,31 +60,10 @@ sub match {
 sub register {
     my ( $self, $c, $action ) = @_;
 
-    my $attrs = $action->attributes;
-    my @register;
-
-    foreach my $r ( @{ $attrs->{Path} || [] } ) {
-        unless ($r) {
-            $r = $action->namespace;
-            $r = '/' unless length $r;
-        }
-        elsif ( $r !~ m!^/! ) {    # It's a relative path
-            $r = $action->namespace . "/$r";
-        }
-        push( @register, $r );
-    }
-
-    if ( $attrs->{Global} || $attrs->{Absolute} ) {
-        push( @register, $action->name );    # Register sub name against root
-    }
-
-    if ( $attrs->{Local} || $attrs->{Relative} ) {
-        push( @register, join( '/', $action->namespace, $action->name ) );
-
-        # Register sub name as a relative path
-    }
+    my @register = @{$action->attributes->{Path}||[]};
 
     $self->register_path( $c, $_, $action ) for @register;
+
     return 1 if @register;
     return 0;
 }