use Moose::Role;
use Carp qw/croak/;
-use namespace::clean qw/croak/;
+use Catalyst::Utils ();
+use Class::MOP ();
+use MooseX::Types::Moose qw/Str HashRef/;
+use namespace::clean -except => 'meta';
+
+with 'Catalyst::Component::ContextClosure';
=head1 NAME
=head1 VERSION
-Version 0.03
+Version 0.07_01
=cut
-our $VERSION = '0.03';
+our $VERSION = '0.07_01';
+$VERSION = eval $VERSION;
=head1 SYNOPSIS
has 'subinclude_plugin' => (
is => 'rw',
- isa => 'ClassName'
+ isa => Str,
);
around 'new' => sub {
$self;
};
-around 'render' => sub {
- my $next = shift;
+before 'render' => sub {
my ($self, $c, @args) = @_;
-
- $c->stash->{subinclude} = sub { $self->_subinclude( $c, @_ ) };
- $c->stash->{subinclude_using} = sub { $self->_subinclude_using( $c, @_ ) };
- $self->$next( $c, @args );
+ $c->stash->{subinclude} = $self->make_context_closure(sub { $self->_subinclude( @_ ) }, $c);
+ $c->stash->{subinclude_using} = $self->make_context_closure(sub { $self->_subinclude_using( @_ ) }, $c);
};
sub set_subinclude_plugin {
my ($self, $plugin) = @_;
- my $subinclude_class = $self->_subinclude_plugin_class_name( $plugin );
+ my $subinclude_class = blessed $self->_subinclude_plugin_class_instance( $plugin );
$self->subinclude_plugin( $subinclude_class );
}
sub _subinclude_using {
my ($self, $c, $plugin, @args) = @_;
- $plugin = $self->_subinclude_plugin_class_name($plugin);
+ $plugin = $self->_subinclude_plugin_class_instance($plugin);
$plugin->generate_subinclude( $c, @args );
}
-sub _subinclude_plugin_class_name {
+has _subinclude_plugin_class_instance_cache => (
+ isa => HashRef,
+ is => 'ro',
+ default => sub { {} },
+);
+
+sub _subinclude_plugin_class_instance {
my ($self, $plugin) = @_;
- # check if name is already fully qualified
- my $pkg = __PACKAGE__;
- return $plugin if $plugin =~ /^$pkg/;
+ my $class = $plugin =~ /::/ ? $plugin : __PACKAGE__ . '::' . $plugin;
- my $class_name = __PACKAGE__ . '::' . $plugin;
+ my $cache = $self->_subinclude_plugin_class_instance_cache;
+ return $cache->{$plugin} if exists $cache->{$plugin};
+
+ my $plugin_config = Catalyst::Utils::merge_hashes(
+ $self->config->{subinclude}->{ALL}||{},
+ $self->config->{subinclude}->{$plugin}||{}
+ );
- eval "require $class_name";
- croak "Error requiring $class_name: $@" if $@;
+ Class::MOP::load_class($class);
- $class_name;
+ return $cache->{$plugin} = $class->new($plugin_config);
}
=head1 SEE ALSO
Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
+=head1 CONTRIBUTORS
+
+Tomas Doran (t0m) C<< <bobtfish@bobtfish.net >>.
+
=head1 SPONSORSHIP
Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
=head1 COPYRIGHT & LICENSE
+Copyright (C) 2010 Nilson Santos Figueiredo Junior and the above contributors.
+
Copyright (C) 2009 Nilson Santos Figueiredo Junior.
Copyright (C) 2009 Ionzero LLC.