Almost works generating controllers
t0m [Thu, 23 Apr 2009 00:27:46 +0000 (01:27 +0100)]
lib/CatalystX/ModelToControllerReflector.pm
lib/CatalystX/ModelsFromConfig.pm
lib/DynamicAppDemo.pm

index 43d5575..60c090c 100644 (file)
@@ -2,7 +2,8 @@ package CatalystX::ModelToControllerReflector;
 use Moose::Role;
 use namespace::clean -except => 'meta';
 
-with 'CatalystX::DynamicComponent';
+with 'CatalystX::DynamicComponent' 
+    => { alias => { _setup_dynamic_component => '_setup_dynamic_controller' } };
 
 requires 'setup_components';
 
@@ -13,13 +14,17 @@ sub _setup_dynamic_controllers {
     my @model_names = grep { /::Model::/ } keys %{ $app->components };
     
     foreach my $model_name (@model_names) {
-        $app->_setup_dynamic_controller( $model_name, $app->components->{$model_name} );
+        $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
     }
 }
 
-sub _setup_dynamic_controller {
-    my ($app, $model_name, $model_component) = @_;
-    warn($model_name);
+sub _reflect_model_to_controller {
+    my ( $app, $model_name, $model ) = @_;
+
+    my $controller_name = $model_name;
+    $controller_name =~ s/::Model::/::Controller::/;
+
+    $app->_setup_dynamic_controller( $controller_name );
 }
 
 1;
index 4e0cb3a..8bf1c9b 100644 (file)
@@ -8,6 +8,11 @@ requires qw/
     setup_component
 /;
 
+# Note method reaming - allows user to modify my setup_dynamic_component without being
+#                       forced to do it globally.
+with 'CatalystX::DynamicComponent' 
+    => { alias => { _setup_dynamic_component => '_setup_dynamic_model' } };
+
 after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };
 
 sub _setup_dynamic_models {
@@ -24,29 +29,5 @@ sub _setup_dynamic_models {
     }
 }
 
-sub _setup_dynamic_model {
-    my ($app, $name, $config) = @_;
-    
-    my $meta = Moose->init_meta( for_class => $name );
-    $meta->superclasses('Catalyst::Model');
-    
-    $meta->add_method( 
-
-      COMPONENT 
-            => sub {
-        my ($model_class_name, $app, $args) = @_;
-        
-        my $class = delete $args->{class};
-        Class::MOP::load_class($class);
-        
-        $class->new($args);
-    });
-
-    $meta->make_immutable;
-
-    my $instance = $app->setup_component($name);
-    $app->components->{ $name } = $instance;
-}
-
 1;
 
index c98596d..1c881f4 100644 (file)
@@ -9,8 +9,11 @@ use Catalyst qw/
 
 extends 'Catalyst';
 
+# Ordering important. :)
 with qw/
+    CatalystX::DynamicComponent
     CatalystX::ModelsFromConfig
+    CatalystX::ModelToControllerReflector
 /;
 
 our $VERSION = '0.01';