489546c201cc7dbab5d2423cd75326b0bfc3a9c7
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelsFromConfig.pm
1 package CatalystX::DynamicComponent::ModelsFromConfig;
2 use Moose::Role;
3 use namespace::autoclean;
4
5 requires qw/
6     config
7     setup_components
8     setup_component
9 /;
10
11 # Note method reaming - allows user to modify my setup_dynamic_component without being
12 #                       forced to do it globally.
13 with 'CatalystX::DynamicComponent' => {
14     name => '_setup_dynamic_model',
15     methods => {
16         COMPONENT => sub {
17             my ($component_class_name, $app, $args) = @_;
18
19             my $class = $args->{class};
20             Class::MOP::load_class($class);
21             
22             $class->new($args);
23         },
24     },
25 };
26
27 after 'setup_components' => sub { shift->_setup_dynamic_models(@_); };
28
29 sub _setup_dynamic_models {
30     my ($app) = @_;
31
32     my $model_prefix = 'Model::';
33
34     my $config = $app->config || {};
35     my $myconfig = $config->{'CatalystX::DynamicComponent::ModelsFromConfig'} || {};
36
37     foreach my $model_name ( grep { /^$model_prefix/ } keys %$config ) {
38         if (my $inc = $myconfig->{include}) {
39             next unless $model_name =~ /$inc/;
40         }
41         if (my $exc = $myconfig->{exclude}) {
42             next if $model_name =~ /$exc/;
43         }
44
45         $app->_setup_dynamic_model( $model_name, $config->{$model_name} );
46     }
47 }
48
49 1;
50
51 __END__
52
53 =head1 NAME
54
55 CatalystX::DynamicComponent::ModelsFromConfig - Generate simple L<Catalyst::Model::Adaptor> like models purely from application config.
56
57 =head1 SYNOPSIS
58
59     package MyApp;
60     use Moose;
61     use namespace::autoclean;
62     use Catalyst qw/
63         +CatalystX::DynamicComponent::ModelsFromConfig
64     /;
65     __PACKAGE__->config(
66         name => __PACKAGE__,
67         'CatalystX::DynamicComponent::ModelsFromConfig' => {
68             include => '(One|Two|Three)^',
69             exclude => 'Tewnty',
70         },
71         'Model::One' => {
72             class => 'SomeClass', # Name of class to load and construct
73             other => 'config',    # Constructor passed other parameters
74         },
75         'Model::Two' => {
76             class => 'SomeOtherClass',
77             other => 'config',
78         },
79         ...
80         'Model::TwentyThree' => { # Ignored, as excluded
81         ...
82     );
83     __PACKAGE__->setup;
84
85 =head1 DESCRIPTION
86
87 FIXME
88
89 =head1 LINKS
90
91 L<Catalyst>, L<MooseX::MethodAttributes>, L<CatalystX::DynamicComponent>.
92
93 =head1 BUGS
94
95 Probably plenty, test suite certainly isn't comprehensive.. Patches welcome.
96
97 =head1 AUTHOR
98
99 Tomas Doran (t0m) <bobtfish@bobtfish.net>
100
101 =head1 LICENSE
102
103 This code is copyright (c) 2009 Tomas Doran. This code is licensed on the same terms as perl
104 itself.
105
106 =cut
107