1 package Catalyst::Action;
5 Catalyst::Action - Catalyst Action
9 <form action="[%c.uri_for(c.action)%]">
11 $c->forward( $action->private_path );
15 This class represents a Catalyst Action. You can access the object for the
16 currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
17 for more information on how actions are dispatched. Actions are defined in
18 L<Catalyst::Controller> subclasses.
23 use Scalar::Util 'looks_like_number';
24 use Moose::Util::TypeConstraints ();
25 with 'MooseX::Emulate::Class::Accessor::Fast';
26 use namespace::clean -except => 'meta';
28 has class => (is => 'rw');
29 has namespace => (is => 'rw');
30 has 'reverse' => (is => 'rw');
31 has attributes => (is => 'rw');
32 has name => (is => 'rw');
33 has code => (is => 'rw');
35 reader => 'private_path',
39 default => sub { '/'.shift->reverse },
42 has number_of_args => (
48 builder=>'_build_number_of_args');
50 sub _build_number_of_args {
52 return 0 unless exists $self->attributes->{Args};
53 if(!defined($self->attributes->{Args}[0])) {
54 # When its 'Args' that internal cue for 'unlimited'
56 } elsif(looks_like_number($self->attributes->{Args}[0])) {
57 # 'old school' numberd args (is allowed to be undef as well)
58 return $self->attributes->{Args}[0];
60 # new hotness named arg constraints
61 return $self->number_of_args_constraints;
65 has args_constraints => (
72 builder=>'_build_args_constraints',
74 has_args_constraints => 'count',
75 number_of_args_constraints => 'count',
78 sub _build_args_constraints {
80 my @arg_protos = @{$self->attributes->{Args}||[]};
82 return [] unless scalar(@arg_protos);
83 # If there is only one arg and it looks like a number
84 # we assume its 'classic' and the number is the number of
88 scalar(@arg_protos) == 1 &&
89 looks_like_number($arg_protos[0])
93 @args = map { Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) || die "$_ is not a constraint!" } @arg_protos;
101 # Stringify to reverse for debug output etc.
102 q{""} => sub { shift->{reverse} },
104 # Codulate to execute to invoke the encapsulated action coderef
105 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
107 # Make general $stuff still work
112 no warnings 'recursion';
114 sub dispatch { # Execute ourselves against a context
115 my ( $self, $c ) = @_;
116 return $c->execute( $self->class, $self );
125 my ( $self, $c ) = @_;
126 #would it be unreasonable to store the number of arguments
127 #the action has as its own attribute?
128 #it would basically eliminate the code below. ehhh. small fish
129 return 1 unless exists $self->attributes->{Args};
130 my $args = $self->attributes->{Args}[0];
131 return 1 unless defined($args) && length($args);
133 if($self->has_args_constraints) {
134 for my $i($#{ $c->req->args }) {
135 $self->args_constraints->[$i]->check($c->req->args->[$i]) || return 0;
139 return scalar( @{ $c->req->args } ) == $args;
143 sub match_captures { 1 }
148 # Wen there is no declared Args for Local and Path (and Default??) we
149 # say that means any number of args... If Args exists however we use
150 # the number of args as determined by inspecting the value of it.
152 my $a1_args = exists($a1->attributes->{Args}) ? $a1->number_of_args : ~0;
153 my $a2_args = exists($a2->attributes->{Args}) ? $a2->number_of_args : ~0;
155 # If we did have an Args but it was undefined value (:Args() or :Args), that
156 # is the cue for 'as many args as you like also...
158 $_ = defined($_) ? $_ : ~0
159 for $a1_args, $a2_args;
161 return $a1_args <=> $a2_args;
164 sub number_of_captures {
167 return 0 unless exists $self->attributes->{CaptureArgs};
168 return $self->attributes->{CaptureArgs}[0] || 0;
172 return exists $_[0]->attributes->{Scheme} ? $_[0]->attributes->{Scheme}[0] : undef;
175 sub list_extra_info {
178 Args => $self->attributes->{Args}[0],
179 CaptureArgs => $self->number_of_captures,
183 __PACKAGE__->meta->make_immutable;
193 The sub attributes that are set for this action, like Local, Path, Private
194 and so on. This determines how the action is dispatched to.
198 Returns the name of the component where this action is defined.
199 Derived by calling the L<catalyst_component_name|Catalyst::Component/catalyst_component_name>
200 method on each component.
204 Returns a code reference to this action.
206 =head2 dispatch( $c )
208 Dispatch this action against a context.
210 =head2 execute( $controller, $c, @args )
212 Execute this action's coderef against a given controller with a given
213 context and arguments
217 Check Args attribute, and makes sure number of args matches the setting.
218 Always returns true if Args is omitted.
220 =head2 match_captures ($c, $captures)
222 Can be implemented by action class and action role authors. If the method
223 exists, then it will be called with the request context and an array reference
224 of the captures for this action.
226 Returning true from this method causes the chain match to continue, returning
227 makes the chain not match (and alternate, less preferred chains will be attempted).
232 Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
233 having the highest precedence.
237 Returns the private namespace this action lives in.
241 Returns the private path for this action.
245 Returns absolute private path for this action. Unlike C<reverse>, the
246 C<private_path> of an action is always suitable for passing to C<forward>.
250 Returns the sub name of this action.
252 =head2 number_of_args
254 Returns 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.
256 =head2 number_of_captures
258 Returns the number of captures this action expects for L<Chained|Catalyst::DispatchType::Chained> actions.
260 =head2 list_extra_info
262 A HashRef of key-values that an action can provide to a debugging screen
266 Any defined scheme for the action
274 Catalyst Contributors, see Catalyst.pm
278 This library is free software. You can redistribute it and/or modify it under
279 the same terms as Perl itself.