From: Zbigniew Łukasiak Date: Wed, 18 Nov 2009 21:16:30 +0000 (+0000) Subject: view and model methods for the application class X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=fd794b27e97345df108cc06b7f46579771cb6fd4 view and model methods for the application class --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 6fe51c6..78d2629 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -291,6 +291,55 @@ sub controller { return $c->component( $c->action->class ); } +sub view { + my ( $c, $name, @args ) = @_; + + if( $name ) { + my @result = $c->_comp_search_prefixes( $name, qw/View V/ ); + return map { $c->_filter_component( $_, @args ) } @result if ref $name; + return $c->_filter_component( $result[ 0 ], @args ); + } + + return $c->view( $c->config->{default_view} ) + if $c->config->{default_view}; + my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/View V/); + + if( $rest ) { + $c->log->warn( 'Calling $c->view() will return a random view unless you specify one of:' ); + $c->log->warn( '* $c->config(default_view => "the name of the default view to use")' ); + $c->log->warn( '* $c->stash->{current_view} # the name of the view to use for this request' ); + $c->log->warn( '* $c->stash->{current_view_instance} # the instance of the view to use for this request' ); + $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' ); + } + + return $c->_filter_component( $comp ); +} + +sub model { + my ( $c, $name, @args ) = @_; + if( $name ) { + my @result = $c->_comp_search_prefixes( $name, qw/Model M/ ); + return map { $c->_filter_component( $_, @args ) } @result if ref $name; + return $c->_filter_component( $result[ 0 ], @args ); + } + + return $c->model( $c->config->{default_model} ) + if $c->config->{default_model}; + + my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/); + + if( $rest ) { + $c->log->warn( Carp::shortmess('Calling $c->model() will return a random model unless you specify one of:') ); + $c->log->warn( '* $c->config(default_model => "the name of the default model to use")' ); + $c->log->warn( '* $c->stash->{current_model} # the name of the model to use for this request' ); + $c->log->warn( '* $c->stash->{current_model_instance} # the instance of the model to use for this request' ); + $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' ); + } + + return $c->_filter_component( $comp ); +} + + sub _comp_search_prefixes { my $c = shift; return map $c->components->{ $_ }, $c->_comp_names_search_prefixes(@_);