Add include/exclude options so that havingModelsFromConfig loaded doesn't involve...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelsFromConfig.pm
CommitLineData
0506e950 1package CatalystX::DynamicComponent::ModelsFromConfig;
7d26c84b 2use Moose::Role;
046d763d 3use namespace::autoclean;
7d26c84b 4
5requires qw/
6 config
7 setup_components
8 setup_component
9/;
10
6a2f1e96 11# Note method reaming - allows user to modify my setup_dynamic_component without being
12# forced to do it globally.
53a42ae0 13with 'CatalystX::DynamicComponent' => {
14 name => '_setup_dynamic_model',
4e8f668c 15 methods => {
16 COMPONENT => sub {
17 my ($component_class_name, $app, $args) = @_;
cd6bd40d 18
4e8f668c 19 my $class = delete $args->{class};
20 Class::MOP::load_class($class);
cd6bd40d 21
4e8f668c 22 $class->new($args);
23 },
cd6bd40d 24 },
53a42ae0 25};
6a2f1e96 26
7d26c84b 27after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };
28
29sub _setup_dynamic_models {
30 my ($app) = @_;
c52d8688 31
7d26c84b 32 my $app_name = blessed($app) || $app;
33 my $model_prefix = 'Model::';
34
77e54b00 35 my $config = $app->config || {};
f5cb8a51 36 my $myconfig = $config->{'CatalystX::DynamicComponent::ModelsFromConfig'} || {};
c52d8688 37
77e54b00 38 foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
f5cb8a51 39 if (my $inc = $myconfig->{include}) {
40 next unless $model_name =~ /$inc/;
41 }
42 if (my $exc = $myconfig->{exclude}) {
43 next if $model_name =~ /$exc/;
44 }
45
7d26c84b 46 my $model_class_name = $app_name . '::' . $model_name;
c52d8688 47
cd6bd40d 48 $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
7d26c84b 49 }
50}
51
7d26c84b 521;
53