Refactored Regex actions
[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 use URI;
7
8 =head1 NAME
9
10 Catalyst::DispatchType::Path - Path DispatchType
11
12 =head1 SYNOPSIS
13
14 See L<Catalyst>.
15
16 =head1 DESCRIPTION
17
18 =head1 METHODS
19
20 =head2 $self->list($c)
21
22 =cut
23
24 sub list {
25     my ( $self, $c ) = @_;
26     my $paths = Text::SimpleTable->new( [ 36, 'Path' ], [ 37, 'Private' ] );
27     for my $path ( sort keys %{ $self->{paths} } ) {
28         my $action = $self->{paths}->{$path};
29         $path = "/$path" unless $path eq '/';
30         $paths->row( "$path", "/$action" );
31     }
32     $c->log->debug( "Loaded Path actions:\n" . $paths->draw )
33       if ( keys %{ $self->{paths} } );
34 }
35
36 =head2 $self->match( $c, $path )
37
38 =cut
39
40 sub match {
41     my ( $self, $c, $path ) = @_;
42
43     $path ||= '/';
44     if ( my $action = $self->{paths}->{$path} ) {
45         return 0 unless $action->match($c);
46         $c->req->action($path);
47         $c->req->match($path);
48         $c->action($action);
49         $c->namespace( $action->namespace );
50         return 1;
51     }
52
53     return 0;
54 }
55
56 =head2 $self->register( $c, $action )
57
58 =cut
59
60 sub register {
61     my ( $self, $c, $action ) = @_;
62
63     my @register = @{ $action->attributes->{Path} || [] };
64
65     $self->register_path( $c, $_, $action ) for @register;
66
67     return 1 if @register;
68     return 0;
69 }
70
71 =head2 $self->register_path($c, $path, $action)
72
73 =cut
74
75 sub register_path {
76     my ( $self, $c, $path, $action ) = @_;
77     $path =~ s!^/!!;
78     $path = '/' unless length $path;
79     $path = URI->new($path)->canonical;
80
81     $self->{paths}{$path} = $action;
82 }
83
84 =head1 AUTHOR
85
86 Matt S Trout
87 Sebastian Riedel, C<sri@cpan.org>
88
89 =head1 COPYRIGHT
90
91 This program is free software, you can redistribute it and/or modify it under
92 the same terms as Perl itself.
93
94 =cut
95
96 1;