From: gfx Date: Wed, 9 Jun 2010 10:25:00 +0000 (+0900) Subject: Follow Moose's new feature: BUILDALL is called by new_object(), not by Mouse::Object... X-Git-Tag: 0.60~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b5956f03156c79cfbdd20f9526f95a9df5aa26b4;p=gitmo%2FMouse.git Follow Moose's new feature: BUILDALL is called by new_object(), not by Mouse::Object::new --- diff --git a/lib/Mouse/PurePerl.pm b/lib/Mouse/PurePerl.pm index 6322710..643795a 100644 --- a/lib/Mouse/PurePerl.pm +++ b/lib/Mouse/PurePerl.pm @@ -270,12 +270,21 @@ sub get_all_attributes { } sub new_object { - my $self = shift; + my $meta = shift; my %args = (@_ == 1 ? %{$_[0]} : @_); - my $object = bless {}, $self->name; + my $object = bless {}, $meta->name; - $self->_initialize_object($object, \%args); + $meta->_initialize_object($object, \%args); + # BUILDALL + if( $object->can('BUILD') ) { + for my $class (reverse $meta->linearized_isa) { + my $build = Mouse::Util::get_code_ref($class, 'BUILD') + || next; + + $object->$build(\%args); + } + } return $object; } @@ -647,19 +656,7 @@ sub new { my $args = $class->BUILDARGS(@_); my $meta = Mouse::Meta::Class->initialize($class); - my $self = $meta->new_object($args); - - # BUILDALL - if( $self->can('BUILD') ) { - for my $class (reverse $meta->linearized_isa) { - my $build = Mouse::Util::get_code_ref($class, 'BUILD') - || next; - - $self->$build($args); - } - } - - return $self; + return $meta->new_object($args); } sub DESTROY { diff --git a/t/001_mouse/014-build.t b/t/001_mouse/014-build.t index b1e3293..c9a16f2 100644 --- a/t/001_mouse/014-build.t +++ b/t/001_mouse/014-build.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 9; +use Test::More; use Test::Mouse; my @called; @@ -51,5 +51,9 @@ with_immutable sub { $child->BUILDALL({}); is_deeply([splice @called], ["Class::BUILD", "Child::BUILD"], 'BUILDALL'); + + $child = Child->meta->new_object(); + is_deeply([splice @called], ["Class::BUILD", "Child::BUILD"], 'new_object calls BUILDALL'); }, qw(Class Child); +done_testing; diff --git a/xs-src/Mouse.xs b/xs-src/Mouse.xs index 7c594c4..c47072d 100644 --- a/xs-src/Mouse.xs +++ b/xs-src/Mouse.xs @@ -580,6 +580,7 @@ CODE: RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc)); mouse_class_initialize_object(aTHX_ meta, RETVAL, args, FALSE); + mouse_buildall(aTHX_ xc, RETVAL, args); /* BUILDALL */ } OUTPUT: RETVAL @@ -673,8 +674,7 @@ CODE: /* new_object */ RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc)); mouse_class_initialize_object(aTHX_ meta, RETVAL, (HV*)SvRV(args), FALSE); - - mouse_buildall(aTHX_ xc, RETVAL, args); + mouse_buildall(aTHX_ xc, RETVAL, args); /* BUILDALL */ } OUTPUT: RETVAL