stick the call to BUILDALL in ->meta->new_object, rather than ->new
Jesse Luehrs [Wed, 31 Mar 2010 20:10:10 +0000 (15:10 -0500)]
Changes
Makefile.PL
lib/Moose/Manual/Delta.pod
lib/Moose/Meta/Class.pm
lib/Moose/Object.pm
t/050_metaclasses/052_new_object_BUILD.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 789a9fa..a40a91d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -7,6 +7,9 @@ for, noteworthy changes.
     moved back up into Moose::Meta::Class and Moose::Meta::Role individually
     (through the Class::MOP::Mixin::HasMethods mixin) (doy).
 
+  * BUILDALL is now called by Moose::Meta::Class::new_object, rather than by
+    Moose::Object::new. (doy)
+
   [BUG FIXES]
 
   * Fix has '+attr' in Roles to explode immediately, rather than when the role
index a4af8ec..80c33be 100644 (file)
@@ -55,7 +55,7 @@ sub check_conflicts {
         'MooseX::Attribute::Prototype'  => '0.10',
         'MooseX::ClassAttribute'        => '0.09',
         'MooseX::MethodAttributes'      => '0.18',
-        'MooseX::NonMoose'              => '0.05',
+        'MooseX::NonMoose'              => '0.07',
         'MooseX::Params::Validate'      => '0.05',
         'MooseX::Role::Cmd'             => '0.06',
         'MooseX::Role::WithOverloading' => '0.04',
index 2aef081..81e097a 100644 (file)
@@ -16,6 +16,18 @@ feature.  If you encounter a problem and have a solution but don't see
 it documented here, or think we missed an important feature, please
 send us a patch.
 
+=over 4
+
+=item L<Moose::Object/BUILD> methods are now called when calling C<new_object>
+
+Previously, C<BUILD> methods would only be called from C<Moose::Object::new>,
+but now they are also called when constructing an object via
+C<Moose::Meta::Class::new_object>. C<BUILD> methods are an inherent part of the
+object construction process, and this should make C<< $meta->new_object >>
+actually usable without forcing people to use C<< $meta->name->new >>.
+
+=back
+
 =head1 1.02
 
 =over 4
index 0dd87c7..05da231 100644 (file)
@@ -275,6 +275,8 @@ sub new_object {
         );
     }
 
+    $self->BUILDALL($params) if $self->can('BUILDALL');
+
     return $self;
 }
 
index 14941b3..aacb0e1 100644 (file)
@@ -18,15 +18,11 @@ our $AUTHORITY = 'cpan:STEVAN';
 
 sub new {
     my $class = shift;
-
-    my $params = $class->BUILDARGS(@_);
-
     my $real_class = Scalar::Util::blessed($class) || $class;
-    my $self = Class::MOP::Class->initialize($real_class)->new_object($params);
 
-    $self->BUILDALL($params);
+    my $params = $real_class->BUILDARGS(@_);
 
-    return $self;
+    return Class::MOP::Class->initialize($real_class)->new_object($params);
 }
 
 sub BUILDARGS {
diff --git a/t/050_metaclasses/052_new_object_BUILD.t b/t/050_metaclasses/052_new_object_BUILD.t
new file mode 100644 (file)
index 0000000..b3bdd8c
--- /dev/null
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+my $called;
+{
+    package Foo;
+    use Moose;
+
+    sub BUILD { $called++ }
+}
+
+Foo->new;
+is($called, 1, "BUILD called from ->new");
+$called = 0;
+Foo->meta->new_object;
+is($called, 1, "BUILD called from ->meta->new_object");
+
+done_testing;