Add more tests for DynamicComponent, and make the config for a new dynamic component...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / ModelToControllerReflector.pm
index 59004e3..f81b5ba 100644 (file)
@@ -1,9 +1,12 @@
 package CatalystX::ModelToControllerReflector;
 use Moose::Role;
-use namespace::clean -except => 'meta';
+use Moose::Util qw/does_role/;
+use List::MoreUtils qw/uniq/;
+use namespace::autoclean;
 
-with 'CatalystX::DynamicComponent' 
-    => { alias => { _setup_dynamic_component => '_setup_dynamic_controller' } };
+with 'CatalystX::DynamicComponent' => {
+    name => '_setup_dynamic_controller',
+};
 
 requires 'setup_components';
 
@@ -12,7 +15,7 @@ after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
 sub _setup_dynamic_controllers {
     my ($app) = @_;
     my @model_names = grep { /::Model::/ } keys %{ $app->components };
-    
+
     foreach my $model_name (@model_names) {
         $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
     }
@@ -29,24 +32,23 @@ sub _reflect_model_to_controller {
     my $suffix = $model_name;
     $suffix =~ s/^.*::Model:://;
 
-    my $controller = $app->_setup_dynamic_controller( $controller_name, {}, sub {
-        shift->next::method(@_); # Just use the default COMPONENT method
-    });
-    my $meta = $controller->meta;
-    $meta->make_mutable; # Dirty, I should build the class, add the methods, then
-                         # last of all make it a component
-    $meta->remove_method('COMPONENT');
-    $meta->superclasses('DynamicAppDemo::ControllerBase');
-
-    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;
+    foreach my $method_name (keys %$model_methods) {
+            next unless does_role($model_methods->{$method_name}, 'CatalystX::ControllerGeneratingModel::DispatchableMethod');
             # 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;
+
+    my $config_name = $controller_name;
+    $config_name =~ s/^[^:]+:://;
+    my $config = $app->config->{$config_name};
+    my @roles = @{ $config->{roles}||[] };
+    @roles = uniq @roles, 'CatalystX::ModelToControllerReflector::ControllerRole';
+    $config->{roles} = \@roles;
+    $config->{methods} = \%controller_methods;
+    $app->_setup_dynamic_controller( $controller_name, $config );
 }
 
 sub generate_reflected_controller_action_method {