Get rid of the insanity of passing the compoenent method as a subref - instead it...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / ModelsFromConfig.pm
1 package CatalystX::ModelsFromConfig;
2 use Moose::Role;
3 use Catalyst::Model::Adaptor ();
4 use namespace::autoclean;
5
6 requires qw/
7     config
8     setup_components
9     setup_component
10 /;
11
12 # Note method reaming - allows user to modify my setup_dynamic_component without being
13 #                       forced to do it globally.
14 with 'CatalystX::DynamicComponent' => {
15     name => '_setup_dynamic_model',
16     COMPONENT => sub {
17          my ($component_class_name, $app, $args) = @_;
18
19         my $class = delete $args->{class};
20         Class::MOP::load_class($class);
21
22         $class->new($args);
23     },
24 };
25
26 after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };
27
28 sub _setup_dynamic_models {
29     my ($app) = @_;
30     
31     my $app_name = blessed($app) || $app;
32     my $model_prefix = 'Model::';
33
34     my $config = $app->config || {};
35     
36     foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
37         my $model_class_name = $app_name . '::' . $model_name;
38         
39         $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} );
40     }
41 }
42
43 1;
44