- Refactored get_action into get_action and get_actions
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Path.pm
CommitLineData
6b239949 1package Catalyst::DispatchType::Path;
2
3use strict;
4use base qw/Catalyst::DispatchType/;
a9cbd748 5use Text::ASCIITable;
6b239949 6
2633d7dc 7=head1 NAME
8
9Catalyst::DispatchType::Path - Path DispatchType
10
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 ) = @_;
27 my $paths = Text::ASCIITable->new;
2f484ed5 28 $paths->setCols( 'Path', 'Private' );
784ab0e4 29 $paths->setColWidth( 'Path', 36, 1 );
a9cbd748 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
2633d7dc 39=item $self->match( $c, $path )
40
41=cut
42
43sub match {
44 my ( $self, $c, $path ) = @_;
6b239949 45
46 if ( my $action = $self->{paths}->{$path} ) {
47 $c->req->action($path);
48 $c->req->match($path);
49 $c->action($action);
11bd4e3e 50 $c->namespace( $action->namespace );
6b239949 51 return 1;
52 }
53
54 return 0;
55}
56
2633d7dc 57=item $self->register( $c, $action )
58
59=cut
60
61sub register {
6b239949 62 my ( $self, $c, $action ) = @_;
22f3a8dd 63
6b239949 64 my $attrs = $action->attributes;
65 my @register;
22f3a8dd 66
2633d7dc 67 foreach my $r ( @{ $attrs->{Path} || [] } ) {
b74baa06 68 unless ( $r ) {
69 $r = $action->namespace;
70 }
71 elsif ( $r !~ m!^/! ) { # It's a relative path
11bd4e3e 72 $r = $action->namespace . "/$r";
6b239949 73 }
2633d7dc 74 push( @register, $r );
6b239949 75 }
76
2633d7dc 77 if ( $attrs->{Global} || $attrs->{Absolute} ) {
78 push( @register, $action->name ); # Register sub name against root
6b239949 79 }
80
2633d7dc 81 if ( $attrs->{Local} || $attrs->{Relative} ) {
11bd4e3e 82 push( @register, join( '/', $action->namespace, $action->name ) );
2633d7dc 83
84 # Register sub name as a relative path
6b239949 85 }
86
87 foreach my $r (@register) {
88 $r =~ s!^/!!;
89 $self->{paths}{$r} = $action;
90 }
91}
92
2633d7dc 93=back
94
95=head1 AUTHOR
96
97Matt S Trout
98Sebastian Riedel, C<sri@cpan.org>
99
100=head1 COPYRIGHT
101
102This program is free software, you can redistribute it and/or modify it under
103the same terms as Perl itself.
104
105=cut
106
6b239949 1071;