From: Yuval Kogman Date: Wed, 13 Aug 2008 21:31:39 +0000 (+0000) Subject: Moose::Exporter fixes + rename build_import_methods to setup_import_methods X-Git-Tag: 0_55_01~30 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aedcb7d99df0e502f1a528e009206ea080d04071;p=gitmo%2FMoose.git Moose::Exporter fixes + rename build_import_methods to setup_import_methods --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 32cc401..74fb31a 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -45,13 +45,13 @@ sub extends { # this checks the metaclass to make sure # it is correct, sometimes it can get out # of sync when the classes are being built - my $meta = $class->meta->_fix_metaclass_incompatability(@supers); + my $meta = Class::MOP::Class->initialize($class)->_fix_metaclass_incompatability(@supers); $meta->superclasses(@supers); } sub with { my $class = shift; - Moose::Util::apply_all_roles($class->meta, @_); + Moose::Util::apply_all_roles(Class::MOP::Class->initialize($class), @_); } sub has { @@ -60,7 +60,7 @@ sub has { croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1; my %options = @_; my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; - $class->meta->add_attribute( $_, %options ) for @$attrs; + Class::MOP::Class->initialize($class)->add_attribute( $_, %options ) for @$attrs; } sub before { @@ -85,7 +85,7 @@ sub super { sub override { my $class = shift; my ( $name, $method ) = @_; - $class->meta->add_override_method_modifier( $name => $method ); + Class::MOP::Class->initialize($class)->add_override_method_modifier( $name => $method ); } sub inner { @@ -105,17 +105,17 @@ sub inner { sub augment { my $class = shift; my ( $name, $method ) = @_; - $class->meta->add_augment_method_modifier( $name => $method ); + Class::MOP::Class->initialize($class)->add_augment_method_modifier( $name => $method ); } sub make_immutable { my $class = shift; cluck "The make_immutable keyword has been deprecated, " . "please go back to __PACKAGE__->meta->make_immutable\n"; - $class->meta->make_immutable(@_); + Class::MOP::Class->initialize($class)->make_immutable(@_); } -my $exporter = Moose::Exporter->build_import_methods( +Moose::Exporter->setup_import_methods( with_caller => [ qw( extends with has before after around override augment make_immutable ) ], @@ -159,7 +159,7 @@ sub init_meta { # this is the case where the metaclass pragma # was used before the 'use Moose' statement to # override a specific class - $meta = $class->meta(); + $meta = Class::MOP::Class->initialize($class); ( blessed($meta) && $meta->isa('Moose::Meta::Class') ) || confess "You already have a &meta function, but it does not return a Moose::Meta::Class"; } @@ -776,7 +776,7 @@ Here is a simple example: use Moose (); # no need to get Moose's exports use Moose::Exporter; - Moose::Exporter->build_import_methods( also => 'Moose' ); + Moose::Exporter->setup_import_methods( also => 'Moose' ); sub init_meta { shift; diff --git a/lib/Moose/Cookbook/Extending/Recipe1.pod b/lib/Moose/Cookbook/Extending/Recipe1.pod index 8dc84aa..47f89f9 100644 --- a/lib/Moose/Cookbook/Extending/Recipe1.pod +++ b/lib/Moose/Cookbook/Extending/Recipe1.pod @@ -20,7 +20,7 @@ Moose::Cookbook::Extending::Recipe1 - Providing an alternate base object class use Moose (); use Moose::Exporter; - Moose::Exporter->build_import_methods( also => 'Moose' ); + Moose::Exporter->setup_import_methods( also => 'Moose' ); sub init_meta { shift; @@ -44,7 +44,7 @@ output every time a new object is created, but you can surely think of some more interesting things to do with your own base class. This all works because of the magic of L. When we -call C<< Moose::Exporter->build_import_methods( also => 'Moose' ) >> +call C<< Moose::Exporter->setup_import_methods( also => 'Moose' ) >> it builds an C and C method for you. The C<< also => 'Moose' >> bit says that we want to export everything that Moose does. diff --git a/lib/Moose/Cookbook/Extending/Recipe2.pod b/lib/Moose/Cookbook/Extending/Recipe2.pod index 6f65169..98a2ea7 100644 --- a/lib/Moose/Cookbook/Extending/Recipe2.pod +++ b/lib/Moose/Cookbook/Extending/Recipe2.pod @@ -15,7 +15,7 @@ Moose::Cookbook::Extending::Recipe2 - Acting like Moose.pm and providing sugar M use Moose (); use Moose::Exporter; - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( with_caller => ['has_table'], also => 'Moose', ); diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 6aef5fe..53165c1 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -11,11 +11,22 @@ use Sub::Exporter; my %EXPORT_SPEC; +sub setup_import_methods { + my ( $class, %args ) = @_; + + my $exporting_package = $args{exporting_package} ||= caller(); + + my ( $import, $unimport) = $class->build_import_methods( %args ); + + no strict 'refs'; + *{ $exporting_package . '::import' } = $import; + *{ $exporting_package . '::unimport' } = $unimport; +} + sub build_import_methods { - my $class = shift; - my %args = @_; + my ( $class, %args ) = @_; - my $exporting_package = caller(); + my $exporting_package = $args{exporting_package} ||= caller(); $EXPORT_SPEC{$exporting_package} = \%args; @@ -38,9 +49,7 @@ sub build_import_methods { my $unimport = $class->_make_unimport_sub( \@exports_from, [ keys %{$exports} ] ); - no strict 'refs'; - *{ $exporting_package . '::import' } = $import; - *{ $exporting_package . '::unimport' } = $unimport; + return ( $import, $unimport ) } { @@ -331,7 +340,7 @@ Moose::Exporter - make an import() and unimport() just like Moose.pm use Moose (); use Moose::Exporter; - Moose::Exporter->build_export_methods( + Moose::Exporter->setup_import_methods( export => [ 'sugar1', 'sugar2', \&Some::Random::thing ], init_meta_args => { metaclass_class => 'MyApp::Meta::Class' ], ); @@ -361,7 +370,7 @@ C module, as long as they all use C. This module provides exactly one public method: -=head2 Moose::Exporter->build_import_methods(...) +=head2 Moose::Exporter->setup_import_methods(...) When you call this method, C build custom C and C methods for your module. The import method will export @@ -400,6 +409,12 @@ when C is called. =back +=head2 Moose::Exporter->build_import_methods(...) + +Returns two code refs, one for import and one for unimport. + +Used by C. + =head1 IMPORTING AND init_meta If you want to set an alternative base object class or metaclass diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 14f4d55..4d2bd67 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -106,7 +106,7 @@ sub augment { croak "Moose::Role cannot support 'augment'"; } -my $exporter = Moose::Exporter->build_import_methods( +my $exporter = Moose::Exporter->setup_import_methods( with_caller => [ qw( with requires excludes has before after around override make_immutable ) ], diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 59fb475..26a3358 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -62,7 +62,7 @@ use Moose::Meta::TypeCoercion::Union; use Moose::Meta::TypeConstraint::Registry; use Moose::Util::TypeConstraints::OptimizedConstraints; -Moose::Exporter->build_import_methods( +Moose::Exporter->setup_import_methods( as_is => [ qw( type subtype class_type role_type as where message optimize_as diff --git a/t/000_recipes/extending/001_base_class.t b/t/000_recipes/extending/001_base_class.t index 2e305d3..f140fd4 100644 --- a/t/000_recipes/extending/001_base_class.t +++ b/t/000_recipes/extending/001_base_class.t @@ -31,7 +31,7 @@ BEGIN { use Moose (); use Moose::Exporter; - Moose::Exporter->build_import_methods( also => 'Moose' ); + Moose::Exporter->setup_import_methods( also => 'Moose' ); sub init_meta { shift; diff --git a/t/000_recipes/extending/002_metaclass_and_sugar.t b/t/000_recipes/extending/002_metaclass_and_sugar.t index fc930f8..1ecbf17 100644 --- a/t/000_recipes/extending/002_metaclass_and_sugar.t +++ b/t/000_recipes/extending/002_metaclass_and_sugar.t @@ -24,7 +24,7 @@ use Test::More tests => 3; use Moose (); use Moose::Exporter; - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( with_caller => ['has_table'], also => 'Moose', ); diff --git a/t/050_metaclasses/012_moose_exporter.t b/t/050_metaclasses/012_moose_exporter.t index 8965d1c..612196d 100644 --- a/t/050_metaclasses/012_moose_exporter.t +++ b/t/050_metaclasses/012_moose_exporter.t @@ -37,7 +37,7 @@ BEGIN { package MooseX::Empty; use Moose (); - Moose::Exporter->build_import_methods( also => 'Moose' ); + Moose::Exporter->setup_import_methods( also => 'Moose' ); } { @@ -80,7 +80,7 @@ BEGIN { return $caller . ' called wrapped1'; } - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( with_caller => ['wrapped1'], also => 'Moose', ); @@ -124,7 +124,7 @@ BEGIN { return 'as_is1'; } - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( with_caller => ['wrapped2'], as_is => ['as_is1'], also => 'MooseX::Sugar', @@ -184,7 +184,7 @@ BEGIN { ); } - Moose::Exporter->build_import_methods( also => 'Moose' ); + Moose::Exporter->setup_import_methods( also => 'Moose' ); } { @@ -205,7 +205,7 @@ BEGIN { ::dies_ok( sub { - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( also => [ 'Moose', 'MooseX::CircularAlso' ], ); }, @@ -226,7 +226,7 @@ BEGIN { ::dies_ok( sub { - Moose::Exporter->build_import_methods( + Moose::Exporter->setup_import_methods( also => [ 'NoSuchThing' ], ); },