make class data unlock and relock classes if immutable
[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
31no warnings 'recursion';
32
33#__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
fbcc39ad 34
b2ddf6d7 35sub dispatch { # Execute ourselves against a context
36 my ( $self, $c ) = @_;
e63bdf38 37 #Moose todo: grrrrrr. this is no good. i don't know enough about it to
38 # debug it though. why can't we just call the accessor?
6680c772 39 #local $c->{namespace} = $self->namespace;
40 #return $c->execute( $self->class, $self );
e63bdf38 41
42 #believed to be equivalent:
6680c772 43 my $orig = $c->namespace;
44 $c->namespace($self->namespace);
45 my $ret = $c->execute( $self->class, $self );
46 $c->namespace($orig);
47 return $ret;
b2ddf6d7 48}
fbcc39ad 49
b2ddf6d7 50sub execute {
51 my $self = shift;
059c085b 52 $self->code->(@_);
b2ddf6d7 53}
fbcc39ad 54
b2ddf6d7 55sub match {
56 my ( $self, $c ) = @_;
e5ecd5bc 57 #would it be unreasonable to store the number of arguments
58 #the action has as it's own attribute?
59 #it would basically eliminate the code below. ehhh. small fish
b2ddf6d7 60 return 1 unless exists $self->attributes->{Args};
61 my $args = $self->attributes->{Args}[0];
62 return 1 unless defined($args) && length($args);
63 return scalar( @{ $c->req->args } ) == $args;
64}
fbcc39ad 65
e5ecd5bc 66__PACKAGE__->meta->make_immutable;
67
b2ddf6d7 681;
fbcc39ad 69
b2ddf6d7 70__END__
4ab87e27 71
fbcc39ad 72=head1 METHODS
73
b5ecfcf0 74=head2 attributes
fbcc39ad 75
4ab87e27 76The sub attributes that are set for this action, like Local, Path, Private
b2ddf6d7 77and so on. This determines how the action is dispatched to.
4ab87e27 78
b5ecfcf0 79=head2 class
b96f127f 80
b2ddf6d7 81Returns the class name where this action is defined.
4ab87e27 82
b5ecfcf0 83=head2 code
11bd4e3e 84
b2ddf6d7 85Returns a code reference to this action.
4ab87e27 86
b8f669f3 87=head2 dispatch( $c )
4ab87e27 88
b8f669f3 89Dispatch this action against a context
fbcc39ad 90
b8f669f3 91=head2 execute( $controller, $c, @args )
92
93Execute this action's coderef against a given controller with a given
94context and arguments
95
649fd1fa 96=head2 match( $c )
4ab87e27 97
649fd1fa 98Check Args attribute, and makes sure number of args matches the setting.
b2ddf6d7 99Always returns true if Args is omitted.
4082e678 100
b5ecfcf0 101=head2 namespace
fbcc39ad 102
4ab87e27 103Returns the private namespace this action lives in.
104
b5ecfcf0 105=head2 reverse
6b239949 106
4ab87e27 107Returns the private path for this action.
108
b5ecfcf0 109=head2 name
fbcc39ad 110
4ab87e27 111returns the sub name of this action.
112
059c085b 113=head2 meta
114
115Provided by Moose
116
fbcc39ad 117=head1 AUTHOR
118
119Matt S. Trout
120
121=head1 COPYRIGHT
122
123This program is free software, you can redistribute it and/or modify it under
124the same terms as Perl itself.
125
85d9fce6 126=cut