X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FDispatchType%2FRegex.pm;h=0b2264d0954bd55d3945227ddeb09184e0c5530c;hb=6323fda2e7ace0fc0aa06305c674957cedc6d025;hp=c40be54e5c30433450c3402708470065a7b6115b;hpb=ea0e58d9d85d93bf94c3ffa6b4d7b71b200bbdf1;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/DispatchType/Regex.pm b/lib/Catalyst/DispatchType/Regex.pm index c40be54..0b2264d 100644 --- a/lib/Catalyst/DispatchType/Regex.pm +++ b/lib/Catalyst/DispatchType/Regex.pm @@ -1,10 +1,12 @@ package Catalyst::DispatchType::Regex; -use strict; -use base qw/Catalyst::DispatchType::Path/; +use Moose; +extends 'Catalyst::DispatchType::Path'; use Text::SimpleTable; use Text::Balanced (); +has _compiled => (is => 'rw', isa => 'ArrayRef', required => 1, default => sub{[]}); + =head1 NAME Catalyst::DispatchType::Regex - Regex DispatchType @@ -25,30 +27,33 @@ Output a table of all regex actions, and their private equivalent. sub list { my ( $self, $c ) = @_; + my @regexes = @{ $self->_compiled }; + return unless @regexes; my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] ); - for my $regex ( @{ $self->{compiled} } ) { + for my $regex ( @regexes ) { my $action = $regex->{action}; $re->row( $regex->{path}, "/$action" ); } - $c->log->debug( "Loaded Regex actions:\n" . $re->draw ) - if ( @{ $self->{compiled} } ); + $c->log->debug( "Loaded Regex actions:\n" . $re->draw . "\n" ); } =head2 $self->match( $c, $path ) -Check path against compiled regexes, and set action to any matching -action. Returns 1 on success and 0 on failure. +Checks path against every compiled regex, and offers the action for any regex +which matches a chance to match the request. If it succeeds, sets action, +match and captures on $c->req and returns 1. If not, returns 0 without +altering $c. =cut -sub match { +override match => sub { my ( $self, $c, $path ) = @_; - return if $self->SUPER::match( $c, $path ); + return if super(); # Check path against plain text first - foreach my $compiled ( @{ $self->{compiled} || [] } ) { + foreach my $compiled ( @{ $self->_compiled } ) { if ( my @captures = ( $path =~ $compiled->{re} ) ) { next unless $compiled->{action}->match($c); $c->req->action( $compiled->{path} ); @@ -61,14 +66,14 @@ sub match { } return 0; -} +}; =head2 $self->register( $c, $action ) -Registers one or more regex actions for an action object.\ +Registers one or more regex actions for an action object. Also registers them as literal paths. -Returns 1 on if any regexps were registered. +Returns 1 if any regexps were registered. =cut @@ -88,15 +93,15 @@ sub register { =head2 $self->register_regex($c, $re, $action) -Register an individual regex on the action. Usually called from the -register action. +Register an individual regex on the action. Usually called from the +register method. =cut sub register_regex { my ( $self, $c, $re, $action ) = @_; push( - @{ $self->{compiled} }, # and compiled regex for us + @{ $self->_compiled }, # and compiled regex for us { re => qr#$re#, action => $action,