Added models/views/controllers methods
Matt S Trout [Sat, 1 Apr 2006 01:13:15 +0000 (01:13 +0000)]
Changes
lib/Catalyst.pm
t/unit_core_mvc.t

diff --git a/Changes b/Changes
index 66daa5d..b0d9f87 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 This file documents the revision history for Perl extension Catalyst.
 
 5.67   
+        - Added $c->models/views/controllers
         - Static::Simple: Unescape the URI path before looking for the file.
           This fixes issues with files that have spaces.
         - Looping and recursion tests plus a fix
index 554e959..34fd2d0 100644 (file)
@@ -399,6 +399,29 @@ sub _comp_prefixes {
     return $comp;
 }
 
+# Find possible names for a prefix 
+
+sub _comp_names {
+    my ( $c, @prefixes ) = @_;
+
+    my $appclass = ref $c || $c;
+
+    my @pre = map { "${appclass}::${_}::" } @prefixes;
+
+    my @names;
+
+    COMPONENT: foreach my $comp ($c->component) {
+        foreach my $p (@pre) {
+            if ($comp =~ s/^$p//) {
+                push(@names, $comp);
+                next COMPONENT;
+            }
+        }
+    }
+
+    return @names;
+}
+
 # Return a component if only one matches.
 sub _comp_singular {
     my ( $c, @prefixes ) = @_;
@@ -475,6 +498,17 @@ sub controller {
     return $c->component( $c->action->class );
 }
 
+=head2 $c->controllers
+
+Returns the available names which can be passed to $c->controller
+
+=cut
+
+sub controllers {
+    my ( $c ) = @_;
+    return $c->_comp_names(qw/Controller C/);
+}
+
 =head2 $c->model($name)
 
 Gets a L<Catalyst::Model> instance by name.
@@ -497,6 +531,17 @@ sub model {
 
 }
 
+=head2 $c->models
+
+Returns the available names which can be passed to $c->model
+
+=cut
+
+sub models {
+    my ( $c ) = @_;
+    return $c->_comp_names(qw/Model M/);
+}
+
 =head2 $c->view($name)
 
 Gets a L<Catalyst::View> instance by name.
@@ -518,6 +563,17 @@ sub view {
     return $c->_filter_component( $c->_comp_singular(qw/View V/) );
 }
 
+=head2 $c->views
+
+Returns the available names which can be passed to $c->view
+
+=cut
+
+sub views {
+    my ( $c ) = @_;
+    return $c->_comp_names(qw/View V/);
+}
+
 =head2 Class data and helper classes
 
 =head2 $c->config
index 4f964eb..d79653e 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 10;
+use Test::More tests => 13;
 use strict;
 use warnings;
 
@@ -17,7 +17,7 @@ push @complist,$thingie;
 
     use base qw/Catalyst/;
 
-    __PACKAGE__->components( { map { ( $_, $_ ) } @complist } );
+    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } );
 }
 
 is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' );
@@ -38,3 +38,15 @@ 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' );
+
+is_deeply( [ sort MyApp->views ],
+           [ qw/V View/ ],
+           'views ok' );
+
+is_deeply( [ sort MyApp->controllers ],
+           [ qw/C Controller Model::Dummy::Model/ ],
+           'controllers ok');
+
+is_deeply( [ sort MyApp->models ],
+           [ qw/Dummy::Model M Model Test::Object/ ],
+           'models ok');