Refactored Regex actions
Matt S Trout [Mon, 27 Feb 2006 01:41:13 +0000 (01:41 +0000)]
lib/Catalyst/Base.pm
lib/Catalyst/DispatchType/Path.pm
lib/Catalyst/DispatchType/Regex.pm

index addc3ae..7536dc6 100644 (file)
@@ -86,6 +86,12 @@ sub action_namespace {
       || '';
 }
 
+=head2 $self->path_prefix($c)
+
+=cut
+
+sub path_prefix { shift->action_namespace(@_); }
+
 =head2 $self->register_actions($c)
 
 =cut
@@ -163,27 +169,44 @@ sub _parse_Global_attr {
     return $self->_parse_Path_attr( $c, $name, "/$name" );
 }
 
-*_parse_Absolute_attr = \&_parse_Global_attr;
+sub _parse_Absolute_attr { shift->_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_Relative_attr { shift->_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) );
     }
+    elsif ( length $value ) {
+        return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
+    }
+    else {
+        return ( 'Path', $self->path_prefix($c) );
+    }
+}
+
+sub _parse_Regex_attr {
+    my ( $self, $c, $name, $value ) = @_;
+    return ( 'Regex', $value );
 }
 
+sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
+
+sub _parse_LocalRegex_attr {
+    my ( $self, $c, $name, $value ) = @_;
+    unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
+    return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
+}
+
+sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
+
 =head1 SEE ALSO
 
 L<Catalyst>, L<Catalyst::Controller>.
index 9cd1a15..bd74c79 100644 (file)
@@ -60,7 +60,7 @@ sub match {
 sub register {
     my ( $self, $c, $action ) = @_;
 
-    my @register = @{$action->attributes->{Path}||[]};
+    my @register = @{ $action->attributes->{Path} || [] };
 
     $self->register_path( $c, $_, $action ) for @register;
 
@@ -77,6 +77,7 @@ sub register_path {
     $path =~ s!^/!!;
     $path = '/' unless length $path;
     $path = URI->new($path)->canonical;
+
     $self->{paths}{$path} = $action;
 }
 
index 49f7636..520e038 100644 (file)
@@ -64,18 +64,13 @@ sub match {
 sub register {
     my ( $self, $c, $action ) = @_;
     my $attrs = $action->attributes;
-    my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' };
-    foreach
-      my $r ( map { @{ $_ || [] } } @{$attrs}{ 'LocalRegex', 'LocalRegexp' } )
-    {
-        unless ( $r =~ s/^\^// ) { $r = "(?:.*?)$r"; }
-        push( @register, '^' . $action->namespace . '/' . $r );
-    }
+    my @register = @{ $attrs->{'Regex'} || [] };
 
     foreach my $r (@register) {
         $self->register_path( $c, $r, $action );
         $self->register_regex( $c, $r, $action );
     }
+
     return 1 if @register;
     return 0;
 }