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