finished the scheme matching and uri_for updates
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
CommitLineData
fbcc39ad 1package Catalyst::Action;
2
b2ddf6d7 3=head1 NAME
4
5Catalyst::Action - Catalyst Action
6
7=head1 SYNOPSIS
8
804fb55d 9 <form action="[%c.uri_for(c.action)%]">
85d9fce6 10
009b5b23 11 $c->forward( $action->private_path );
12
b2ddf6d7 13=head1 DESCRIPTION
14
43c58153 15This class represents a Catalyst Action. You can access the object for the
b2ddf6d7 16currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
17for more information on how actions are dispatched. Actions are defined in
18L<Catalyst::Controller> subclasses.
19
20=cut
21
059c085b 22use Moose;
05b47f2e 23use Scalar::Util 'looks_like_number';
241edc9b 24with 'MooseX::Emulate::Class::Accessor::Fast';
05b47f2e 25use namespace::clean -except => 'meta';
241edc9b 26
5fb12dbb 27has class => (is => 'rw');
28has namespace => (is => 'rw');
29has 'reverse' => (is => 'rw');
30has attributes => (is => 'rw');
31has name => (is => 'rw');
32has code => (is => 'rw');
009b5b23 33has private_path => (
34 reader => 'private_path',
35 isa => 'Str',
36 lazy => 1,
37 required => 1,
38 default => sub { '/'.shift->reverse },
39);
059c085b 40
2055d9ad 41use overload (
42
43 # Stringify to reverse for debug output etc.
44 q{""} => sub { shift->{reverse} },
45
46 # Codulate to execute to invoke the encapsulated action coderef
47 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
48
49 # Make general $stuff still work
50 fallback => 1,
51
52);
53
54
55
059c085b 56no warnings 'recursion';
57
b2ddf6d7 58sub dispatch { # Execute ourselves against a context
59 my ( $self, $c ) = @_;
049f82e2 60 return $c->execute( $self->class, $self );
b2ddf6d7 61}
fbcc39ad 62
b2ddf6d7 63sub execute {
64 my $self = shift;
059c085b 65 $self->code->(@_);
b2ddf6d7 66}
fbcc39ad 67
b2ddf6d7 68sub match {
60034b8c 69 my ( $self, $c ) = @_;
70 #would it be unreasonable to store the number of arguments
71 #the action has as its own attribute?
72 #it would basically eliminate the code below. ehhh. small fish
73 return 1 unless exists $self->attributes->{Args};
74 my $args = $self->attributes->{Args}[0];
75 return 1 unless defined($args) && length($args);
76 return scalar( @{ $c->req->args } ) == $args;
760d121e 77}
78
60034b8c 79sub match_captures { 1 }
fbcc39ad 80
05b47f2e 81sub compare {
82 my ($a1, $a2) = @_;
83
cbe555e8 84 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
85 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
86
18a9655c 87 $_ = looks_like_number($_) ? $_ : ~0
cbe555e8 88 for $a1_args, $a2_args;
89
90 return $a1_args <=> $a2_args;
05b47f2e 91}
92
0cff119a 93sub number_of_args {
94 my ( $self ) = @_;
95 return 0 unless exists $self->attributes->{Args};
96 return $self->attributes->{Args}[0];
97}
98
99sub number_of_captures {
100 my ( $self ) = @_;
101
102 return 0 unless exists $self->attributes->{CaptureArgs};
103 return $self->attributes->{CaptureArgs}[0] || 0;
104}
105
342d2169 106sub scheme {
107 return exists $_[0]->attributes->{Scheme} ? $_[0]->attributes->{Scheme}[0] : undef;
108}
109
ffca3e96 110sub list_extra_info {
111 my $self = shift;
112 return {
113 Args => $self->attributes->{Args}[0],
114 CaptureArgs => $self->number_of_captures,
115 }
116}
3c0da3ec 117
e5ecd5bc 118__PACKAGE__->meta->make_immutable;
119
b2ddf6d7 1201;
fbcc39ad 121
b2ddf6d7 122__END__
4ab87e27 123
fbcc39ad 124=head1 METHODS
125
b5ecfcf0 126=head2 attributes
fbcc39ad 127
4ab87e27 128The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 129and so on. This determines how the action is dispatched to.
4ab87e27 130
b5ecfcf0 131=head2 class
b96f127f 132
4d38cb07 133Returns the name of the component where this action is defined.
f9818250 134Derived by calling the L<catalyst_component_name|Catalyst::Component/catalyst_component_name>
fb0c5b21 135method on each component.
4ab87e27 136
b5ecfcf0 137=head2 code
11bd4e3e 138
b2ddf6d7 139Returns a code reference to this action.
4ab87e27 140
b8f669f3 141=head2 dispatch( $c )
4ab87e27 142
18a9655c 143Dispatch this action against a context.
fbcc39ad 144
b8f669f3 145=head2 execute( $controller, $c, @args )
146
147Execute this action's coderef against a given controller with a given
148context and arguments
149
649fd1fa 150=head2 match( $c )
4ab87e27 151
649fd1fa 152Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 153Always returns true if Args is omitted.
4082e678 154
760d121e 155=head2 match_captures ($c, $captures)
156
157Can be implemented by action class and action role authors. If the method
158exists, then it will be called with the request context and an array reference
159of the captures for this action.
160
161Returning true from this method causes the chain match to continue, returning
162makes the chain not match (and alternate, less preferred chains will be attempted).
163
164
91955398 165=head2 compare
166
cbe555e8 167Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
168having the highest precedence.
91955398 169
b5ecfcf0 170=head2 namespace
fbcc39ad 171
4ab87e27 172Returns the private namespace this action lives in.
173
b5ecfcf0 174=head2 reverse
6b239949 175
4ab87e27 176Returns the private path for this action.
177
009b5b23 178=head2 private_path
179
180Returns absolute private path for this action. Unlike C<reverse>, the
181C<private_path> of an action is always suitable for passing to C<forward>.
182
b5ecfcf0 183=head2 name
fbcc39ad 184
18a9655c 185Returns the sub name of this action.
4ab87e27 186
0cff119a 187=head2 number_of_args
188
189Returns the number of args this action expects. This is 0 if the action doesn't take any arguments and undef if it will take any number of arguments.
190
191=head2 number_of_captures
192
193Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
194
3c0da3ec 195=head2 list_extra_info
196
ffca3e96 197A HashRef of key-values that an action can provide to a debugging screen
3c0da3ec 198
342d2169 199=head2 scheme
200
201Any defined scheme for the action
202
059c085b 203=head2 meta
204
18a9655c 205Provided by Moose.
059c085b 206
2f381252 207=head1 AUTHORS
fbcc39ad 208
2f381252 209Catalyst Contributors, see Catalyst.pm
fbcc39ad 210
211=head1 COPYRIGHT
212
536bee89 213This library is free software. You can redistribute it and/or modify it under
fbcc39ad 214the same terms as Perl itself.
215
85d9fce6 216=cut