Fix a code typo
[gitmo/Moose.git] / lib / Moose / Object.pm
index 829c90d..afcd693 100644 (file)
@@ -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.73';
+our $VERSION   = '0.74';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 sub new {
     my $class = shift;
+
     my $params = $class->BUILDARGS(@_);
-    my $self = Class::MOP::Class->initialize($class)->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;
 }
 
@@ -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) {