Add more tests for DynamicComponent, and make the config for a new dynamic component...
Tomas Doran (t0m) [Sun, 24 May 2009 18:41:51 +0000 (19:41 +0100)]
lib/CatalystX/DynamicComponent.pm
lib/CatalystX/ModelToControllerReflector.pm
lib/CatalystX/ModelsFromConfig.pm
t/03_dynamiccomponent.t

index 2ba384f..8566aab 100644 (file)
@@ -26,7 +26,9 @@ role {
     my $pre_immutable_hook = $p->pre_immutable_hook;
 
     method $name => sub {
-        my ($app, $name, $config, $methods) = @_;
+        my ($app, $name, $config) = @_;
+
+        $config ||= {};
 
         my $appclass = blessed($app) || $app;
         my $type = $name;
@@ -49,9 +51,8 @@ role {
 
         $app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook;
 
-        $methods ||= {};
-        foreach my $name (keys %$methods) {
-            $meta->add_method($name => $methods->{$name});
+        foreach my $name (keys %{ $config->{methods}||{} }) {
+            $meta->add_method($name => $config->{methods}->{$name});
         }
         $meta->make_immutable;
 
index 7752b97..f81b5ba 100644 (file)
@@ -47,7 +47,8 @@ sub _reflect_model_to_controller {
     my @roles = @{ $config->{roles}||[] };
     @roles = uniq @roles, 'CatalystX::ModelToControllerReflector::ControllerRole';
     $config->{roles} = \@roles;
-    $app->_setup_dynamic_controller( $controller_name, $config, \%controller_methods );
+    $config->{methods} = \%controller_methods;
+    $app->_setup_dynamic_controller( $controller_name, $config );
 }
 
 sub generate_reflected_controller_action_method {
index 985ac8f..d02e74e 100644 (file)
@@ -27,15 +27,15 @@ after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };
 
 sub _setup_dynamic_models {
     my ($app) = @_;
-    
+
     my $app_name = blessed($app) || $app;
     my $model_prefix = 'Model::';
 
     my $config = $app->config || {};
-    
+
     foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
         my $model_class_name = $app_name . '::' . $model_name;
-        
+
         $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
     }
 }
index a6f006e..bc11476 100644 (file)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 10;
+use Test::More tests => 15;
 use Test::Exception;
 
 use Moose ();
@@ -47,4 +47,35 @@ throws_ok { generate_testapp(); }
     is($name->can('COMPONENT'), $COMPONENT, 'Supplied COMPONENT method is on $app::Model::Foo');
 }
 
+{
+    package My::Model;
+    use Moose;
+    extends qw/Catalyst::Model/;
+    __PACKAGE__->meta->make_immutable;
+}
+{
+    package My::Role;
+    use Moose::Role;
+    sub _some_method_from_role {}
+}
+{
+    my $app_meta = generate_testapp({
+        name => 'dynamic_component_method',
+    });
+    my $app = $app_meta->name;
+    my $extra_config = {
+        superclasses => ['My::Model'],
+        roles => ['My::Role'],
+        methods => {
+            my_injected_method => sub { 'quuux' },
+        }
+    };
+    $app->dynamic_component_method( $app . "::Model::Foo", $extra_config );
+    my $model = $app->model('Foo');
+    isa_ok($model, 'My::Model');
+    ok $model->can('_some_method_from_role'), 'Has had role applied';
+    ok !My::Model->can('_some_method_from_role'), 'Role applied at right place';
+    ok $model->can('my_injected_method'), 'Injected method there as expected';
+    is $model->my_injected_method, 'quuux', 'Injected method returns correct val';
+}