mro compat stuff
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
1 package Catalyst::Action;
2
3 =head1 NAME
4
5 Catalyst::Action - Catalyst Action
6
7 =head1 SYNOPSIS
8
9     <form action="[%c.uri_for(c.action.reverse)%]">
10
11 =head1 DESCRIPTION
12
13 This class represents a Catalyst Action. You can access the object for the 
14 currently dispatched action via $c->action. See the L<Catalyst::Dispatcher>
15 for more information on how actions are dispatched. Actions are defined in
16 L<Catalyst::Controller> subclasses.
17
18 =cut
19
20 use MRO::Compat;
21 use mro 'c3';
22 use Moose;
23
24 has class => (is => 'rw');
25 has namespace => (is => 'rw');
26 has 'reverse' => (is => 'rw');
27 has attributes => (is => 'rw');
28 has name => (is => 'rw');
29 has code => (is => 'rw');
30
31 no Moose;
32
33 no warnings 'recursion';
34
35 #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
36
37 sub dispatch {    # Execute ourselves against a context
38     my ( $self, $c ) = @_;
39     #Moose todo: grrrrrr. this is no good. i don't know enough about it to
40     # debug it though. why can't we just call the accessor?
41     #local $c->{namespace} = $self->namespace;
42     #return $c->execute( $self->class, $self );
43
44     #believed to be equivalent:
45     my $orig = $c->namespace;
46     $c->namespace($self->namespace);
47     my $ret = $c->execute( $self->class, $self );
48     $c->namespace($orig);
49     return $ret;
50 }
51
52 sub execute {
53   my $self = shift;
54   $self->code->(@_);
55 }
56
57 sub match {
58     my ( $self, $c ) = @_;
59     #would it be unreasonable to store the number of arguments
60     #the action has as it's own attribute?
61     #it would basically eliminate the code below.  ehhh. small fish
62     return 1 unless exists $self->attributes->{Args};
63     my $args = $self->attributes->{Args}[0];
64     return 1 unless defined($args) && length($args);
65     return scalar( @{ $c->req->args } ) == $args;
66 }
67
68 __PACKAGE__->meta->make_immutable;
69
70 1;
71
72 __END__
73
74 =head1 METHODS
75
76 =head2 attributes
77
78 The sub attributes that are set for this action, like Local, Path, Private
79 and so on. This determines how the action is dispatched to.
80
81 =head2 class
82
83 Returns the class name where this action is defined.
84
85 =head2 code
86
87 Returns a code reference to this action.
88
89 =head2 dispatch( $c )
90
91 Dispatch this action against a context
92
93 =head2 execute( $controller, $c, @args )
94
95 Execute this action's coderef against a given controller with a given
96 context and arguments
97
98 =head2 match( $c )
99
100 Check Args attribute, and makes sure number of args matches the setting.
101 Always returns true if Args is omitted.
102
103 =head2 namespace
104
105 Returns the private namespace this action lives in.
106
107 =head2 reverse
108
109 Returns the private path for this action.
110
111 =head2 name
112
113 returns the sub name of this action.
114
115 =head2 meta
116
117 Provided by Moose
118
119 =head1 AUTHOR
120
121 Matt S. Trout
122
123 =head1 COPYRIGHT
124
125 This program is free software, you can redistribute it and/or modify it under
126 the same terms as Perl itself.
127
128 =cut