Redid this as (mostly) roles which are applied at runtime to the meta
Dave Rolsky [Wed, 20 Aug 2008 20:45:16 +0000 (20:45 +0000)]
& object classes.

lib/MooseX/StrictConstructor.pm
lib/MooseX/StrictConstructor/Role/Metaclass.pm [moved from lib/MooseX/StrictConstructor/Meta/Class.pm with 91% similarity]
lib/MooseX/StrictConstructor/Role/Object.pm [moved from lib/MooseX/Object/StrictConstructor.pm with 86% similarity]

index 0e3714b..68b412f 100644 (file)
@@ -5,28 +5,54 @@ use warnings;
 
 our $VERSION = '0.06';
 
-use Moose;
-use MooseX::Object::StrictConstructor;
+use Class::MOP ();
+use Moose ();
+use Moose::Exporter;
+use MooseX::StrictConstructor::Role::Object;
+use MooseX::StrictConstructor::Role::Metaclass;
 
+Moose::Exporter->setup_import_methods( also => 'Moose' );
 
-sub import
+sub init_meta
 {
-    my $caller = caller();
-
-    return if $caller eq 'main';
-
-    Moose::init_meta( $caller,
-                      'MooseX::Object::StrictConstructor',
-                      'MooseX::StrictConstructor::Meta::Class',
-                    );
-
-    Moose->import( { into => $caller } );
-
-    return;
+    shift;
+    my %p = @_;
+
+    Moose->init_meta(%p);
+
+    my $caller = $p{for_class};
+
+    my $metameta = $caller->meta()->meta();
+    unless ( $metameta->can('does_role')
+             && $metameta->does_role( 'MooseX::StrictConstructor::Role::Metaclass' ) )
+    {
+        my $new_meta =
+            Moose::Meta::Class->create_anon_class
+                ( superclasses => [ ref $caller->meta() ],
+                  roles        => [ 'MooseX::StrictConstructor::Role::Metaclass' ],
+                  cache        => 1,
+                );
+
+        Class::MOP::remove_metaclass_by_name($caller);
+
+        $new_meta->name()->initialize($caller);
+    }
+
+    unless ( $caller->meta()->does_role('MooseX::StrictConstructor::Role::Object') )
+    {
+        my $new_base =
+            Moose::Meta::Class->create_anon_class
+                ( superclasses => [ $caller->meta()->superclasses() ],
+                  roles        => [ 'MooseX::StrictConstructor::Role::Object' ],
+                  cache        => 1,
+                );
+
+        $caller->meta()->superclasses( $new_base->name() );
+    }
+
+    return $caller->meta();
 }
 
-
-
 1;
 
 __END__
similarity index 91%
rename from lib/MooseX/StrictConstructor/Meta/Class.pm
rename to lib/MooseX/StrictConstructor/Role/Metaclass.pm
index d3a9694..76505d8 100644 (file)
@@ -1,13 +1,12 @@
-package MooseX::StrictConstructor::Meta::Class;
+package MooseX::StrictConstructor::Role::Metaclass;
 
 use strict;
 use warnings;
 
 use MooseX::StrictConstructor::Meta::Method::Constructor;
 
-use Moose;
+use Moose::Role;
 
-extends 'Moose::Meta::Class';
 
 around 'make_immutable' => sub ## no critic RequireArgUnpacking
 {
@@ -21,7 +20,7 @@ around 'make_immutable' => sub ## no critic RequireArgUnpacking
             );
 };
 
-no Moose;
+no Moose::Role;
 
 
 1;
similarity index 86%
rename from lib/MooseX/Object/StrictConstructor.pm
rename to lib/MooseX/StrictConstructor/Role/Object.pm
index 55b5d91..cefb342 100644 (file)
@@ -1,16 +1,10 @@
-package MooseX::Object::StrictConstructor;
+package MooseX::StrictConstructor::Role::Object;
 
 use strict;
 use warnings;
 
-use Moose;
+use Moose::Role;
 
-use Carp 'confess';
-
-use metaclass 'MooseX::StrictConstructor::Meta::Class';
-
-
-extends 'Moose::Object';
 
 after 'BUILDALL' => sub
 {
@@ -34,6 +28,7 @@ after 'BUILDALL' => sub
     return;
 };
 
+no Moose::Role;
 
 1;