Commit | Line | Data |
192db6f8 |
1 | package CatalystX::DynamicComponent::ModelToControllerReflector; |
59fc9d16 |
2 | use Moose::Role; |
abcde601 |
3 | use Moose::Util qw/does_role/; |
279c014c |
4 | use List::MoreUtils qw/uniq/; |
046d763d |
5 | use namespace::autoclean; |
59fc9d16 |
6 | |
28627027 |
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); |
83c57636 |
11 | $meta->register_method_attributes($m->body, ['Local']); |
28627027 |
12 | } |
13 | }; |
14 | |
53a42ae0 |
15 | with 'CatalystX::DynamicComponent' => { |
16 | name => '_setup_dynamic_controller', |
28627027 |
17 | pre_immutable_hook => $mangle_attributes_on_generated_methods, |
53a42ae0 |
18 | }; |
59fc9d16 |
19 | |
cbc455a6 |
20 | requires 'setup_components'; |
59fc9d16 |
21 | |
cbc455a6 |
22 | after 'setup_components' => sub { shift->_setup_dynamic_controllers(@_); }; |
59fc9d16 |
23 | |
24 | sub _setup_dynamic_controllers { |
25 | my ($app) = @_; |
00b934f1 |
26 | |
62680454 |
27 | my @model_names = grep { /::Model::/ } keys %{ $app->components }; |
59fc9d16 |
28 | foreach my $model_name (@model_names) { |
6a2f1e96 |
29 | $app->_reflect_model_to_controller( $model_name, $app->components->{$model_name} ); |
59fc9d16 |
30 | } |
31 | } |
32 | |
6a2f1e96 |
33 | sub _reflect_model_to_controller { |
34 | my ( $app, $model_name, $model ) = @_; |
35 | |
77e54b00 |
36 | my $class = blessed($app) || $app; |
37 | |
6a2f1e96 |
38 | my $controller_name = $model_name; |
96e54fc3 |
39 | $controller_name =~ s/^.*::Model::/Controller::/; |
6a2f1e96 |
40 | |
77e54b00 |
41 | my $suffix = $model_name; |
42 | $suffix =~ s/^.*::Model:://; |
43 | |
549d6abc |
44 | my %controller_methods; |
192db6f8 |
45 | # FIXME - Abstract this strategy crap out. |
549d6abc |
46 | my $model_methods = $model->meta->get_method_map; |
6eb165c2 |
47 | my $interface_roles = $model_name->dynamic_model_config->{interface_roles}; |
62680454 |
48 | |
49 | for my $interface_role (@$interface_roles) { |
6eb165c2 |
50 | for my $required_method ($interface_role->meta->get_required_method_list) { |
51 | # Note need to pass model name, as the method actually comes from |
52 | # the underlying model class, not the Catalyst shim class we autogenerated. |
53 | $controller_methods{$required_method} = |
54 | $app->generate_reflected_controller_action_method($suffix, $model_methods->{$required_method}) |
55 | } |
77e54b00 |
56 | } |
549d6abc |
57 | |
4e8f668c |
58 | # Shallow copy so we don't stuff method refs in config |
96e54fc3 |
59 | my $config = { %{$app->config->{$controller_name}||{}} }; |
60 | |
c52d8688 |
61 | $config->{methods} = \%controller_methods; |
62 | $app->_setup_dynamic_controller( $controller_name, $config ); |
2f61148c |
63 | } |
64 | |
77e54b00 |
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 | }; |
59fc9d16 |
74 | } |
75 | |
76 | 1; |
77 | |
7299c4b9 |
78 | __END__ |
79 | |
80 | =head1 NAME |
81 | |
82 | CatalystX::DynamicComponent::ModelToControllerReflector - Generate Catalyst controllers automaticall from models and configuration. |
83 | |
84 | =head1 SYNOPSIS |
85 | |
86 | =head1 DESCRIPTION |
87 | |
88 | =head1 LINKS |
89 | |
90 | L<Catalyst>, L<MooseX::MethodAttributes>, L<CatalystX::ModelsFromConfig>. |
91 | |
92 | =head1 BUGS |
93 | |
94 | Probably plenty, test suite certainly isn't comprehensive.. Patches welcome. |
95 | |
96 | =head1 AUTHOR |
97 | |
98 | Tomas Doran (t0m) <bobtfish@bobtfish.net> |
99 | |
100 | =head1 LICENSE |
101 | |
102 | This code is copyright (c) 2009 Tomas Doran. This code is licensed on the same terms as perl |
103 | itself. |
104 | |
105 | =cut |
106 | |