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