- Start of DispatchType refactor
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
1 package Catalyst::DispatchType::Regex;
2
3 use strict;
4 use base qw/Catalyst::DispatchType/;
5
6 sub prepare_action {
7     my ($self, $c, $path) = @_;
8
9     if ( my $action = $self->{paths}->{$path} ) {
10         $c->req->action($path);
11         $c->req->match($path);
12         $c->action($action);
13         $c->namespace($action->prefix);
14         return 1;
15     }
16
17     foreach my $compiled (@{$self->{compiled}||[]}) {
18         if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
19             $c->req->action($compiled->{path});
20             $c->req->match($path);
21             $c->req->snippets(\@snippets);
22             $c->action($compiled->{action});
23             $c->namespace($compiled->{action}->prefix);
24             return 1;
25         }
26     }
27
28     return 0;
29 }
30
31 sub register_action {
32     my ( $self, $c, $action ) = @_;
33     my $attrs = $action->attributes;
34     my @register = map { @{$_ || []} } @{$attrs}{'Regex', 'Regexp'};
35     foreach my $r (@register) {
36         $self->{paths}{$r} = $action;
37         push(@{$self->{compiled}},
38             {
39                 re => qr#$r#,
40                 action => $action,
41                 path => $r,
42             } );
43     }
44 }
45
46 1;