From: Matt S Trout Date: Sat, 3 Jun 2006 13:26:06 +0000 (+0000) Subject: added Catalyst::Component->action_for X-Git-Tag: 5.7099_04~546 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=2a5635138b6600a5fb2bf1b7d040d32582eddd24 added Catalyst::Component->action_for --- diff --git a/Changes b/Changes index d7c06b5..d9f8534 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ This file documents the revision history for Perl extension Catalyst. + - added Catalyst::Base->action_for('methodname') - checked and tested :Args multimethod dispatch - added ability to set action attributes from controller config - added merge_config_hashes() as a convenience method diff --git a/lib/Catalyst/Base.pm b/lib/Catalyst/Base.pm index cd14a4c..1fe7d94 100644 --- a/lib/Catalyst/Base.pm +++ b/lib/Catalyst/Base.pm @@ -60,6 +60,14 @@ sub _END : Private { return !@{ $c->error }; } +sub new { + my $self = shift; + my $app = $_[0]; + my $new = $self->NEXT::new(@_); + $new->{application} = $app; + return $new; +} + =head1 NAME Catalyst::Base - Catalyst Base Class @@ -77,6 +85,19 @@ dispatch of actions for controllers. =head1 METHODS +=head2 $self->action_for('name') + +Returns the Catalyst::Action object (if any) for a given method name in +this component. + +=cut + +sub action_for { + my ( $self, $name ) = @_; + my $app = ($self->isa('Catalyst') ? $self : $self->{application}); + return $app->dispatcher->get_action($name, $self->action_namespace); +} + =head2 $self->action_namespace($c) Returns the private namespace for actions in this component. Defaults to a value @@ -87,6 +108,9 @@ from the controller name (for e.g. MyApp::Controller::Foo::Bar becomes sub action_namespace { my ( $self, $c ) = @_; + unless ( $c ) { + $c = ($self->isa('Catalyst') ? $self : $self->{application}); + } my $hash = (ref $self ? $self : $self->config); # hate app-is-class return $hash->{namespace} if exists $hash->{namespace}; return Catalyst::Utils::class2prefix( ref($self) || $self, @@ -104,6 +128,9 @@ overriden from the "path" config key. sub path_prefix { my ( $self, $c ) = @_; + unless ( $c ) { + $c = ($self->isa('Catalyst') ? $self : $self->{application}); + } my $hash = (ref $self ? $self : $self->config); # hate app-is-class return $hash->{path} if exists $hash->{path}; return shift->action_namespace(@_); diff --git a/t/unit_core_action_for.t b/t/unit_core_action_for.t new file mode 100644 index 0000000..71772f8 --- /dev/null +++ b/t/unit_core_action_for.t @@ -0,0 +1,20 @@ +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/lib"; + +use Test::More; + +plan tests => 3; + +use_ok('TestApp'); + +is(TestApp->action_for('global_action')->code, TestApp->can('global_action'), + 'action_for on appclass ok'); + +is(TestApp->controller('Args')->action_for('args')->code, + TestApp::Controller::Args->can('args'), + 'action_for on controller ok');