Less lies in the deprecated block comment in Dispatcher.pm as it's throwing people off
[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
b2ddf6d7 11=head1 DESCRIPTION
12
43c58153 13This class represents a Catalyst Action. You can access the object for the
b2ddf6d7 14currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
15for more information on how actions are dispatched. Actions are defined in
16L<Catalyst::Controller> subclasses.
17
18=cut
19
059c085b 20use Moose;
05b47f2e 21use Scalar::Util 'looks_like_number';
241edc9b 22with 'MooseX::Emulate::Class::Accessor::Fast';
05b47f2e 23use namespace::clean -except => 'meta';
241edc9b 24
5fb12dbb 25has class => (is => 'rw');
26has namespace => (is => 'rw');
27has 'reverse' => (is => 'rw');
28has attributes => (is => 'rw');
29has name => (is => 'rw');
30has code => (is => 'rw');
059c085b 31
2055d9ad 32use overload (
33
34 # Stringify to reverse for debug output etc.
35 q{""} => sub { shift->{reverse} },
36
37 # Codulate to execute to invoke the encapsulated action coderef
38 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
39
40 # Make general $stuff still work
41 fallback => 1,
42
43);
44
45
46
059c085b 47no warnings 'recursion';
48
49#__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
fbcc39ad 50
b2ddf6d7 51sub dispatch { # Execute ourselves against a context
52 my ( $self, $c ) = @_;
049f82e2 53 return $c->execute( $self->class, $self );
b2ddf6d7 54}
fbcc39ad 55
b2ddf6d7 56sub execute {
57 my $self = shift;
059c085b 58 $self->code->(@_);
b2ddf6d7 59}
fbcc39ad 60
b2ddf6d7 61sub match {
62 my ( $self, $c ) = @_;
e5ecd5bc 63 #would it be unreasonable to store the number of arguments
43c58153 64 #the action has as its own attribute?
e5ecd5bc 65 #it would basically eliminate the code below. ehhh. small fish
b2ddf6d7 66 return 1 unless exists $self->attributes->{Args};
67 my $args = $self->attributes->{Args}[0];
68 return 1 unless defined($args) && length($args);
69 return scalar( @{ $c->req->args } ) == $args;
70}
fbcc39ad 71
05b47f2e 72sub compare {
73 my ($a1, $a2) = @_;
74
cbe555e8 75 my ($a1_args) = @{ $a1->attributes->{Args} || [] };
76 my ($a2_args) = @{ $a2->attributes->{Args} || [] };
77
78 $_ = looks_like_number($_) ? $_ : ~0
79 for $a1_args, $a2_args;
80
81 return $a1_args <=> $a2_args;
05b47f2e 82}
83
e5ecd5bc 84__PACKAGE__->meta->make_immutable;
85
b2ddf6d7 861;
fbcc39ad 87
b2ddf6d7 88__END__
4ab87e27 89
fbcc39ad 90=head1 METHODS
91
b5ecfcf0 92=head2 attributes
fbcc39ad 93
4ab87e27 94The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 95and so on. This determines how the action is dispatched to.
4ab87e27 96
b5ecfcf0 97=head2 class
b96f127f 98
b2ddf6d7 99Returns the class name where this action is defined.
4ab87e27 100
b5ecfcf0 101=head2 code
11bd4e3e 102
b2ddf6d7 103Returns a code reference to this action.
4ab87e27 104
b8f669f3 105=head2 dispatch( $c )
4ab87e27 106
b8f669f3 107Dispatch this action against a context
fbcc39ad 108
b8f669f3 109=head2 execute( $controller, $c, @args )
110
111Execute this action's coderef against a given controller with a given
112context and arguments
113
649fd1fa 114=head2 match( $c )
4ab87e27 115
649fd1fa 116Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 117Always returns true if Args is omitted.
4082e678 118
91955398 119=head2 compare
120
cbe555e8 121Compares 2 actions based on the value of the C<Args> attribute, with no C<Args>
122having the highest precedence.
91955398 123
b5ecfcf0 124=head2 namespace
fbcc39ad 125
4ab87e27 126Returns the private namespace this action lives in.
127
b5ecfcf0 128=head2 reverse
6b239949 129
4ab87e27 130Returns the private path for this action.
131
b5ecfcf0 132=head2 name
fbcc39ad 133
4ab87e27 134returns the sub name of this action.
135
059c085b 136=head2 meta
137
138Provided by Moose
139
2f381252 140=head1 AUTHORS
fbcc39ad 141
2f381252 142Catalyst Contributors, see Catalyst.pm
fbcc39ad 143
144=head1 COPYRIGHT
145
536bee89 146This library is free software. You can redistribute it and/or modify it under
fbcc39ad 147the same terms as Perl itself.
148
85d9fce6 149=cut