Move CatalystX::ModelsFromConfig => ::DynamicComponent::ModelsFromConfig
[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 || {};
c52d8688 36
77e54b00 37 foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
7d26c84b 38 my $model_class_name = $app_name . '::' . $model_name;
c52d8688 39
cd6bd40d 40 $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
7d26c84b 41 }
42}
43
7d26c84b 441;
45