Merge branch 'expand_modules'
Florian Ragwitz [Thu, 4 Feb 2010 05:50:05 +0000 (05:50 +0000)]
expand_modules:
Allow models and components to specify the names of any components they generate
Branching to allow components to specify any modules they may have created

lib/Catalyst.pm
lib/Catalyst/Component.pm
t/aggregate/unit_core_component_generating.t [new file with mode: 0644]
t/lib/TestApp/Model/Generating.pm [new file with mode: 0644]

index 16510e5..24e45bd 100644 (file)
@@ -2239,8 +2239,11 @@ sub setup_components {
     }
 
     for my $component (@comps) {
-        $class->components->{ $component } = $class->setup_component($component);
-        for my $component ($class->expand_component_module( $component, $config )) {
+        my $instance = $class->components->{ $component } = $class->setup_component($component);
+        my @expanded_components = $instance->can('expand_modules')
+            ? $instance->expand_modules( $component, $config )
+            : $class->expand_component_module( $component, $config );
+        for my $component (@expanded_components) {
             next if $comps{$component};
             $class->_controller_init_base_classes($component); # Also cover inner packages
             $class->components->{ $component } = $class->setup_component($component);
index a279cd7..1b2d462 100644 (file)
@@ -5,6 +5,7 @@ use Class::MOP;
 use Class::MOP::Object;
 use Catalyst::Utils;
 use Class::C3::Adopt::NEXT;
+use Devel::InnerPackage ();
 use MRO::Compat;
 use mro 'c3';
 use Scalar::Util 'blessed';
@@ -147,6 +148,11 @@ sub process {
           . " did not override Catalyst::Component::process" );
 }
 
+sub expand_modules {
+    my ($class, $component) = @_;
+    return Devel::InnerPackage::list_packages( $component );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
@@ -205,6 +211,13 @@ when you forward to them. The default is an abstract method.
 Merges two hashes together recursively, giving right-hand precedence.
 Alias for the method in L<Catalyst::Utils>.
 
+=head2 $c->expand_modules( $setup_component_config )
+
+Return a list of extra components that this component has created. By default,
+it just looks for a list of inner packages of this component
+
+=cut
+
 =head1 OPTIONAL METHODS
 
 =head2 ACCEPT_CONTEXT($c, @args)
diff --git a/t/aggregate/unit_core_component_generating.t b/t/aggregate/unit_core_component_generating.t
new file mode 100644 (file)
index 0000000..a518fce
--- /dev/null
@@ -0,0 +1,10 @@
+use Test::More tests => 3;
+use strict;
+use warnings;
+
+use lib 't/lib';
+use TestApp;
+
+ok(TestApp->model('Generating'), 'knows about generating model');
+ok(TestApp->model('Generated'), 'knows about the generated model');
+is(TestApp->model('Generated')->foo, 'foo', 'can operate on generated model');
diff --git a/t/lib/TestApp/Model/Generating.pm b/t/lib/TestApp/Model/Generating.pm
new file mode 100644 (file)
index 0000000..24981e0
--- /dev/null
@@ -0,0 +1,22 @@
+package TestApp::Model::Generating;
+use Moose;
+extends 'Catalyst::Model';
+
+sub BUILD {
+    Class::MOP::Class->create(
+        'TestApp::Model::Generated' => (
+            methods => {
+                foo => sub { 'foo' }
+            }
+        )
+    );
+}
+
+sub expand_modules {
+    return ('TestApp::Model::Generated');
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;