From: Matt S Trout Date: Mon, 27 Feb 2006 00:37:15 +0000 (+0000) Subject: Refactored path dispatch X-Git-Tag: 5.7099_04~691 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=5b707014dd4a5a756d22bcafc6c99e2b00a52d06 Refactored path dispatch --- diff --git a/lib/Catalyst/Base.pm b/lib/Catalyst/Base.pm index 42f28be..addc3ae 100644 --- a/lib/Catalyst/Base.pm +++ b/lib/Catalyst/Base.pm @@ -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, L. diff --git a/lib/Catalyst/DispatchType/Path.pm b/lib/Catalyst/DispatchType/Path.pm index ceea17b..9cd1a15 100644 --- a/lib/Catalyst/DispatchType/Path.pm +++ b/lib/Catalyst/DispatchType/Path.pm @@ -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; }