move the model to controller reflector into a namespace under dynamiccomponent
[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     roles => ['CatalystX::DynamicComponent::ModelToControllerReflector::ControllerRole'],
18     pre_immutable_hook => $mangle_attributes_on_generated_methods,
19 };
20
21 requires 'setup_components';
22
23 after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
24
25 sub _setup_dynamic_controllers {
26     my ($app) = @_;
27     my @model_names = grep { /::Model::/ } keys %{ $app->components };
28
29     foreach my $model_name (@model_names) {
30         $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
31     }
32 }
33
34 sub _reflect_model_to_controller {
35     my ( $app, $model_name, $model ) = @_;
36
37     my $class = blessed($app) || $app;
38
39     my $controller_name = $model_name;
40     $controller_name =~ s/::Model::/::Controller::/;
41
42     my $suffix = $model_name;
43     $suffix =~ s/^.*::Model:://;
44
45     my %controller_methods;
46     # FIXME - Abstract this strategy crap out.
47     my $model_methods = $model->meta->get_method_map;
48     foreach my $method_name (keys %$model_methods) {
49             next unless does_role($model_methods->{$method_name}, 'CatalystX::ControllerGeneratingModel::DispatchableMethod');
50             # Note need to pass model name, as the method actually comes from
51             # the underlying model class, not the Catalyst shim class we autogenerated.
52             $controller_methods{$method_name} = $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
53     }
54
55     my $config_name = $controller_name;
56     $config_name =~ s/^[^:]+:://;
57     
58     # Shallow copy so we don't stuff method refs in config
59     my $config = { %{$app->config->{$config_name}||{}} };
60     
61     $config->{methods} = \%controller_methods;
62     $app->_setup_dynamic_controller( $controller_name, $config );
63 }
64
65 sub generate_reflected_controller_action_method {
66     my ( $app, $model, $method ) = @_;
67     my $method_name = $method->name; # Is it worth passing the actual method object here?
68     sub {
69         my ($self, $c, @args) = @_;
70         $c->res->header('X-From-Model', $model);
71         $c->res->header('X-From-Model-Data', $c->model($model)->$method_name(@args));
72         $c->res->body('OK');
73     };
74 }
75
76 1;
77