Commit | Line | Data |
7d26c84b |
1 | package CatalystX::ModelsFromConfig; |
2 | use Moose::Role; |
3 | use Catalyst::Model::Adaptor (); |
046d763d |
4 | use namespace::autoclean; |
7d26c84b |
5 | |
6 | requires qw/ |
7 | config |
8 | setup_components |
9 | setup_component |
10 | /; |
11 | |
6a2f1e96 |
12 | # Note method reaming - allows user to modify my setup_dynamic_component without being |
13 | # forced to do it globally. |
53a42ae0 |
14 | with 'CatalystX::DynamicComponent' => { |
15 | name => '_setup_dynamic_model', |
cd6bd40d |
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 | }, |
53a42ae0 |
24 | }; |
6a2f1e96 |
25 | |
7d26c84b |
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 | |
77e54b00 |
34 | my $config = $app->config || {}; |
7d26c84b |
35 | |
77e54b00 |
36 | foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) { |
7d26c84b |
37 | my $model_class_name = $app_name . '::' . $model_name; |
77e54b00 |
38 | |
cd6bd40d |
39 | $app->_setup_dynamic_model( $model_class_name, $config->{$model_name} ); |
7d26c84b |
40 | } |
41 | } |
42 | |
7d26c84b |
43 | 1; |
44 | |