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