X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FView%2FComponent%2FSubInclude.pm;h=57fd5caa16e714612aaa3805f0f10071965f6859;hb=cc37b7b32b018f25612606a817a0588d10acdf7a;hp=dd0caeaeb23e7be5663a08c1f49a780f21d0c1b1;hpb=f97c716560e114d118695339a200fe5a7e09e9e5;p=catagits%2FCatalyst-View-Component-SubInclude.git diff --git a/lib/Catalyst/View/Component/SubInclude.pm b/lib/Catalyst/View/Component/SubInclude.pm index dd0caea..57fd5ca 100644 --- a/lib/Catalyst/View/Component/SubInclude.pm +++ b/lib/Catalyst/View/Component/SubInclude.pm @@ -3,6 +3,8 @@ use Moose::Role; use Carp qw/croak/; use Catalyst::Utils (); +use Class::MOP (); +use MooseX::Types::Moose qw/Str HashRef/; use namespace::clean -except => 'meta'; with 'Catalyst::Component::ContextClosure'; @@ -13,11 +15,12 @@ Catalyst::View::Component::SubInclude - Use subincludes in your Catalyst views =head1 VERSION -Version 0.07 +Version 0.07_01 =cut -our $VERSION = '0.07'; +our $VERSION = '0.07_01'; +$VERSION = eval $VERSION; =head1 SYNOPSIS @@ -107,7 +110,7 @@ in runtime. It expects a fully qualified class name. has 'subinclude_plugin' => ( is => 'rw', - isa => 'Str' + isa => Str, ); around 'new' => sub { @@ -132,7 +135,7 @@ before 'render' => sub { 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 ); } @@ -143,24 +146,32 @@ sub _subinclude { sub _subinclude_using { my ($self, $c, $plugin, @args) = @_; - $plugin = $self->_subinclude_plugin_class_name($plugin); - my $plugin_config = Catalyst::Utils::merge_hashes($self->config->{subinclude}->{ALL}||{}, $self->config->{subinclude}->{$plugin}||{}); - $plugin->new($plugin_config)->generate_subinclude( $c, @args ); + $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 $cache = $self->_subinclude_plugin_class_instance_cache; + return $cache->{$plugin} if exists $cache->{$plugin}; - my $class_name = __PACKAGE__ . '::' . $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