From: gfx Date: Mon, 12 Oct 2009 07:09:33 +0000 (+0900) Subject: Fix role application to instances X-Git-Tag: 0.37_06~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f774b7de063779726b9f84c70f512cab94fee065;p=gitmo%2FMouse.git Fix role application to instances --- diff --git a/lib/Mouse/Meta/Method/Accessor.pm b/lib/Mouse/Meta/Method/Accessor.pm index 9b2d92b..1e788c8 100755 --- a/lib/Mouse/Meta/Method/Accessor.pm +++ b/lib/Mouse/Meta/Method/Accessor.pm @@ -74,8 +74,7 @@ sub _generate_accessor{ $class->throw_error("Unknown accessor type '$type'"); } - # XXX: an anon class can be a runtime created class - if ($attribute->is_lazy || $class->is_anon_class) { + if ($attribute->is_lazy) { my $value; if (defined $builder){ diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index a3579b6..a3d3544 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -179,6 +179,8 @@ sub apply { my %args = (@_ == 1) ? %{ $_[0] } : @_; + my $instance; + if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass $args{_to} = 'class'; } @@ -187,14 +189,12 @@ sub apply { } else{ # Appplication::ToInstance $args{_to} = 'instance'; + $instance = $applicant; - my $metaclass = (Mouse::Util::class_of($applicant) || 'Mouse::Meta::Class')->create_anon_class( - superclasses => [ref $applicant], + $applicant = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class( + superclasses => [ref $instance], cache => 1, ); - bless $applicant, $metaclass->name; # rebless - - $applicant = $metaclass; } if($args{alias} && !exists $args{-alias}){ @@ -224,6 +224,14 @@ sub apply { $self->_apply_methods($applicant, \%args); $self->_apply_modifiers($applicant, \%args); $self->_append_roles($applicant, \%args); + + + if(defined $instance){ # Application::ToInstance + # rebless instance + bless $instance, $applicant->name; + $applicant->_initialize_object($instance, $instance); + } + return; }