Start my reflector. Factor out the component generation from the other shit into...
t0m [Thu, 23 Apr 2009 00:14:30 +0000 (01:14 +0100)]
lib/CatalystX/DynamicComponent.pm [new file with mode: 0644]
lib/CatalystX/ModelToControllerReflector.pm [new file with mode: 0644]

diff --git a/lib/CatalystX/DynamicComponent.pm b/lib/CatalystX/DynamicComponent.pm
new file mode 100644 (file)
index 0000000..8208689
--- /dev/null
@@ -0,0 +1,35 @@
+package CatalystX::DynamicComponent;
+use Moose::Role;
+use namespace::clean -excpept => 'meta';
+
+sub _setup_dynamic_component {
+    my ($app, $name, $config) = @_;
+
+    my $appclass = blessed($app) || $app;
+    my $type = $name;
+    $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this.
+    $type =~ s/::.*$//;
+
+    my $meta = Moose->init_meta( for_class => $name );
+    $meta->superclasses('Catalyst::' . $type);
+
+    $meta->add_method(
+
+      COMPONENT
+            => sub {
+        my ($component_class_name, $app, $args) = @_;
+
+        my $class = delete $args->{class};
+        Class::MOP::load_class($class);
+
+        $class->new($args);
+    });
+
+    $meta->make_immutable;
+
+    my $instance = $app->setup_component($name);
+    $app->components->{ $name } = $instance;
+}
+
+1;
+
diff --git a/lib/CatalystX/ModelToControllerReflector.pm b/lib/CatalystX/ModelToControllerReflector.pm
new file mode 100644 (file)
index 0000000..43d5575
--- /dev/null
@@ -0,0 +1,26 @@
+package CatalystX::ModelToControllerReflector;
+use Moose::Role;
+use namespace::clean -except => 'meta';
+
+with 'CatalystX::DynamicComponent';
+
+requires 'setup_components';
+
+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->_setup_dynamic_controller( $model_name, $app->components->{$model_name} );
+    }
+}
+
+sub _setup_dynamic_controller {
+    my ($app, $model_name, $model_component) = @_;
+    warn($model_name);
+}
+
+1;
+