- Added a bunch of comments to new dispatcher code
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
1 package Catalyst::DispatchType::Regex;
2
3 use strict;
4 use base qw/Catalyst::DispatchType::Path/;
5
6 sub prepare_action {
7     my ($self, $c, $path) = @_;
8
9     return if $self->SUPER::prepare_action($c, $path);
10         # Check path against plain text first
11
12     foreach my $compiled (@{$self->{compiled}||[]}) {
13         if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
14             $c->req->action($compiled->{path});
15             $c->req->match($path);
16             $c->req->snippets(\@snippets);
17             $c->action($compiled->{action});
18             $c->namespace($compiled->{action}->prefix);
19             return 1;
20         }
21     }
22
23     return 0;
24 }
25
26 sub register_action {
27     my ( $self, $c, $action ) = @_;
28     my $attrs = $action->attributes;
29     my @register = map { @{$_ || []} } @{$attrs}{'Regex', 'Regexp'};
30     foreach my $r (@register) {
31         $self->{paths}{$r} = $action; # Register path for superclass
32         push(@{$self->{compiled}},    # and compiled regex for us
33             {
34                 re => qr#$r#,
35                 action => $action,
36                 path => $r,
37             } );
38     }
39 }
40
41 1;