- Made Action accessor names more consistent (namespace -> class, prefix -> namespace)
[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 =~ m!^/! ) {    # It's a relative path
69             $r = $action->namespace . "/$r";
70         }
71         push( @register, $r );
72     }
73
74     if ( $attrs->{Global} || $attrs->{Absolute} ) {
75         push( @register, $action->name );    # Register sub name against root
76     }
77
78     if ( $attrs->{Local} || $attrs->{Relative} ) {
79         push( @register, join( '/', $action->namespace, $action->name ) );
80
81         # Register sub name as a relative path
82     }
83
84     foreach my $r (@register) {
85         $r =~ s!^/!!;
86         $self->{paths}{$r} = $action;
87     }
88 }
89
90 =back
91
92 =head1 AUTHOR
93
94 Matt S Trout
95 Sebastian Riedel, C<sri@cpan.org>
96
97 =head1 COPYRIGHT
98
99 This program is free software, you can redistribute it and/or modify it under
100 the same terms as Perl itself.
101
102 =cut
103
104 1;