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