Commit | Line | Data |
59fc9d16 |
1 | package CatalystX::DynamicComponent; |
53a42ae0 |
2 | use MooseX::Role::Parameterized; |
046d763d |
3 | use namespace::autoclean; |
59fc9d16 |
4 | |
53a42ae0 |
5 | parameter 'name' => ( |
6 | isa => 'Str', |
7 | required => 1, |
8 | ); |
9 | |
10 | parameter 'pre_immutable_hook' => ( |
11 | isa => 'Str', |
12 | predicate => 'has_pre_immutable_hook', |
13 | ); |
14 | |
cd6bd40d |
15 | parameter 'COMPONENT' => ( |
16 | isa => 'CodeRef', |
17 | predicate => 'has_custom_component_method', |
18 | ); |
19 | |
53a42ae0 |
20 | role { |
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 | |
52 | 1; |
53 | |