Fixed attrcontainer to ignore lvalue methods
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Regex.pm
CommitLineData
b96f127f 1package Catalyst::DispatchType::Regex;
2
3use strict;
6b239949 4use base qw/Catalyst::DispatchType::Path/;
8c113188 5use Text::SimpleTable;
b96f127f 6
2633d7dc 7=head1 NAME
b96f127f 8
2633d7dc 9Catalyst::DispatchType::Regex - Regex DispatchType
b96f127f 10
2633d7dc 11=head1 SYNOPSIS
12
13See L<Catalyst>.
14
15=head1 DESCRIPTION
16
17=head1 METHODS
18
19=over 4
20
a9cbd748 21=item $self->list($c)
22
23=cut
24
25sub list {
26 my ( $self, $c ) = @_;
8c113188 27 my $re = Text::SimpleTable->new( [ 36, 'Regex' ], [ 37, 'Private' ] );
a9cbd748 28 for my $regex ( @{ $self->{compiled} } ) {
a9cbd748 29 my $action = $regex->{action};
dcc61a75 30 $re->row( $regex->{path}, "/$action" );
a9cbd748 31 }
32 $c->log->debug( "Loaded Regex actions:\n" . $re->draw )
8c113188 33 if ( @{ $self->{compiled} } );
a9cbd748 34}
35
2633d7dc 36=item $self->match( $c, $path )
37
38=cut
39
40sub match {
41 my ( $self, $c, $path ) = @_;
42
43 return if $self->SUPER::match( $c, $path );
44
45 # Check path against plain text first
46
47 foreach my $compiled ( @{ $self->{compiled} || [] } ) {
b96f127f 48 if ( my @snippets = ( $path =~ $compiled->{re} ) ) {
2633d7dc 49 $c->req->action( $compiled->{path} );
b96f127f 50 $c->req->match($path);
2633d7dc 51 $c->req->snippets( \@snippets );
52 $c->action( $compiled->{action} );
11bd4e3e 53 $c->namespace( $compiled->{action}->namespace );
b96f127f 54 return 1;
55 }
56 }
57
58 return 0;
59}
60
2633d7dc 61=item $self->register( $c, $action )
62
63=cut
64
65sub register {
b96f127f 66 my ( $self, $c, $action ) = @_;
67 my $attrs = $action->attributes;
2633d7dc 68 my @register = map { @{ $_ || [] } } @{$attrs}{ 'Regex', 'Regexp' };
081def36 69 foreach
70 my $r ( map { @{ $_ || [] } } @{$attrs}{ 'LocalRegex', 'LocalRegexp' } )
71 {
72 unless ( $r =~ s/^\^// ) { $r = "(?:.*?)$r"; }
73 push( @register, '^' . $action->namespace . '/' . $r );
74 }
75
b96f127f 76 foreach my $r (@register) {
081def36 77 $self->register_path( $c, $r, $action );
78 $self->register_regex( $c, $r, $action );
b96f127f 79 }
80}
81
081def36 82=item $self->register_regex($c, $re, $action)
83
84=cut
85
86sub register_regex {
87 my ( $self, $c, $re, $action ) = @_;
88 push(
89 @{ $self->{compiled} }, # and compiled regex for us
90 {
91 re => qr#$re#,
92 action => $action,
93 path => $re,
94 }
95 );
96}
97
2633d7dc 98=back
99
100=head1 AUTHOR
101
102Matt S Trout
103Sebastian Riedel, C<sri@cpan.org>
104
105=head1 COPYRIGHT
106
107This program is free software, you can redistribute it and/or modify it under
108the same terms as Perl itself.
109
110=cut
111
b96f127f 1121;