X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FObject.pm;h=afcd693fddad21a1cba9d9f37621a8625e53f237;hb=f9b1ab71234d11a9121de96c574ca980bbfb3eaa;hp=1455ab1fe4e372a774b0718c03a91e489c83d355;hpb=080e3614eaacadc5834631fd44ea7ec645927d14;p=gitmo%2FMoose.git diff --git a/lib/Moose/Object.pm b/lib/Moose/Object.pm index 1455ab1..afcd693 100644 --- a/lib/Moose/Object.pm +++ b/lib/Moose/Object.pm @@ -4,18 +4,27 @@ package Moose::Object; use strict; use warnings; +use Scalar::Util; + use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; -our $VERSION = '0.72'; +our $VERSION = '0.74'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; sub new { my $class = shift; + my $params = $class->BUILDARGS(@_); - my $self = $class->meta->new_object($params); + + # We want to support passing $self->new, but initialize + # takes only an unblessed class name + my $real_class = Scalar::Util::blessed($class) || $class; + my $self = Class::MOP::Class->initialize($real_class)->new_object($params); + $self->BUILDALL($params); + return $self; } @@ -23,7 +32,7 @@ sub BUILDARGS { my $class = shift; if ( scalar @_ == 1 ) { unless ( defined $_[0] && ref $_[0] eq 'HASH' ) { - $class->meta->throw_error( + Class::MOP::class_of($class)->throw_error( "Single parameters to new() must be a HASH ref", data => $_[0] ); } @@ -40,18 +49,27 @@ sub BUILDALL { # extra meta level calls return unless $_[0]->can('BUILD'); my ($self, $params) = @_; - foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) { + foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) { $method->{code}->execute($self, $params); } } sub DEMOLISHALL { - my $self = shift; - # NOTE: we ask Perl if we even + my $self = shift; + + # NOTE: we ask Perl if we even # need to do this first, to avoid - # extra meta level calls + # extra meta level calls return unless $self->can('DEMOLISH'); - foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) { + + # This is a hack, because Moose::Meta::Class may not be the right + # metaclass, but class_of may return undef during global + # destruction, if the metaclass object has already been cleaned + # up. + my $meta = Class::MOP::class_of($self) + || Moose::Meta::Class->initialize( ref $self ); + + foreach my $method ( $meta->find_all_methods_by_name('DEMOLISH') ) { $method->{code}->execute($self); } } @@ -84,7 +102,7 @@ BEGIN { # as appropiate see Moose::Meta::Role sub does { my ($self, $role_name) = @_; - my $meta = $self->meta; + my $meta = Class::MOP::class_of($self); (defined $role_name) || $meta->throw_error("You much supply a role name to does()"); foreach my $class ($meta->class_precedence_list) {