Commit | Line | Data |
59fc9d16 |
1 | package CatalystX::DynamicComponent; |
53a42ae0 |
2 | use MooseX::Role::Parameterized; |
0b07685c |
3 | use MooseX::Types::Moose qw/Str CodeRef ArrayRef/; |
046d763d |
4 | use namespace::autoclean; |
59fc9d16 |
5 | |
104abdae |
6 | our $VERSION = 0.000001; |
7 | |
53a42ae0 |
8 | parameter 'name' => ( |
0b07685c |
9 | isa => Str, |
53a42ae0 |
10 | required => 1, |
11 | ); |
12 | |
13 | parameter 'pre_immutable_hook' => ( |
0b07685c |
14 | isa => Str, |
53a42ae0 |
15 | predicate => 'has_pre_immutable_hook', |
16 | ); |
17 | |
cd6bd40d |
18 | parameter 'COMPONENT' => ( |
0b07685c |
19 | isa => CodeRef, |
cd6bd40d |
20 | predicate => 'has_custom_component_method', |
21 | ); |
22 | |
53a42ae0 |
23 | role { |
24 | my $p = shift; |
25 | my $name = $p->name; |
cd6bd40d |
26 | my $pre_immutable_hook = $p->pre_immutable_hook; |
0b07685c |
27 | |
53a42ae0 |
28 | method $name => sub { |
549d6abc |
29 | my ($app, $name, $config, $methods) = @_; |
53a42ae0 |
30 | |
31 | my $appclass = blessed($app) || $app; |
32 | my $type = $name; |
33 | $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this. |
34 | $type =~ s/::.*$//; |
35 | |
36 | my $meta = Moose->init_meta( for_class => $name ); |
0b07685c |
37 | my @superclasses = @{ $config->{superclasses} || [] }; |
38 | push(@superclasses, 'Catalyst::' . $type) unless @superclasses; |
39 | $meta->superclasses(@superclasses); |
40 | |
41 | Moose::Util::apply_all_roles( $meta, @{ $config->{roles}||[] } ) if @{ $config->{roles}||[] }; |
00b934f1 |
42 | |
cd6bd40d |
43 | if ($p->has_custom_component_method) { |
44 | $meta->add_method(COMPONENT => $p->COMPONENT); |
45 | } |
00b934f1 |
46 | |
cd6bd40d |
47 | $app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook; |
00b934f1 |
48 | |
549d6abc |
49 | $methods ||= {}; |
50 | foreach my $name (keys %$methods) { |
51 | $meta->add_method($name => $methods->{$name}); |
52 | } |
53a42ae0 |
53 | $meta->make_immutable; |
54 | |
55 | my $instance = $app->setup_component($name); |
56 | $app->components->{ $name } = $instance; |
57 | }; |
58 | }; |
59fc9d16 |
59 | |
60 | 1; |
61 | |