From: Sebastian Riedel Date: Sun, 6 Nov 2005 19:15:57 +0000 (+0000) Subject: Added $c->controller, $c->model and $c->view shortcuts X-Git-Tag: 5.7099_04~1008 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=af3ff00e4738b2e87d189e79fd12739315711ab3 Added $c->controller, $c->model and $c->view shortcuts --- diff --git a/Changes b/Changes index 49639e8..70ce52e 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Tis file documents the revision history for Perl extension Catalyst. 5.5 + - Added $c->controller, $c->model and $c->view shortcuts - Switched to Text::SimpleTable 5.49_03 2005-11-03 12:00:00 diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 323ff5b..702bd8a 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -219,6 +219,23 @@ sub component { Returns a hashref containing your applications settings. +=cut + +=item $c->controller($name) + +Get a L instance by name. + + $c->controller('Foo')->do_stuff; + +=cut + +sub controller { + my ( $c, $name ) = @_; + my $controller = $c->comp("Controller::$name"); + return $controller if $controller; + return $c->comp("C::$name"); +} + =item debug Overload to enable debug messages. @@ -257,6 +274,21 @@ from the function. sub forward { my $c = shift; $c->dispatcher->forward( $c, @_ ) } +=item $c->model($name) + +Get a L instance by name. + + $c->model('Foo')->do_stuff; + +=cut + +sub model { + my ( $c, $name ) = @_; + my $model = $c->comp("Model::$name"); + return $model if $model; + return $c->comp("M::$name"); +} + =item $c->namespace Accessor to the namespace of the current action @@ -557,6 +589,21 @@ sub stash { return $c->{stash}; } +=item $c->view($name) + +Get a L instance by name. + + $c->view('Foo')->do_stuff; + +=cut + +sub view { + my ( $c, $name ) = @_; + my $view = $c->comp("View::$name"); + return $view if $view; + return $c->comp("V::$name"); +} + =item $c->welcome_message Returns the Catalyst welcome HTML page. @@ -1542,13 +1589,15 @@ sub setup_log { unless ( $class->log ) { $class->log( Catalyst::Log->new ); } - + my $app_flag = Catalyst::Utils::class2env($class) . '_DEBUG'; - if ( ( defined( $ENV{CATALYST_DEBUG} ) || - defined( $ENV{ $app_flag } ) ) ? - ( $ENV{CATALYST_DEBUG} || $ENV{ $app_flag } ) : - $debug ) { + if ( + ( defined( $ENV{CATALYST_DEBUG} ) || defined( $ENV{$app_flag} ) ) + ? ( $ENV{CATALYST_DEBUG} || $ENV{$app_flag} ) + : $debug + ) + { no strict 'refs'; *{"$class\::debug"} = sub { 1 }; $class->log->debug('Debug messages enabled'); diff --git a/t/unit/core/mvc.t b/t/unit/core/mvc.t new file mode 100644 index 0000000..f96002b --- /dev/null +++ b/t/unit/core/mvc.t @@ -0,0 +1,31 @@ +use Test::More tests => 7; +use strict; +use warnings; + +use_ok('Catalyst'); + +my @complist = + map { "MyApp::$_"; } + qw/C::Controller M::Model V::View Controller::C Model::M View::V/; + +{ + + package MyApp; + + use base qw/Catalyst/; + + __PACKAGE__->components( { map { ( $_, $_ ) } @complist } ); +} + +is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' ); + +is( MyApp->controller('Controller'), + 'MyApp::C::Controller', 'C::Controller ok' ); + +is( MyApp->model('Model'), 'MyApp::M::Model', 'M::Model ok' ); + +is( MyApp->view('V'), 'MyApp::View::V', 'View::V ok' ); + +is( MyApp->controller('C'), 'MyApp::Controller::C', 'Controller::C ok' ); + +is( MyApp->model('M'), 'MyApp::Model::M', 'Model::M ok' );