- sub foo :Path { ... } now works!
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
1 package Catalyst::DispatchType::Path;
2
3 use strict;
4 use base qw/Catalyst::DispatchType/;
5 use Text::ASCIITable;
6
7 =head1 NAME
8
9 Catalyst::DispatchType::Path - Path DispatchType
10
11 =head1 SYNOPSIS
12
13 See L<Catalyst>.
14
15 =head1 DESCRIPTION
16
17 =head1 METHODS
18
19 =over 4
20
21 =item $self->list($c)
22
23 =cut
24
25 sub list {
26     my ( $self, $c ) = @_;
27     my $paths = Text::ASCIITable->new;
28     $paths->setCols( 'Path', 'Private' );
29     $paths->setColWidth( 'Path',  36, 1 );
30     $paths->setColWidth( 'Private', 37, 1 );
31     for my $path ( sort keys %{ $self->{paths} } ) {
32         my $action = $self->{paths}->{$path};
33         $paths->addRow( "/$path", "/$action" );
34     }
35     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
36       if ( @{ $paths->{tbl_rows} } );
37 }
38
39 =item $self->match( $c, $path )
40
41 =cut
42
43 sub match {
44     my ( $self, $c, $path ) = @_;
45
46     if ( my $action = $self->{paths}->{$path} ) {
47         $c->req->action($path);
48         $c->req->match($path);
49         $c->action($action);
50         $c->namespace( $action->namespace );
51         return 1;
52     }
53
54     return 0;
55 }
56
57 =item $self->register( $c, $action )
58
59 =cut
60
61 sub register {
62     my ( $self, $c, $action ) = @_;
63
64     my $attrs = $action->attributes;
65     my @register;
66
67     foreach my $r ( @{ $attrs->{Path} || [] } ) {
68         unless ( $r ) {
69             $r = $action->namespace;
70         }
71         elsif ( $r !~ m!^/! ) {    # It's a relative path
72             $r = $action->namespace . "/$r";
73         }
74         push( @register, $r );
75     }
76
77     if ( $attrs->{Global} || $attrs->{Absolute} ) {
78         push( @register, $action->name );    # Register sub name against root
79     }
80
81     if ( $attrs->{Local} || $attrs->{Relative} ) {
82         push( @register, join( '/', $action->namespace, $action->name ) );
83
84         # Register sub name as a relative path
85     }
86
87     foreach my $r (@register) {
88         $r =~ s!^/!!;
89         $self->{paths}{$r} = $action;
90     }
91 }
92
93 =back
94
95 =head1 AUTHOR
96
97 Matt S Trout
98 Sebastian Riedel, C<sri@cpan.org>
99
100 =head1 COPYRIGHT
101
102 This program is free software, you can redistribute it and/or modify it under
103 the same terms as Perl itself.
104
105 =cut
106
107 1;