c61437a7376dfb2cba3ce4b791a529ec6e646024
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelToControllerReflector.pm
1 package CatalystX::DynamicComponent::ModelToControllerReflector;
2 use Moose::Role;
3 use Moose::Util qw/does_role/;
4 use List::MoreUtils qw/uniq/;
5 use namespace::autoclean;
6
7 my $mangle_attributes_on_generated_methods = sub {
8     my ($meta, $config) = @_;
9     foreach my $name (keys %{ $config->{methods}}) {
10         my $m = $meta->get_method($name);
11         $meta->register_method_attributes($m->body, ['Local']);
12     }
13 };
14
15 with 'CatalystX::DynamicComponent' => {
16     name => '_setup_dynamic_controller',
17     pre_immutable_hook => $mangle_attributes_on_generated_methods,
18 };
19
20 requires 'setup_components';
21
22 after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
23
24 sub _setup_dynamic_controllers {
25     my ($app) = @_;
26
27     my @model_names = grep { /::Model::/ } keys %{ $app->components };
28     foreach my $model_name (@model_names) {
29         $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
30     }
31 }
32
33 sub _reflect_model_to_controller {
34     my ( $app, $model_name, $model ) = @_;
35
36     # Model passed in as MyApp::Model::Foo, strip MyApp
37     $model_name =~ s/^[^:]+:://;
38
39     # Get Controller::Foo
40     my $controller_name = $model_name;
41     $controller_name =~ s/^Model::/Controller::/;
42
43     # Get Foo
44     my $suffix = $model_name;
45     $suffix =~ s/Model:://;
46
47     my %controller_methods;
48     # FIXME - Abstract this strategy crap out.
49     my $model_methods = $model->meta->get_method_map;
50     my $interface_roles = $app->config->{$model_name}->{interface_roles};
51
52     for my $interface_role (@$interface_roles) {
53             for my $required_method ($interface_role->meta->get_required_method_list) {
54                     # Note need to pass model name, as the method actually comes from
55                     # the underlying model class, not the Catalyst shim class we autogenerated.
56                     $controller_methods{$required_method} = 
57                          $app->generate_reflected_controller_action_method($suffix, $model_methods->{$required_method})
58             }
59     }
60
61     # Shallow copy so we don't stuff method refs in config
62     my $config = { %{$app->config->{$controller_name}||{}} };
63
64     $config->{methods} = \%controller_methods;
65     $app->_setup_dynamic_controller( $controller_name, $config );
66 }
67
68 sub generate_reflected_controller_action_method {
69     my ( $app, $model, $method ) = @_;
70     my $method_name = $method->name; # Is it worth passing the actual method object here?
71     sub {
72         my ($self, $c, @args) = @_;
73         $c->res->header('X-From-Model', $model);
74         my $response = $c->model($model)->$method_name($c->req->data);
75         $c->res->header('X-From-Model-Data', $response);
76         $c->res->body('OK');
77         $c->stash->{response} = $response;
78     };
79 }
80
81 1;
82
83 __END__
84
85 =head1 NAME
86
87 CatalystX::DynamicComponent::ModelToControllerReflector - Generate Catalyst controllers automaticall from models and configuration.
88
89 =head1 SYNOPSIS
90
91 =head1 DESCRIPTION
92
93 =head1 LINKS
94
95 L<Catalyst>, L<MooseX::MethodAttributes>, L<CatalystX::ModelsFromConfig>.
96
97 =head1 BUGS
98
99 Probably plenty, test suite certainly isn't comprehensive.. Patches welcome.
100
101 =head1 AUTHOR
102
103 Tomas Doran (t0m) <bobtfish@bobtfish.net>
104
105 =head1 LICENSE
106
107 This code is copyright (c) 2009 Tomas Doran. This code is licensed on the same terms as perl
108 itself.
109
110 =cut
111