This is for the tests, move over there
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent.pm
CommitLineData
59fc9d16 1package CatalystX::DynamicComponent;
53a42ae0 2use MooseX::Role::Parameterized;
0b07685c 3use MooseX::Types::Moose qw/Str CodeRef ArrayRef/;
046d763d 4use namespace::autoclean;
59fc9d16 5
104abdae 6our $VERSION = 0.000001;
7
53a42ae0 8parameter 'name' => (
0b07685c 9 isa => Str,
53a42ae0 10 required => 1,
11);
12
13parameter 'pre_immutable_hook' => (
0b07685c 14 isa => Str,
53a42ae0 15 predicate => 'has_pre_immutable_hook',
16);
17
cd6bd40d 18parameter 'COMPONENT' => (
0b07685c 19 isa => CodeRef,
cd6bd40d 20 predicate => 'has_custom_component_method',
21);
22
53a42ae0 23role {
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 );
279c014c 37
0b07685c 38 my @superclasses = @{ $config->{superclasses} || [] };
39 push(@superclasses, 'Catalyst::' . $type) unless @superclasses;
40 $meta->superclasses(@superclasses);
41
279c014c 42 if (my @roles = @{ $config->{roles}||[] }) {
43 Moose::Util::apply_all_roles( $name, @roles);
44 }
00b934f1 45
cd6bd40d 46 if ($p->has_custom_component_method) {
47 $meta->add_method(COMPONENT => $p->COMPONENT);
48 }
00b934f1 49
cd6bd40d 50 $app->$pre_immutable_hook($meta) if $p->has_pre_immutable_hook;
00b934f1 51
549d6abc 52 $methods ||= {};
53 foreach my $name (keys %$methods) {
54 $meta->add_method($name => $methods->{$name});
55 }
53a42ae0 56 $meta->make_immutable;
57
58 my $instance = $app->setup_component($name);
59 $app->components->{ $name } = $instance;
60 };
61};
59fc9d16 62
631;
64