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