Reformatted documentation
[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::SimpleTable;
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 =head2 $self->list($c)
20
21 =cut
22
23 sub list {
24     my ( $self, $c ) = @_;
25     my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
26     for my $path ( sort keys %{ $self->{paths} } ) {
27         my $action = $self->{paths}->{$path};
28         $paths->row( "/$path", "/$action" );
29     }
30     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
31       if ( keys %{ $self->{paths} } );
32 }
33
34 =head2 $self->match( $c, $path )
35
36 =cut
37
38 sub match {
39     my ( $self, $c, $path ) = @_;
40
41     if ( my $action = $self->{paths}->{$path} ) {
42         $c->req->action($path);
43         $c->req->match($path);
44         $c->action($action);
45         $c->namespace( $action->namespace );
46         return 1;
47     }
48
49     return 0;
50 }
51
52 =head2 $self->register( $c, $action )
53
54 =cut
55
56 sub register {
57     my ( $self, $c, $action ) = @_;
58
59     my $attrs = $action->attributes;
60     my @register;
61
62     foreach my $r ( @{ $attrs->{Path} || [] } ) {
63         unless ($r) {
64             $r = $action->namespace;
65             $r = '' if $r eq '/';
66         }
67         elsif ( $r !~ m!^/! ) {    # It's a relative path
68             $r = $action->namespace . "/$r";
69         }
70         push( @register, $r );
71     }
72
73     if ( $attrs->{Global} || $attrs->{Absolute} ) {
74         push( @register, $action->name );    # Register sub name against root
75     }
76
77     if ( $attrs->{Local} || $attrs->{Relative} ) {
78         push( @register, join( '/', $action->namespace, $action->name ) );
79
80         # Register sub name as a relative path
81     }
82
83     $self->register_path( $c, $_, $action ) for @register;
84     return 1 if @register;
85     return 0;
86 }
87
88 =head2 $self->register_path($c, $path, $action)
89
90 =cut
91
92 sub register_path {
93     my ( $self, $c, $path, $action ) = @_;
94     $path =~ s!^/!!;
95     $self->{paths}{$path} = $action;
96 }
97
98 =head1 AUTHOR
99
100 Matt S Trout
101 Sebastian Riedel, C<sri@cpan.org>
102
103 =head1 COPYRIGHT
104
105 This program is free software, you can redistribute it and/or modify it under
106 the same terms as Perl itself.
107
108 =cut
109
110 1;