First step switch to MX::Role::Parameterized
[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
15role {
16 my $p = shift;
17 my $name = $p->name;
18 my $pre_immutable_hook = $p->pre_immutable_hook if $p->has_pre_immutable_hook;
19 method $name => sub {
20 my ($app, $name, $config, $component_method) = @_;
21
22 my $appclass = blessed($app) || $app;
23 my $type = $name;
24 $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this.
25 $type =~ s/::.*$//;
26
27 my $meta = Moose->init_meta( for_class => $name );
28 $meta->superclasses('Catalyst::' . $type);
29 $meta->add_method( COMPONENT => $component_method );
30 $app->$pre_immutable_hook($meta) if $pre_immutable_hook;
31 $meta->make_immutable;
32
33 my $instance = $app->setup_component($name);
34 $app->components->{ $name } = $instance;
35 };
36};
59fc9d16 37
381;
39