Rename to Catalyst-View-Component-SubInclude
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude.pm
1 package Catalyst::View::Component::SubInclude;
2 use Moose::Role;
3
4 use Carp qw/croak/;
5 use namespace::clean qw/croak/;
6
7 has 'subinclude_plugin' => (
8     is => 'rw',
9     isa => 'ClassName'
10 ); 
11
12 around 'new' => sub {
13     my $next = shift;
14     my $class = shift;
15     
16     my $self = $class->$next( @_ );
17     
18     my $subinclude_plugin = $self->config->{subinclude_plugin} || 'SubRequest';
19     my $subinclude_class  = __PACKAGE__ . '::' . $subinclude_plugin;
20     
21     eval "require $subinclude_class";
22     croak "Error requiring $subinclude_class: $@" if $@;
23
24     $self->subinclude_plugin( $subinclude_class );
25     
26     $self;
27 };
28
29 around 'render' => sub {
30     my $next = shift;
31     my ($self, $c, @args) = @_;
32     
33     $c->stash->{subinclude} = sub {
34         $self->subinclude_plugin->generate_subinclude( $c, @_ );
35     };
36
37     $self->$next( $c, @args );
38 };
39
40 1;