d55ca0075635a6c44f9821ba7275059d1f587d0a
[catagits/Catalyst-Runtime.git] / lib / Catalyst / DispatchType / Index.pm
1 package Catalyst::DispatchType::Index;
2
3 use Moose;
4 extends 'Catalyst::DispatchType';
5 use namespace::clean -except => 'meta';
6
7 =head1 NAME
8
9 Catalyst::DispatchType::Index - Index DispatchType
10
11 =head1 SYNOPSIS
12
13 See L<Catalyst::DispatchType>.
14
15 =head1 DESCRIPTION
16
17 Dispatch type managing behaviour for index pages.  For more information on
18 dispatch types, see:
19
20 =over 4
21
22 =item * L<Catalyst::Manual::Intro> for how they affect application authors
23
24 =item * L<Catalyst::DispatchType> for implementation information.
25
26 =back
27
28 =cut
29
30 has _actions => (
31     is => 'rw', isa => 'HashRef', default => sub { +{} }
32 );
33
34 =head1 METHODS
35
36 =head2 $self->match( $c, $path )
37
38 Check if there's an index action for a given path, and set it up to use it
39 if there is; only matches a full URI - if $c->req->args is already set
40 this DispatchType is guaranteed not to match.
41
42 =cut
43
44 sub match {
45     my ( $self, $c, $path ) = @_;
46     return if @{ $c->req->args };
47     my $result = $c->get_action( 'index', $path );
48
49     return 0 unless $result && exists $self->_actions->{ $result->reverse };
50
51     if ($result && $result->match($c)) {
52         $c->action($result);
53         $c->namespace( $result->namespace );
54         $c->req->action('index');
55         $c->req->match( $c->req->path );
56         return 1;
57     }
58     return 0;
59 }
60
61 =head2 $self->register( $c, $action )
62
63 Register an action with this DispatchType.
64
65 =cut
66
67 sub register {
68     my ( $self, $c, $action ) = @_;
69
70     $self->_actions->{ $action->reverse } = $action;
71
72     return 1;
73 }
74
75 =head2 $self->uri_for_action( $action, $captures )
76
77 get a URI part for an action; always returns undef is $captures is set
78 since index actions don't have captures
79
80 =cut
81
82 sub uri_for_action {
83     my ( $self, $action, $captures ) = @_;
84
85     return undef if @$captures;
86
87     return undef unless $action->name eq 'index';
88
89     return "/".$action->namespace;
90 }
91
92 =head1 AUTHORS
93
94 Catalyst Contributors, see Catalyst.pm
95
96 =head1 COPYRIGHT
97
98 This library is free software. You can redistribute it and/or modify it under
99 the same terms as Perl itself.
100
101 =cut
102
103 __PACKAGE__->meta->make_immutable;
104
105 1;