2e711d4d500a451ddc469b497140283def1c9e71
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ActionContainer.pm
1 package Catalyst::ActionContainer;
2
3 =head1 NAME
4
5 Catalyst::ActionContainer - Catalyst Action Container
6
7 =head1 SYNOPSIS
8
9 See L<Catalyst>.
10
11 =head1 DESCRIPTION
12
13 This is a container for actions. The dispatcher sets up a tree of these
14 to represent the various dispatch points in your application.
15
16 =cut
17
18 use Moose;
19 with 'MooseX::Emulate::Class::Accessor::Fast';
20
21 has part => (is => 'rw', required => 1);
22 has actions => (is => 'rw', required => 1, lazy => 1, default => sub { {} });
23
24 around new => sub {
25   my ($orig, $self, $params) = @_;
26   $orig->($self, (ref($params) ? $params :  { part => $params } ));
27 };
28
29 no Moose;
30
31 use overload (
32     # Stringify to path part for tree search
33     q{""} => sub { shift->part },
34 );
35
36 sub get_action {
37     my ( $self, $name ) = @_;
38     return $self->actions->{$name} if defined $self->actions->{$name};
39     return;
40 }
41
42 sub add_action {
43     my ( $self, $action, $name ) = @_;
44     $name ||= $action->name;
45     $self->actions->{$name} = $action;
46 }
47
48 __PACKAGE__->meta->make_immutable;
49
50 1;
51
52 __END__
53
54 =head1 METHODS
55
56 =head2 new(\%data | $part)
57
58 Can be called with { part => $part, actions => \%actions } for full
59 construction or with just a part, which will result in an empty actions
60 hashref to be populated via add_action later
61
62 =head2 get_action($name)
63
64 Returns an action from this container based on the action name, or undef
65
66 =head2 add_action($action, [ $name ])
67
68 Adds an action, optionally providing a name to override $action->name
69
70 =head2 actions
71
72 Accessor to the actions hashref, containing all actions in this container.
73
74 =head2 part
75
76 Accessor to the path part this container resolves to. Also what the container
77 stringifies to.
78
79 =head2 meta
80
81 Provided by Moose
82
83 =head1 AUTHORS
84
85 Catalyst Contributors, see Catalyst.pm
86
87 =head1 COPYRIGHT
88
89 This program is free software, you can redistribute it and/or modify it under
90 the same terms as Perl itself.
91
92 =cut
93
94 1;