a51f90a0e2d9f14e04b24bf6e4b7cfce94d446e6
[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 Class::C3;
19 use Moose;
20
21 use overload (
22
23     # Stringify to path part for tree search
24     q{""} => sub { shift->part },
25
26 );
27
28 has part => (is => 'rw', required => 1, lazy => 1, default => sub { {} });
29 has actions => (is => 'rw', required => 1, lazy => 1, default => sub { {} });
30
31 no Moose;
32
33 sub new {
34   my ($self, $params) = @_;
35   $params = { part => $params } unless ref $params;
36   $self->next::method($params);
37 }
38
39
40 sub get_action {
41     my ( $self, $name ) = @_;
42     return $self->actions->{$name} if defined $self->actions->{$name};
43     return;
44 }
45
46 sub add_action {
47     my ( $self, $action, $name ) = @_;
48     $name ||= $action->name;
49     $self->actions->{$name} = $action;
50 }
51
52 __PACKAGE__->meta->make_immutable;
53
54 1;
55
56 __END__
57
58 =head1 METHODS
59
60 =head2 new(\%data | $part)
61
62 Can be called with { part => $part, actions => \%actions } for full
63 construction or with just a part, which will result in an empty actions
64 hashref to be populated via add_action later
65
66 =head2 get_action($name)
67
68 Returns an action from this container based on the action name, or undef
69
70 =head2 add_action($action, [ $name ])
71
72 Adds an action, optionally providing a name to override $action->name
73
74 =head2 actions
75
76 Accessor to the actions hashref, containing all actions in this container.
77
78 =head2 part
79
80 Accessor to the path part this container resolves to. Also what the container
81 stringifies to.
82
83 =head2 meta
84
85 Provided by Moose
86
87 =head1 AUTHOR
88
89 Matt S. Trout
90
91 =head1 COPYRIGHT
92
93 This program is free software, you can redistribute it and/or modify it under
94 the same terms as Perl itself.
95
96 =cut
97
98 1;