From: Dave Rolsky Date: Mon, 28 Jul 2008 12:36:17 +0000 (+0000) Subject: Copy caching from ToInstance application X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=296a6f0db81d3246ce02d1388bf096b10bd077c3;p=gitmo%2FMoose.git Copy caching from ToInstance application --- diff --git a/lib/Moose/Meta/Role/Application/ToMetaclassInstance.pm b/lib/Moose/Meta/Role/Application/ToMetaclassInstance.pm index 0abe3e1..73aec16 100644 --- a/lib/Moose/Meta/Role/Application/ToMetaclassInstance.pm +++ b/lib/Moose/Meta/Role/Application/ToMetaclassInstance.pm @@ -19,15 +19,24 @@ __PACKAGE__->meta->add_attribute('rebless_params' => ( my %ANON_CLASSES; sub apply { - my ($self, $role, $meta) = @_; + my ( $self, $role, $meta ) = @_; - my $class = (blessed $meta)->create_anon_class( - superclasses => [ blessed($meta) ] - ); + my $anon_role_key = (blessed($meta) . $role->name); - $self->SUPER::apply($role, $class); + my $class; + if (exists $ANON_CLASSES{$anon_role_key} && defined $ANON_CLASSES{$anon_role_key}) { + $class = $ANON_CLASSES{$anon_role_key}; + } + else { + $class = blessed($meta)->create_anon_class( + superclasses => [ blessed($meta) ], + ); - $class->rebless_instance($meta, %{$self->rebless_params}); + $ANON_CLASSES{$anon_role_key} = $class; + $self->SUPER::apply( $role, $class ); + } + + $class->rebless_instance( $meta, %{ $self->rebless_params } ); } 1;