Update dispatcher attributes to have sane defaults.
[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 Class::C3;
21 use Moose;
22
23 has class => (is => 'rw');
24 has namespace => (is => 'rw');
25 has 'reverse' => (is => 'rw');
26 has attributes => (is => 'rw');
27 has name => (is => 'rw');
28 has code => (is => 'rw');
29
30 no Moose;
31
32 no warnings 'recursion';
33
34 #__PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
35
36 use overload (
37
38     # Stringify to reverse for debug output etc.
39     q{""} => sub { shift->reverse() },
40
41     # Codulate to execute to invoke the encapsulated action coderef
42     '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
43
44     # Make general $stuff still work
45     fallback => 1,
46
47 );
48
49 sub dispatch {    # Execute ourselves against a context
50     my ( $self, $c ) = @_;
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?
53     #local $c->{namespace} = $self->namespace;
54     #return $c->execute( $self->class, $self );
55
56     #believed to be equivalent:
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;
62 }
63
64 sub execute {
65   my $self = shift;
66   $self->code->(@_);
67 }
68
69 sub match {
70     my ( $self, $c ) = @_;
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
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 }
79
80 __PACKAGE__->meta->make_immutable;
81
82 1;
83
84 __END__
85
86 =head1 METHODS
87
88 =head2 attributes
89
90 The sub attributes that are set for this action, like Local, Path, Private
91 and so on. This determines how the action is dispatched to.
92
93 =head2 class
94
95 Returns the class name where this action is defined.
96
97 =head2 code
98
99 Returns a code reference to this action.
100
101 =head2 dispatch( $c )
102
103 Dispatch this action against a context
104
105 =head2 execute( $controller, $c, @args )
106
107 Execute this action's coderef against a given controller with a given
108 context and arguments
109
110 =head2 match( $c )
111
112 Check Args attribute, and makes sure number of args matches the setting.
113 Always returns true if Args is omitted.
114
115 =head2 namespace
116
117 Returns the private namespace this action lives in.
118
119 =head2 reverse
120
121 Returns the private path for this action.
122
123 =head2 name
124
125 returns the sub name of this action.
126
127 =head2 meta
128
129 Provided by Moose
130
131 =head1 AUTHOR
132
133 Matt S. Trout
134
135 =head1 COPYRIGHT
136
137 This program is free software, you can redistribute it and/or modify it under
138 the same terms as Perl itself.
139
140 =cut