X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FComponent.pm;h=1ea719fd8068635460b1c29bc4cf56b88b76a6d5;hb=e75fd3e49d0b99efeabadae124329b671e4c8300;hp=101f29c19112e8c1c4a8837b4d797f97fb363285;hpb=cf8eab35bae767daf240ec91a97b1809fda012ea;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Component.pm b/lib/Catalyst/Component.pm index 101f29c..1ea719f 100644 --- a/lib/Catalyst/Component.pm +++ b/lib/Catalyst/Component.pm @@ -28,14 +28,18 @@ Catalyst::Component - Catalyst Component Base Class __PACKAGE__->config( foo => 'bar' ); + has foo => ( + is => 'ro', + ); + sub test { my $self = shift; - return $self->{foo}; + return $self->foo; } sub forward_to_me { my ( $self, $c ) = @_; - $c->response->output( $self->{foo} ); + $c->response->output( $self->foo ); } 1; @@ -46,7 +50,7 @@ Catalyst::Component - Catalyst Component Base Class # Or just methods print $c->comp('MyApp::Model::Something')->test; - print $c->comp('MyApp::Model::Something')->{foo}; + print $c->comp('MyApp::Model::Something')->foo; =head1 DESCRIPTION @@ -56,6 +60,13 @@ This is the universal base class for Catalyst components It provides you with a generic new() for component construction through Catalyst's component loader with config() support and a process() method placeholder. +B that calling C<< $self->config >> inside a component is strongly +not recommended - the correctly merged config should have already been +passed to the constructor and stored in attributes - accessing +the config accessor directly from an instance is likely to get the +wrong values (as it only holds the class wide config, not things loaded +from the config file!) + =cut __PACKAGE__->mk_classdata('_plugins'); @@ -63,14 +74,12 @@ __PACKAGE__->mk_classdata('_config'); has catalyst_component_name => ( is => 'ro' ); # Cannot be required => 1 as context # class @ISA component - HATE -# Make accessor callable as a class method, as we need to call setup_actions -# on the application class, which we don't have an instance of, ewwwww -# Also, naughty modules like Catalyst::View::JSON try to write to _everything_, +# Naughty modules like Catalyst::View::JSON try to write to _everything_, # so spit a warning, ignore that (and try to do the right thing anyway) here.. around catalyst_component_name => sub { my ($orig, $self) = (shift, shift); Carp::cluck("Tried to write to the catalyst_component_name accessor - is your component broken or just mad? (Write ignored - using default value.)") if scalar @_; - blessed($self) ? $self->$orig() || blessed($self) : $self; + return $self->$orig() || blessed($self); }; sub BUILDARGS { @@ -202,7 +211,7 @@ a Catalyst application has its own config hash. The component's config hash is merged with any config entry on the application for this component and passed to C (as mentioned -above at L). The common practice to access the merged +above at L). The recommended practice to access the merged config is to use a Moose attribute for each config entry on the receiving component.