bump version to 0.66
[gitmo/Moose.git] / lib / Moose / Util / MetaRole.pm
index f0b53c4..f3e8bf3 100644 (file)
@@ -3,24 +3,36 @@ package Moose::Util::MetaRole;
 use strict;
 use warnings;
 
+our $VERSION   = '0.66';
+$VERSION = eval $VERSION;
+our $AUTHORITY = 'cpan:STEVAN';
+
 use List::MoreUtils qw( all );
 
+my @Classes = qw( constructor_class destructor_class error_class );
+
 sub apply_metaclass_roles {
     my %options = @_;
 
     my $for = $options{for_class};
 
-    my $meta = _make_new_metaclass( $for, \%options );
+    my %old_classes
+        = map { $_ => $for->meta->$_ } grep { $for->meta->can($_) } @Classes;
 
-    for my $tor_class ( grep { $options{ $_ . '_roles' } }
-        qw( constructor_class destructor_class ) ) {
+    my $meta = _make_new_metaclass( $for, \%options );
 
-        my $class = _make_new_class(
-            $meta->$tor_class(),
-            $options{ $tor_class . '_roles' }
-        );
+    for my $c ( grep { $meta->can($_) } @Classes ) {
+        if ( $options{ $c . '_roles' } ) {
+            my $class = _make_new_class(
+                $meta->$c(),
+                $options{ $c . '_roles' }
+            );
 
-        $meta->$tor_class($class);
+            $meta->$c($class);
+        }
+        else {
+            $meta->$c( $old_classes{$c} );
+        }
     }
 
     return $meta;
@@ -82,7 +94,7 @@ sub _make_new_class {
 
     return $existing_class unless $roles;
 
-    my $meta = $existing_class->meta();
+    my $meta = Class::MOP::Class->initialize($existing_class);
 
     return $existing_class
         if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
@@ -147,7 +159,7 @@ experimental, and it could go away in the future!>
 This utility module is designed to help authors of Moose extensions
 write extensions that are able to cooperate with other Moose
 extensions. To do this, you must write your extensions as roles, which
-can then be dynamically applyied to the caller's metaclasses.
+can then be dynamically applied to the caller's metaclasses.
 
 This module makes sure to preserve any existing superclasses and roles
 already set for the meta objects, which means that any number of
@@ -203,13 +215,51 @@ once.
 
 This function will apply the specified roles to the object's base class.
 
+=head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
+
+Because of the way this module works, there is an ordering problem
+which occurs in certain situations. This sequence of events causes an
+error:
+
+=over 4
+
+=item 1.
+
+There is a class (ClassA) which uses some extension(s) that apply
+roles to the metaclass.
+
+=item 2.
+
+You have another class (ClassB) which wants to subclass ClassA and
+apply some more extensions.
+
+=back
+
+Normally, the call to C<extends> will happen at run time, I<after> the
+additional extensions are applied. This causes an error when we try to
+make the metaclass for ClassB compatible with the metaclass for
+ClassA.
+
+We hope to be able to fix this in the future.
+
+For now the workaround is for ClassB to make sure it extends ClassA
+I<before> it loads extensions:
+
+  package ClassB;
+
+  use Moose;
+
+  BEGIN { extends 'ClassA' }
+
+  use MooseX::SomeExtension;
+
 =head1 AUTHOR
 
 Dave Rolsky E<lt>autarch@urth.orgE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2008 by Infinity Interactive, Inc.
+Copyright 2009 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>