And mangle things such that the actual app test works
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent / ModelToControllerReflector.pm
CommitLineData
192db6f8 1package CatalystX::DynamicComponent::ModelToControllerReflector;
59fc9d16 2use Moose::Role;
abcde601 3use Moose::Util qw/does_role/;
f38d3061 4use MooseX::Types::Moose qw/Str/;
5use Moose::Util::TypeConstraints;
046d763d 6use namespace::autoclean;
59fc9d16 7
28627027 8my $mangle_attributes_on_generated_methods = sub {
9 my ($meta, $config) = @_;
10 foreach my $name (keys %{ $config->{methods}}) {
11 my $m = $meta->get_method($name);
83c57636 12 $meta->register_method_attributes($m->body, ['Local']);
28627027 13 }
14};
15
53a42ae0 16with 'CatalystX::DynamicComponent' => {
17 name => '_setup_dynamic_controller',
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) = @_;
00b934f1 27
62680454 28 my @model_names = grep { /::Model::/ } keys %{ $app->components };
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
f38d3061 34my $interface = 'CatalystX::DynamicComponent::ModelToControllerReflector::Strategy';
35role_type $interface;
36
6a2f1e96 37sub _reflect_model_to_controller {
38 my ( $app, $model_name, $model ) = @_;
39
fd379fe9 40 # Model passed in as MyApp::Model::Foo, strip MyApp
41 $model_name =~ s/^[^:]+:://;
77e54b00 42
fd379fe9 43 # Get Controller::Foo
6a2f1e96 44 my $controller_name = $model_name;
fd379fe9 45 $controller_name =~ s/^Model::/Controller::/;
6a2f1e96 46
fd379fe9 47 # Get Foo
77e54b00 48 my $suffix = $model_name;
fd379fe9 49 $suffix =~ s/Model:://;
77e54b00 50
549d6abc 51 my %controller_methods;
192db6f8 52 # FIXME - Abstract this strategy crap out.
f38d3061 53
84d8e4e1 54 my $config = exists $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'}
55 ? $app->config->{'CatalystX::DynamicComponent::ModelToControllerReflector'} : {};
56 my $strategy = exists $config->{reflection_strategy} ? $config->{reflection_strategy} : 'InterfaceRoles';
f38d3061 57 $strategy = "CatalystX::DynamicComponent::ModelToControllerReflector::Strategy::$strategy";
58 Class::MOP::load_class($strategy);
59 $strategy->new;
60
549d6abc 61 my $model_methods = $model->meta->get_method_map;
a3d3547a 62 foreach my $method_name ( $strategy->get_reflected_method_list($app, $model_name, $model) ) {
f38d3061 63 # Note need to pass model name, as the method actually comes from
64 # the underlying model class, not the Catalyst shim class we autogenerated.
65 $controller_methods{$method_name} =
66 $app->generate_reflected_controller_action_method($suffix, $model_methods->{$method_name})
77e54b00 67 }
549d6abc 68
4e8f668c 69 # Shallow copy so we don't stuff method refs in config
84d8e4e1 70 my $controller_config = { %{$app->config->{$controller_name}||{}} };
96e54fc3 71
84d8e4e1 72 $controller_config->{methods} = \%controller_methods;
73 $app->_setup_dynamic_controller( $controller_name, $controller_config );
2f61148c 74}
75
77e54b00 76sub generate_reflected_controller_action_method {
77 my ( $app, $model, $method ) = @_;
78 my $method_name = $method->name; # Is it worth passing the actual method object here?
79 sub {
80 my ($self, $c, @args) = @_;
81 $c->res->header('X-From-Model', $model);
4e90c38e 82 my $response = $c->model($model)->$method_name( { name => $args[0] });
83 $c->res->header('X-From-Model-Data', $response->{body});
77e54b00 84 $c->res->body('OK');
d7bd7765 85 $c->stash->{response} = $response;
77e54b00 86 };
59fc9d16 87}
88
891;
90
7299c4b9 91__END__
92
93=head1 NAME
94
95CatalystX::DynamicComponent::ModelToControllerReflector - Generate Catalyst controllers automaticall from models and configuration.
96
97=head1 SYNOPSIS
98
99=head1 DESCRIPTION
100
101=head1 LINKS
102
103L<Catalyst>, L<MooseX::MethodAttributes>, L<CatalystX::ModelsFromConfig>.
104
105=head1 BUGS
106
107Probably plenty, test suite certainly isn't comprehensive.. Patches welcome.
108
109=head1 AUTHOR
110
111Tomas Doran (t0m) <bobtfish@bobtfish.net>
112
113=head1 LICENSE
114
115This code is copyright (c) 2009 Tomas Doran. This code is licensed on the same terms as perl
116itself.
117
118=cut
119