Fixed Catalyst pod
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Base.pm
CommitLineData
fc7ec1d9 1package Catalyst::Base;
2
3use strict;
4use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
5use NEXT;
6
812a28c9 7__PACKAGE__->mk_classdata($_) for qw/_attr_cache _action_cache _config/;
8__PACKAGE__->_attr_cache( {} );
9__PACKAGE__->_action_cache( [] );
ac733264 10
61b1e958 11# note - see attributes(3pm)
ac733264 12sub MODIFY_CODE_ATTRIBUTES {
13 my ( $class, $code, @attrs ) = @_;
812a28c9 14 $class->_attr_cache->{$code} = [@attrs];
15 push @{ $class->_action_cache }, [ $code, [@attrs] ];
ac733264 16 return ();
17}
fc7ec1d9 18
812a28c9 19sub FETCH_CODE_ATTRIBUTES { $_[0]->_attr_cache->{ $_[1] } || () }
d70195d8 20
fc7ec1d9 21=head1 NAME
22
23Catalyst::Base - Catalyst Universal Base Class
24
25=head1 SYNOPSIS
26
27 # lib/MyApp/Model/Something.pm
28 package MyApp::Model::Something;
29
30 use base 'Catalyst::Base';
31
32 __PACKAGE__->config( foo => 'bar' );
33
34 sub test {
35 my $self = shift;
36 return $self->{foo};
37 }
38
39 sub forward_to_me {
40 my ( $self, $c ) = @_;
41 $c->response->output( $self->{foo} );
42 }
43
44 1;
45
46 # Methods can be a request step
47 $c->forward(qw/MyApp::Model::Something forward_to_me/);
fc7ec1d9 48
49 # Or just methods
50 print $c->comp('MyApp::Model::Something')->test;
51
52 print $c->comp('MyApp::Model::Something')->{foo};
53
54=head1 DESCRIPTION
55
56This is the universal base class for Catalyst components
57(Model/View/Controller).
58
59It provides you with a generic new() for instantiation through Catalyst's
60component loader with config() support and a process() method placeholder.
61
23f9d934 62=head1 METHODS
63
64=over 4
65
66=item new($c)
fc7ec1d9 67
68=cut
69
70sub new {
71 my ( $self, $c ) = @_;
a268a011 72
73 # Temporary fix, some components does not pass context to constructor
74 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
75
76 return $self->NEXT::new( { %{ $self->config }, %{ $arguments } } );
fc7ec1d9 77}
78
23f9d934 79# remember to leave blank lines between the consecutive =item's
80# otherwise the pod tools don't recognize the subsequent =items
81
82=item $c->config
83
84=item $c->config($hashref)
85
86=item $c->config($key, $value, ...)
fc7ec1d9 87
88=cut
89
90sub config {
91 my $self = shift;
92 $self->_config( {} ) unless $self->_config;
812a28c9 93 if (@_) {
c19e2f4a 94 my $config = @_ > 1 ? {@_} : $_[0];
fc7ec1d9 95 while ( my ( $key, $val ) = each %$config ) {
96 $self->_config->{$key} = $val;
97 }
98 }
99 return $self->_config;
100}
101
23f9d934 102=item $c->process()
fc7ec1d9 103
104=cut
105
d70195d8 106sub process {
107 die( ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process" );
108}
fc7ec1d9 109
4aff785c 110=item FETCH_CODE_ATTRIBUTES
111
112=item MODIFY_CODE_ATTRIBUTES
113
bea4160a 114=back
115
fc7ec1d9 116=head1 SEE ALSO
117
118L<Catalyst>.
119
120=head1 AUTHOR
121
122Sebastian Riedel, C<sri@cpan.org>
61b1e958 123Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 124
125=head1 COPYRIGHT
126
127This program is free software, you can redistribute it and/or modify it under
128the same terms as Perl itself.
129
130=cut
131
1321;