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