[Catalyst] Catalyst::component()
Matt S Trout
dbix-class at trout.me.uk
Tue Oct 4 19:15:04 CEST 2005
On Tue, Oct 04, 2005 at 09:00:01AM +0200, Bernhard Graf wrote:
> it would be very nice if this method gets fixed for the 5.50 release.
>
> See http://lists.rawmode.org/pipermail/catalyst/2005-August/001464.html
> and
> http://lists.rawmode.org/pipermail/catalyst/2005-September/001728.html
Since for the second time nobody's suggested changes to my RFC, I've gone
ahead and committed it. Anybody who wants it done differently, patches
welcome.
=== lib/Catalyst.pm
==================================================================
--- lib/Catalyst.pm (revision 2058)
+++ lib/Catalyst.pm (local)
@@ -592,15 +592,24 @@
my $name = shift;
- if ( my $component = $c->components->{$name} ) {
- return $component;
- }
+ my $appclass = ref $c || $c;
- else {
- for my $component ( keys %{ $c->components } ) {
- return $c->components->{$component} if $component =~ /$name/i;
+ my @names = ($name, "${appclass}::${name}",
+ map { "${appclass}::${_}::${name}" } qw/M V C/);
+
+ foreach my $try (@names) {
+
+ if ( exists $c->components->{$try} ) {
+
+ return $c->components->{$try};
}
}
+
+ foreach my $component ( keys %{ $c->components } ) {
+
+ return $c->components->{$component} if $component =~ /$name/i;
+ }
+
}
return sort keys %{ $c->components };
=== t/unit/core/component
==================================================================
--- t/unit/core/component (revision 2058)
+++ t/unit/core/component (local)
@@ -0,0 +1,28 @@
+use Test::More tests => 7;
+use strict;
+use warnings;
+
+use_ok('Catalyst');
+
+my @complist = map { "MyApp::$_"; } qw/C::Controller M::Model V::View/;
+
+{
+ package MyApp;
+
+ use base qw/Catalyst/;
+
+ __PACKAGE__->components({ map { ($_, $_) } @complist });
+}
+
+is(MyApp->comp('MyApp::V::View'), 'MyApp::V::View', 'Explicit return ok');
+
+is(MyApp->comp('C::Controller'), 'MyApp::C::Controller', 'Two-part return ok');
+
+is(MyApp->comp('Model'), 'MyApp::M::Model', 'Single part return ok');
+
+is(MyApp->comp('::M::'), 'MyApp::M::Model', 'Regex return ok');
+
+is_deeply([ MyApp->comp() ], \@complist, 'Empty return ok');
+
+is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok');
--
Matt S Trout Specialists in perl consulting, web development, and
Technical Director UNIX/Linux systems architecture and automation. Mail
Shadowcat Systems Ltd. mst (at) shadowcatsystems.co.uk for more information
+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +
More information about the Catalyst
mailing list