Start my reflector. Factor out the component generation from the other shit into...
[catagits/CatalystX-DynamicComponent.git] / lib / CatalystX / DynamicComponent.pm
1 package CatalystX::DynamicComponent;
2 use Moose::Role;
3 use namespace::clean -excpept => 'meta';
4
5 sub _setup_dynamic_component {
6     my ($app, $name, $config) = @_;
7
8     my $appclass = blessed($app) || $app;
9     my $type = $name;
10     $type =~ s/^${appclass}:://; # FIXME - I think there is shit in C::Utils to do this.
11     $type =~ s/::.*$//;
12
13     my $meta = Moose->init_meta( for_class => $name );
14     $meta->superclasses('Catalyst::' . $type);
15
16     $meta->add_method(
17
18       COMPONENT
19             => sub {
20         my ($component_class_name, $app, $args) = @_;
21
22         my $class = delete $args->{class};
23         Class::MOP::load_class($class);
24
25         $class->new($args);
26     });
27
28     $meta->make_immutable;
29
30     my $instance = $app->setup_component($name);
31     $app->components->{ $name } = $instance;
32 }
33
34 1;
35