From: Dave Rolsky Date: Thu, 21 Aug 2008 15:43:12 +0000 (+0000) Subject: redo constructor class as a role too X-Git-Tag: 0.06_01~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=64c958efc2fc078bc77a92c651518e7963051427;p=gitmo%2FMooseX-StrictConstructor.git redo constructor class as a role too --- diff --git a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm b/lib/MooseX/StrictConstructor/Role/Constructor.pm similarity index 85% rename from lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm rename to lib/MooseX/StrictConstructor/Role/Constructor.pm index 19547d3..54820df 100644 --- a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm +++ b/lib/MooseX/StrictConstructor/Role/Constructor.pm @@ -1,18 +1,18 @@ -package MooseX::StrictConstructor::Meta::Method::Constructor; +package MooseX::StrictConstructor::Role::Constructor; use strict; use warnings; use Carp (); -use Moose; -extends 'Moose::Meta::Method::Constructor'; +use Moose::Role; -override '_generate_BUILDALL' => sub +around '_generate_BUILDALL' => sub { + my $orig = shift; my $self = shift; - my $source = super(); + my $source = $self->$orig(); $source .= ";\n" if $source; my @attrs = @@ -35,8 +35,7 @@ EOF return $source; }; -no Moose; - +no Moose::Role; 1; diff --git a/lib/MooseX/StrictConstructor/Role/Metaclass.pm b/lib/MooseX/StrictConstructor/Role/Metaclass.pm index d2c0239..e96b981 100644 --- a/lib/MooseX/StrictConstructor/Role/Metaclass.pm +++ b/lib/MooseX/StrictConstructor/Role/Metaclass.pm @@ -7,7 +7,24 @@ use MooseX::StrictConstructor::Meta::Method::Constructor; use Moose::Role; +has 'constructor_class' => + ( is => 'ro', + isa => 'ClassName', + lazy_build => 1, + ); +sub _build_constructor_class +{ + return + Moose::Meta::Class->create_anon_class + ( superclasses => [ 'Moose::Meta::Method::Constructor' ], + roles => [ 'MooseX::StrictConstructor::Role::Constructor' ], + cache => 1, + )->name(); +} + +# If Moose::Meta::Class had a constructor_class attribute, this +# wrapper would not be necessary. around 'make_immutable' => sub { my $orig = shift; @@ -15,7 +32,7 @@ around 'make_immutable' => sub return $self->$orig - ( constructor_class => 'MooseX::StrictConstructor::Meta::Method::Constructor', + ( constructor_class => $self->constructor_class(), @_, ); };