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