Ok, I don't feel so dirty now. Still need to implement method filtering, such that...
t0m [Sun, 26 Apr 2009 19:16:13 +0000 (20:16 +0100)]
lib/CatalystX/DynamicComponent.pm
lib/CatalystX/ModelToControllerReflector.pm

index cb5a0d2..2e279d9 100644 (file)
@@ -22,7 +22,7 @@ role {
     my $name = $p->name;
     my $pre_immutable_hook = $p->pre_immutable_hook;
     method $name => sub {
-        my ($app, $name, $config) = @_;
+        my ($app, $name, $config, $methods) = @_;
 
         my $appclass = blessed($app) || $app;
         my $type = $name;
@@ -31,10 +31,17 @@ role {
 
         my $meta = Moose->init_meta( for_class => $name );
         $meta->superclasses('Catalyst::' . $type);
+        
         if ($p->has_custom_component_method) {
             $meta->add_method(COMPONENT => $p->COMPONENT);
         }
+        
         $app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook;
+        
+        $methods ||= {};
+        foreach my $name (keys %$methods) {
+            $meta->add_method($name => $methods->{$name});
+        }
         $meta->make_immutable;
 
         my $instance = $app->setup_component($name);
index d70bd49..686853a 100644 (file)
@@ -31,22 +31,17 @@ sub _reflect_model_to_controller {
     my $suffix = $model_name;
     $suffix =~ s/^.*::Model:://;
 
-    my $controller = $app->_setup_dynamic_controller( $controller_name, {} );
-    my $meta = $controller->meta;
-    $meta->make_mutable; # Dirty, I should build the class, add the methods, then
-                         # last of all make it a component. The only reason it works
-                         # like this is that I wrote the simple thing for the model
-                         # code, abstracted _just_ enough to make it fly with this
-                         # dirty hack, then stopped. EVERY TIME YOU DO THIS KITTENS DIE
-    my $methods = $model->meta->get_method_map;
-    foreach my $method_name (keys %$methods) {
-        $controller->meta->add_method(
+    my %controller_methods;
+    my $model_methods = $model->meta->get_method_map;
+    delete $model_methods->{new}; # FIXME..
+    delete $model_methods->{meta};
+    foreach my $method_name (keys %$model_methods) {
             # Note need to pass model name, as the method actually comes from
             # the underlying model class, not the Catalyst shim class we autogenerated.
-            $method_name => $app->generate_reflected_controller_action_method($suffix, $methods->{$method_name})    
-        );
+            $controller_methods{$method_name} = $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
     }
-    $meta->make_immutable;
+
+    $app->_setup_dynamic_controller( $controller_name, {}, \%controller_methods );
 }
 
 sub _setup_dynamic_controller_meta {