Thats action registration sorted out. Now I just need to introspect all the models...
t0m [Wed, 22 Apr 2009 23:44:55 +0000 (00:44 +0100)]
lib/DynamicAppDemo/Controller/FakeExample.pm [new file with mode: 0644]
lib/DynamicAppDemo/ControllerBase.pm
t/01app.t

diff --git a/lib/DynamicAppDemo/Controller/FakeExample.pm b/lib/DynamicAppDemo/Controller/FakeExample.pm
new file mode 100644 (file)
index 0000000..d7c2365
--- /dev/null
@@ -0,0 +1,25 @@
+package DynamicAppDemo::Controller::FakeExample;
+use Moose;
+use namespace::clean -except => 'meta'; # N.B. namespace clean cest tres important
+                                        #      as this package is introspected, a lot!
+
+BEGIN { extends 'DynamicAppDemo::ControllerBase' }
+
+# This is a fake controller.
+
+# It exists to show an example of using ::ControllerBase which is autogenerated,
+# which helps me seperate the building and testing of the controller action
+# registration functionality, seperate the the code generation :)
+
+sub register_me { 
+    my ($self, $c) = @_;
+    $c->res->body('quux');
+    $c->res->header('X-Foo', 'bar');
+}
+
+sub _do_not_register_me { }
+
+# In a non-trivial example, you would use MX::Method::Sigs :)
+
+__PACKAGE__->meta->make_immutable;
+
index 8a84f23..7f0aafd 100644 (file)
@@ -1,9 +1,33 @@
 package DynamicAppDemo::ControllerBase;
 use Moose;
+use Moose::Util qw/find_meta/;
 use namespace::clean -except => 'meta';
 
 # Should not need attributes here, but what the hell..
 BEGIN { extends 'Catalyst::Controller' }
 
+around get_action_methods => sub {
+    my $orig = shift;
+    my $self = shift;
+
+    my $meta = find_meta($self);
+    
+    # FIXME - fugly, and nasty
+    return (
+        (   map { 
+                my $m = $meta->get_method($_);
+                # EPIC CHEAT to just smash the attribute definition :)
+                $m->meta->get_attribute('attributes')->set_value($m, ['Local']);
+                $m;
+            }
+            grep { ! /^(_|new|meta)/ }
+            $meta->get_method_list
+        ),
+        (
+            $self->$orig(@_)
+        )
+    ); 
+};
+
 __PACKAGE__->meta->make_immutable;
 
index 9ac48a9..edd83cd 100644 (file)
--- a/t/01app.t
+++ b/t/01app.t
@@ -1,7 +1,12 @@
 use strict;
 use warnings;
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 BEGIN { use_ok 'Catalyst::Test', 'DynamicAppDemo' }
 
-ok( request('/')->is_success, 'Request should succeed' );
+{
+    my $res = request('/fakeexample/register_me');
+    ok( $res->is_success, 'should succeed' );
+    is( $res->header('X-Foo'), 'bar', 'is calling correct code' );
+}
+