From: Dave Rolsky Date: Wed, 20 Aug 2008 20:45:16 +0000 (+0000) Subject: Redid this as (mostly) roles which are applied at runtime to the meta X-Git-Tag: 0.06_01~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0cdff4316fbe5eff66d9649dc85d42eefc91f29a;p=gitmo%2FMooseX-StrictConstructor.git Redid this as (mostly) roles which are applied at runtime to the meta & object classes. --- diff --git a/lib/MooseX/StrictConstructor.pm b/lib/MooseX/StrictConstructor.pm index 0e3714b..68b412f 100644 --- a/lib/MooseX/StrictConstructor.pm +++ b/lib/MooseX/StrictConstructor.pm @@ -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__ diff --git a/lib/MooseX/StrictConstructor/Meta/Class.pm b/lib/MooseX/StrictConstructor/Role/Metaclass.pm similarity index 91% rename from lib/MooseX/StrictConstructor/Meta/Class.pm rename to lib/MooseX/StrictConstructor/Role/Metaclass.pm index d3a9694..76505d8 100644 --- a/lib/MooseX/StrictConstructor/Meta/Class.pm +++ b/lib/MooseX/StrictConstructor/Role/Metaclass.pm @@ -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; diff --git a/lib/MooseX/Object/StrictConstructor.pm b/lib/MooseX/StrictConstructor/Role/Object.pm similarity index 86% rename from lib/MooseX/Object/StrictConstructor.pm rename to lib/MooseX/StrictConstructor/Role/Object.pm index 55b5d91..cefb342 100644 --- a/lib/MooseX/Object/StrictConstructor.pm +++ b/lib/MooseX/StrictConstructor/Role/Object.pm @@ -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;