First step switch to MX::Role::Parameterized
t0m [Sun, 26 Apr 2009 18:39:46 +0000 (19:39 +0100)]
Makefile.PL
lib/CatalystX/DynamicComponent.pm
lib/CatalystX/ModelToControllerReflector.pm
lib/CatalystX/ModelsFromConfig.pm

index 71d8e6f..2380afb 100644 (file)
@@ -8,6 +8,7 @@ all_from 'lib/DynamicAppDemo.pm';
 requires 'MooseX::Types::Structured' => '0.12';
 requires 'namespace::autoclean';
 requires 'MooseX::Types' => '0.10';
+requires 'MooseX::Role::Parameterized' => '0.05';
 requires 'Catalyst::Model::Adaptor';
 requires 'Catalyst::Runtime' => '5.80001';
 requires 'Catalyst::Plugin::ConfigLoader';
index 4cf8f68..5e8bfc2 100644 (file)
@@ -1,25 +1,39 @@
 package CatalystX::DynamicComponent;
-use Moose::Role;
+use MooseX::Role::Parameterized;
 use namespace::autoclean;
 
-sub _setup_dynamic_component {
-    my ($app, $name, $config, $component_method) = @_;
-
-    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 => $component_method );
-
-    $meta->make_immutable;
-
-    my $instance = $app->setup_component($name);
-    $app->components->{ $name } = $instance;
-}
+parameter 'name' => (
+    isa => 'Str',
+    required => 1,
+);
+
+parameter 'pre_immutable_hook' => (
+    isa => 'Str',
+    predicate => 'has_pre_immutable_hook',
+);
+
+role {
+    my $p = shift;
+    my $name = $p->name;
+    my $pre_immutable_hook = $p->pre_immutable_hook if $p->has_pre_immutable_hook;
+    method $name => sub {
+        my ($app, $name, $config, $component_method) = @_;
+
+        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 => $component_method );
+        $app->$pre_immutable_hook($meta) if $pre_immutable_hook;
+        $meta->make_immutable;
+
+        my $instance = $app->setup_component($name);
+        $app->components->{ $name } = $instance;
+    };
+};
 
 1;
 
index 41f6be3..5f698a4 100644 (file)
@@ -2,8 +2,9 @@ package CatalystX::ModelToControllerReflector;
 use Moose::Role;
 use namespace::autoclean;
 
-with 'CatalystX::DynamicComponent' 
-    => { alias => { _setup_dynamic_component => '_setup_dynamic_controller' } };
+with 'CatalystX::DynamicComponent' => {
+    name => '_setup_dynamic_controller',
+};
 
 requires 'setup_components';
 
index 24ef29e..ba9fb12 100644 (file)
@@ -11,8 +11,9 @@ requires qw/
 
 # Note method reaming - allows user to modify my setup_dynamic_component without being
 #                       forced to do it globally.
-with 'CatalystX::DynamicComponent' 
-    => { alias => { _setup_dynamic_component => '_setup_dynamic_model' } };
+with 'CatalystX::DynamicComponent' => {
+    name => '_setup_dynamic_model',
+};
 
 after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };