Convert the controller base class into a role applied to the controller meta. There...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent.pm
CommitLineData
59fc9d16 1package CatalystX::DynamicComponent;
53a42ae0 2use MooseX::Role::Parameterized;
046d763d 3use namespace::autoclean;
59fc9d16 4
53a42ae0 5parameter 'name' => (
6 isa => 'Str',
7 required => 1,
8);
9
10parameter 'pre_immutable_hook' => (
11 isa => 'Str',
12 predicate => 'has_pre_immutable_hook',
13);
14
cd6bd40d 15parameter 'COMPONENT' => (
16 isa => 'CodeRef',
17 predicate => 'has_custom_component_method',
18);
19
53a42ae0 20role {
21 my $p = shift;
22 my $name = $p->name;
cd6bd40d 23 my $pre_immutable_hook = $p->pre_immutable_hook;
53a42ae0 24 method $name => sub {
549d6abc 25 my ($app, $name, $config, $methods) = @_;
53a42ae0 26
27 my $appclass = blessed($app) || $app;
28 my $type = $name;
29 $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this.
30 $type =~ s/::.*$//;
31
32 my $meta = Moose->init_meta( for_class => $name );
33 $meta->superclasses('Catalyst::' . $type);
549d6abc 34
cd6bd40d 35 if ($p->has_custom_component_method) {
36 $meta->add_method(COMPONENT => $p->COMPONENT);
37 }
549d6abc 38
cd6bd40d 39 $app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook;
549d6abc 40
41 $methods ||= {};
42 foreach my $name (keys %$methods) {
43 $meta->add_method($name => $methods->{$name});
44 }
53a42ae0 45 $meta->make_immutable;
46
47 my $instance = $app->setup_component($name);
48 $app->components->{ $name } = $instance;
49 };
50};
59fc9d16 51
521;
53