Fix for Catalyst::Action::REST
[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
85d9fce6 9 <form action="[%c.uri_for(c.action.reverse)%]">
10
b2ddf6d7 11=head1 DESCRIPTION
12
ac5c933b 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;
21
5fb12dbb 22has class => (is => 'rw');
23has namespace => (is => 'rw');
24has 'reverse' => (is => 'rw');
25has attributes => (is => 'rw');
26has name => (is => 'rw');
27has code => (is => 'rw');
059c085b 28
29no Moose;
30
2055d9ad 31use overload (
32
33 # Stringify to reverse for debug output etc.
34 q{""} => sub { shift->{reverse} },
35
36 # Codulate to execute to invoke the encapsulated action coderef
37 '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
38
39 # Make general $stuff still work
40 fallback => 1,
41
42);
43
44
45
059c085b 46no warnings 'recursion';
47
48#__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
fbcc39ad 49
b2ddf6d7 50sub dispatch { # Execute ourselves against a context
51 my ( $self, $c ) = @_;
049f82e2 52 return $c->execute( $self->class, $self );
b2ddf6d7 53}
fbcc39ad 54
b2ddf6d7 55sub execute {
56 my $self = shift;
059c085b 57 $self->code->(@_);
b2ddf6d7 58}
fbcc39ad 59
b2ddf6d7 60sub match {
61 my ( $self, $c ) = @_;
e5ecd5bc 62 #would it be unreasonable to store the number of arguments
63 #the action has as it's own attribute?
64 #it would basically eliminate the code below. ehhh. small fish
b2ddf6d7 65 return 1 unless exists $self->attributes->{Args};
66 my $args = $self->attributes->{Args}[0];
67 return 1 unless defined($args) && length($args);
68 return scalar( @{ $c->req->args } ) == $args;
69}
fbcc39ad 70
e5ecd5bc 71__PACKAGE__->meta->make_immutable;
72
b2ddf6d7 731;
fbcc39ad 74
b2ddf6d7 75__END__
4ab87e27 76
fbcc39ad 77=head1 METHODS
78
b5ecfcf0 79=head2 attributes
fbcc39ad 80
4ab87e27 81The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 82and so on. This determines how the action is dispatched to.
4ab87e27 83
b5ecfcf0 84=head2 class
b96f127f 85
b2ddf6d7 86Returns the class name where this action is defined.
4ab87e27 87
b5ecfcf0 88=head2 code
11bd4e3e 89
b2ddf6d7 90Returns a code reference to this action.
4ab87e27 91
b8f669f3 92=head2 dispatch( $c )
4ab87e27 93
b8f669f3 94Dispatch this action against a context
fbcc39ad 95
b8f669f3 96=head2 execute( $controller, $c, @args )
97
98Execute this action's coderef against a given controller with a given
99context and arguments
100
649fd1fa 101=head2 match( $c )
4ab87e27 102
649fd1fa 103Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 104Always returns true if Args is omitted.
4082e678 105
b5ecfcf0 106=head2 namespace
fbcc39ad 107
4ab87e27 108Returns the private namespace this action lives in.
109
b5ecfcf0 110=head2 reverse
6b239949 111
4ab87e27 112Returns the private path for this action.
113
b5ecfcf0 114=head2 name
fbcc39ad 115
4ab87e27 116returns the sub name of this action.
117
059c085b 118=head2 meta
119
120Provided by Moose
121
2f381252 122=head1 AUTHORS
fbcc39ad 123
2f381252 124Catalyst Contributors, see Catalyst.pm
fbcc39ad 125
126=head1 COPYRIGHT
127
128This program is free software, you can redistribute it and/or modify it under
129the same terms as Perl itself.
130
85d9fce6 131=cut