Separated execute and dispatch on Catalyst::Action
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Action.pm
1 package Catalyst::Action;
2
3 use strict;
4 use base qw/Class::Accessor::Fast/;
5
6 __PACKAGE__->mk_accessors(qw/class namespace reverse attributes name code/);
7
8 use overload (
9
10     # Stringify to reverse for debug output etc.
11     q{""} => sub { shift->{reverse} },
12
13     # Codulate to execute to invoke the encapsulated action coderef
14     '&{}' => sub { my $self = shift; sub { $self->execute(@_); }; },
15
16     # Make general $stuff still work
17     fallback => 1,
18
19 );
20
21 =head1 NAME
22
23 Catalyst::Action - Catalyst Action
24
25 =head1 SYNOPSIS
26
27 See L<Catalyst>.
28
29 =head1 DESCRIPTION
30
31 This class represents a Catalyst Action. You can access the object for the 
32 currently dispatched action via $c->action
33
34 =head1 METHODS
35
36 =head2 attributes
37
38 The sub attributes that are set for this action, like Local, Path, Private
39 and so on.
40
41 =head2 class
42
43 Returns the class name of this action
44
45 =head2 code
46
47 Returns a code reference to this action
48
49 =head2 dispatch( $c )
50
51 Dispatch this action against a context
52
53 =cut
54
55 sub dispatch {    # Execute ourselves against a context
56     my ( $self, $c ) = @_;
57     local $c->namespace = $self->namespace;
58     return $c->execute( $self->class, $self );
59 }
60
61 =head2 execute( $controller, $c, @args )
62
63 Execute this action's coderef against a given controller with a given
64 context and arguments
65
66 =cut
67
68 sub execute {
69   my $self = shift;
70   $self->{code}->(@_);
71 }
72
73 =head2 match( $c )
74
75 Check Args attribute, and makes sure number of args matches the setting.
76
77 =cut
78
79 sub match {
80     my ( $self, $c ) = @_;
81     return 1 unless exists $self->attributes->{Args};
82     return scalar( @{ $c->req->args } ) == $self->attributes->{Args}[0];
83 }
84
85 =head2 namespace
86
87 Returns the private namespace this action lives in.
88
89 =head2 reverse
90
91 Returns the private path for this action.
92
93 =head2 name
94
95 returns the sub name of this action.
96
97 =head1 AUTHOR
98
99 Matt S. Trout
100
101 =head1 COPYRIGHT
102
103 This program is free software, you can redistribute it and/or modify it under
104 the same terms as Perl itself.
105
106 =cut
107
108 1;