Add more tests for config replacement and make them pass. I seem to have made 10_app...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / ModelToControllerReflector.pm
CommitLineData
59fc9d16 1package CatalystX::ModelToControllerReflector;
2use Moose::Role;
abcde601 3use Moose::Util qw/does_role/;
279c014c 4use List::MoreUtils qw/uniq/;
046d763d 5use namespace::autoclean;
59fc9d16 6
28627027 7my $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 $m->meta->get_attribute('attributes')->set_value($m, ['Local']);
12 }
13};
14
53a42ae0 15with 'CatalystX::DynamicComponent' => {
16 name => '_setup_dynamic_controller',
4e8f668c 17 roles => ['CatalystX::ModelToControllerReflector::ControllerRole'],
28627027 18 pre_immutable_hook => $mangle_attributes_on_generated_methods,
53a42ae0 19};
59fc9d16 20
cbc455a6 21requires 'setup_components';
59fc9d16 22
cbc455a6 23after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); };
59fc9d16 24
25sub _setup_dynamic_controllers {
26 my ($app) = @_;
27 my @model_names = grep { /::Model::/ } keys %{ $app->components };
00b934f1 28
59fc9d16 29 foreach my $model_name (@model_names) {
6a2f1e96 30 $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} );
59fc9d16 31 }
32}
33
6a2f1e96 34sub _reflect_model_to_controller {
35 my ( $app, $model_name, $model ) = @_;
36
77e54b00 37 my $class = blessed($app) || $app;
38
6a2f1e96 39 my $controller_name = $model_name;
40 $controller_name =~ s/::Model::/::Controller::/;
41
77e54b00 42 my $suffix = $model_name;
43 $suffix =~ s/^.*::Model:://;
44
549d6abc 45 my %controller_methods;
46 my $model_methods = $model->meta->get_method_map;
549d6abc 47 foreach my $method_name (keys %$model_methods) {
abcde601 48 next unless does_role($model_methods->{$method_name}, 'CatalystX::ControllerGeneratingModel::DispatchableMethod');
77e54b00 49 # Note need to pass model name, as the method actually comes from
50 # the underlying model class, not the Catalyst shim class we autogenerated.
549d6abc 51 $controller_methods{$method_name} = $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
77e54b00 52 }
549d6abc 53
0b07685c 54 my $config_name = $controller_name;
55 $config_name =~ s/^[^:]+:://;
4e8f668c 56
57 # Shallow copy so we don't stuff method refs in config
58 my $config = { %{$app->config->{$config_name}} };
59
c52d8688 60 $config->{methods} = \%controller_methods;
61 $app->_setup_dynamic_controller( $controller_name, $config );
2f61148c 62}
63
77e54b00 64sub generate_reflected_controller_action_method {
65 my ( $app, $model, $method ) = @_;
66 my $method_name = $method->name; # Is it worth passing the actual method object here?
67 sub {
68 my ($self, $c, @args) = @_;
69 $c->res->header('X-From-Model', $model);
70 $c->res->header('X-From-Model-Data', $c->model($model)->$method_name(@args));
71 $c->res->body('OK');
72 };
59fc9d16 73}
74
751;
76