From: Dave Rolsky Date: Thu, 7 Aug 2008 16:04:16 +0000 (+0000) Subject: Made init_meta a public API again and got rid of init_meta_args in X-Git-Tag: 0_55_01~43^2~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=085fba615666e2297e8bc2d965b37ce4f0eb1182;p=gitmo%2FMoose.git Made init_meta a public API again and got rid of init_meta_args in Moose::Exporter. The theory is that if you want to override the base class or metaclass in your MX extension, you provide your own init_meta which does the right thing. --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 5eb3770..d2cd2f3 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -126,29 +126,24 @@ my $exporter = Moose::Exporter->build_import_methods( ], ); -# This exists for backwards compat sub init_meta { - my ( $class, $base_class, $metaclass ) = @_; - - __PACKAGE__->_init_meta( for_class => $class, - object_base_class => $base_class, - metaclass_class => $metaclass, - ); -} - -# This may be used in some older MooseX extensions. -sub _get_caller { - goto &Moose::Exporter::_get_caller; -} + # This used to be called as a function. This hack preserves + # backwards compatibility. + if ( $_[0] ne __PACKAGE__ ) { + return __PACKAGE__->init_meta( + for_class => $_[0], + base_class => $_[1], + metaclass => $_[2], + ); + } -sub _init_meta { shift; my %args = @_; my $class = $args{for_class} - or confess "Cannot call _init_meta without specifying a for_class"; - my $base_class = $args{object_base_class} || 'Moose::Object'; - my $metaclass = $args{metaclass_class} || 'Moose::Meta::Class'; + or confess "Cannot call init_meta without specifying a for_class"; + my $base_class = $args{base_class} || 'Moose::Object'; + my $metaclass = $args{metaclass} || 'Moose::Meta::Class'; confess "The Metaclass $metaclass must be a subclass of Moose::Meta::Class." @@ -188,9 +183,15 @@ sub _init_meta { $meta->superclasses($base_class) unless $meta->superclasses(); + return $meta; } +# This may be used in some older MooseX extensions. +sub _get_caller { + goto &Moose::Exporter::_get_caller; +} + ## make 'em all immutable $_->meta->make_immutable( diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index c49bc0b..ac98376 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -31,10 +31,7 @@ sub build_import_methods { } ); - my $import = $class->_make_import_sub( - $exporter, - $args{init_meta_args}, - ); + my $import = $class->_make_import_sub($exporter); my $unimport = $class->_make_unimport_sub( [ keys %{$exports} ] ); @@ -157,7 +154,6 @@ sub _process_exports { sub _make_import_sub { my $class = shift; my $exporter = shift; - my $init_meta_args = shift; return sub { @@ -183,10 +179,9 @@ sub _process_exports { return; } - if ( $class->can('_init_meta') ) { - $class->_init_meta( + if ( $class->can('init_meta') ) { + $class->init_meta( for_class => $CALLER, - %{ $init_meta_args || {} } ); } diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 93727ef..e991e47 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -120,7 +120,7 @@ my $exporter = Moose::Exporter->build_import_methods( { my %METAS; - sub _init_meta { + sub init_meta { shift; my %args = @_; diff --git a/t/050_metaclasses/012_moose_exporter.t b/t/050_metaclasses/012_moose_exporter.t index efeb1d6..a436ec1 100644 --- a/t/050_metaclasses/012_moose_exporter.t +++ b/t/050_metaclasses/012_moose_exporter.t @@ -140,6 +140,41 @@ use Test::Exception; } { + package My::Metaclass; + use Moose; + BEGIN { extends 'Moose::Meta::Class' } + + package My::Object; + use Moose; + BEGIN { extends 'Moose::Object' } + + package HasInitMeta; + + use Moose (); + + sub init_meta { + shift; + return Moose->init_meta( @_, + metaclass => 'My::Metaclass', + base_class => 'My::Object', + ); + } + + BEGIN { Moose::Exporter->build_import_methods( also => 'Moose' ); } +} + +{ + package NewMeta; + + BEGIN { HasInitMeta->import() } +} + +{ + isa_ok( NewMeta->meta(), 'My::Metaclass' ); + isa_ok( NewMeta->new(), 'My::Object' ); +} + +{ package MooseX::CircularAlso; use Moose ();