Add include/exclude options so that havingModelsFromConfig loaded doesn't involve...
t0m [Mon, 25 May 2009 16:20:23 +0000 (17:20 +0100)]
lib/CatalystX/DynamicComponent/ModelsFromConfig.pm
lib/CatalystX/ModelToControllerReflector.pm
t/04_dynamicmodel.t
t/lib/DynamicAppDemo.pm

index ad2c88d..89f9c14 100644 (file)
@@ -33,8 +33,16 @@ sub _setup_dynamic_models {
     my $model_prefix = 'Model::';
 
     my $config = $app->config || {};
+    my $myconfig = $config->{'CatalystX::DynamicComponent::ModelsFromConfig'} || {};
 
     foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
+        if (my $inc = $myconfig->{include}) {
+            next unless $model_name =~ /$inc/;
+        }
+        if (my $exc = $myconfig->{exclude}) {
+            next if $model_name =~ /$exc/;
+        }
+
         my $model_class_name = $app_name . '::' . $model_name;
 
         $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
index 97c2b9a..5381c23 100644 (file)
@@ -55,7 +55,7 @@ sub _reflect_model_to_controller {
     $config_name =~ s/^[^:]+:://;
     
     # Shallow copy so we don't stuff method refs in config
-    my $config = { %{$app->config->{$config_name}} };
+    my $config = { %{$app->config->{$config_name}||{}} };
     
     $config->{methods} = \%controller_methods;
     $app->_setup_dynamic_controller( $controller_name, $config );
index 3824a96..48274a5 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 3;
+use Test::More tests => 6;
 
 use FindBin qw/$Bin/;
 use lib "$Bin/lib";
@@ -14,3 +14,9 @@ ok $model;
 isa_ok $model, 'SomeModelClass';
 is $model->say_hello('world'), 'Hello world';
 
+ok(DynamicAppDemo->model('Two'), 'Have model Two');
+
+ok(!DynamicAppDemo->model('Three'), 'No model Three');
+
+ok(!DynamicAppDemo->model('Four'), 'No model Four');
+
index 7dec654..127b305 100644 (file)
@@ -17,9 +17,22 @@ __PACKAGE__->config(
         superclasses => [qw/DynamicAppDemo::ControllerBase/],
         roles      => [qw/DynamicAppDemo::ControllerRole/],
     },
+    'CatalystX::DynamicComponent::ModelsFromConfig' => {
+        include => 'One|Two|Four',
+        exclude => 'Four',
+    },
     'Model::One' => {
         class => 'SomeModelClass',
     },
+    'Model::Two' => {
+        class => 'SomeModelClass',
+    },
+    'Model::Three' => {
+        class => 'SomeModelClass',
+    },
+    'Model::Four' => {
+        class => 'SomeModelClass',
+    },
 );
 
 __PACKAGE__->setup();