Make the generated action method fit better into what the StompTestApp::Controller...
t0m [Thu, 28 May 2009 00:28:24 +0000 (01:28 +0100)]
Also add a type check to the input document (i.e. the deserialized message) using structured and lexical
type constraints. I do this in a method modifier, to illustrate that we can get the reflector
(or something) to apply a modifier like this around every model method.

Tada, instant explosion when the data incoming is bad.

You can do this in the controller action with the response document before you stash it also.

This isn't quite as elegant as applying the types as part of the interface role, but I'm sure we
can work through that if this is considered useful.

Makefile.PL
lib/CatalystX/DynamicComponent/ModelToControllerReflector.pm
t/lib/SomeModelClass.pm

index b294dcb..0b3e661 100644 (file)
@@ -12,6 +12,9 @@ requires 'MooseX::Role::Parameterized' => '0.06';
 requires 'Catalyst::Runtime' => '5.80004';
 requires 'List::MoreUtils';
 
+requires 'MooseX::Types::Structured';
+requires 'MooseX::Lexical::Types';
+
 resources repository => 'git@github.com:bobtfish/catalyst-dynamicappdemo.git';
 
 auto_install;
index 11e19e4..c61437a 100644 (file)
@@ -71,8 +71,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;
     };
 }
 
index 22dc3b6..f3184f8 100644 (file)
@@ -7,6 +7,8 @@ requires 'say_hello';
 package SomeModelClass;
 use Moose;
 use CatalystX::ControllerGeneratingModel;
+use DemoTypeLibrary qw/MessageDocument/;
+use MooseX::Lexical::Types qw/MessageDocument/;
 use namespace::autoclean;
 
 # Note trivial calling convention.
@@ -15,12 +17,24 @@ use namespace::autoclean;
 # Introspection should only reflect methods which satisfy the calling convention
 # This is left as an exercise to the reader. :)
 
+# Note command syntax not actually needed, this could be a normal sub,
+# but doing so makes the eventual merge harder..
+
 command say_hello => sub {
-    my ($self, $name) = @_;
-    return("Hello $name");
+    my ($self, $document) = @_;
+
+    my $name = $document->{name};
+    return({ type => 'say_hello_response',
+
+            body => "Hello $name" });
 };
 
 with 'SomeModelClassInterface';
 
+before 'say_hello' => sub {
+    my $self = shift;
+    my MessageDocument $message = shift;
+};
+
 __PACKAGE__->meta->make_immutable;