Fix bugs introduced previously
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelToControllerReflector.pm
index d697e7c..c9f3877 100644 (file)
@@ -1,7 +1,8 @@
 package CatalystX::DynamicComponent::ModelToControllerReflector;
 use Moose::Role;
 use Moose::Util qw/does_role/;
-use List::MoreUtils qw/uniq/;
+use MooseX::Types::Moose qw/Str/;
+use Moose::Util::TypeConstraints;
 use namespace::autoclean;
 
 my $mangle_attributes_on_generated_methods = sub {
@@ -30,36 +31,46 @@ sub _setup_dynamic_controllers {
     }
 }
 
+my $interface = 'CatalystX::DynamicComponent::ModelToControllerReflector::Strategy';
+role_type $interface;
+
 sub _reflect_model_to_controller {
     my ( $app, $model_name, $model ) = @_;
 
-    my $class = blessed($app) || $app;
+    # Model passed in as MyApp::Model::Foo, strip MyApp
+    $model_name =~ s/^[^:]+:://;
 
+    # Get Controller::Foo
     my $controller_name = $model_name;
-    $controller_name =~ s/^.*::Model::/Controller::/;
+    $controller_name =~ s/^Model::/Controller::/;
 
+    # Get Foo
     my $suffix = $model_name;
-    $suffix =~ s/^.*::Model:://;
+    $suffix =~ s/Model:://;
 
     my %controller_methods;
     # FIXME - Abstract this strategy crap out.
+
+    my $config = exists $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'}
+        ? $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'} : {};
+    my $strategy = exists $config->{reflection_strategy} ? $config->{reflection_strategy} : 'InterfaceRoles';
+    $strategy = "CatalystX::DynamicComponent::ModelToControllerReflector::Strategy::$strategy";
+    Class::MOP::load_class($strategy);
+    $strategy->new;
+
     my $model_methods = $model->meta->get_method_map;
-    my $interface_roles = $model_name->dynamic_model_config->{interface_roles};
-
-    for my $interface_role (@$interface_roles) {
-            for my $required_method ($interface_role->meta->get_required_method_list) {
-                    # Note need to pass model name, as the method actually comes from
-                    # the underlying model class, not the Catalyst shim class we autogenerated.
-                    $controller_methods{$required_method} = 
-                         $app->generate_reflected_controller_action_method($suffix, $model_methods->{$required_method})
-            }
+    foreach my $method_name ( $strategy->get_reflected_method_list($app, $model_name) ) {
+        # Note need to pass model name, as the method actually comes from
+        # the underlying model class, not the Catalyst shim class we autogenerated.
+        $controller_methods{$method_name} = 
+             $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
     }
 
     # Shallow copy so we don't stuff method refs in config
-    my $config = { %{$app->config->{$controller_name}||{}} };
+    my $controller_config = { %{$app->config->{$controller_name}||{}} };
 
-    $config->{methods} = \%controller_methods;
-    $app->_setup_dynamic_controller( $controller_name, $config );
+    $controller_config->{methods} = \%controller_methods;
+    $app->_setup_dynamic_controller( $controller_name, $controller_config );
 }
 
 sub generate_reflected_controller_action_method {
@@ -68,8 +79,10 @@ sub generate_reflected_controller_action_method {
     sub {
         my ($self, $c, @args) = @_;
         $c->res->header('X-From-Model', $model);
-        $c->res->header('X-From-Model-Data', $c->model($model)->$method_name(@args));
+        my $response = $c->model($model)->$method_name($c->req->data);
+        $c->res->header('X-From-Model-Data', $response);
         $c->res->body('OK');
+        $c->stash->{response} = $response;
     };
 }