# 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 {
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 {
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 {
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 )
],
# 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";
}
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;
use Moose ();
use Moose::Exporter;
- Moose::Exporter->build_import_methods( also => 'Moose' );
+ Moose::Exporter->setup_import_methods( also => 'Moose' );
sub init_meta {
shift;
some more interesting things to do with your own base class.
This all works because of the magic of L<Moose::Exporter>. When we
-call C<< Moose::Exporter->build_import_methods( also => 'Moose' ) >>
+call C<< Moose::Exporter->setup_import_methods( also => 'Moose' ) >>
it builds an C<import> and C<unimport> method for you. The C<< also =>
'Moose' >> bit says that we want to export everything that Moose does.
use Moose ();
use Moose::Exporter;
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
with_caller => ['has_table'],
also => 'Moose',
);
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;
my $unimport = $class->_make_unimport_sub( \@exports_from, [ keys %{$exports} ] );
- no strict 'refs';
- *{ $exporting_package . '::import' } = $import;
- *{ $exporting_package . '::unimport' } = $unimport;
+ return ( $import, $unimport )
}
{
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' ],
);
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<Moose::Exporter> build custom C<import>
and C<unimport> methods for your module. The import method will export
=back
+=head2 Moose::Exporter->build_import_methods(...)
+
+Returns two code refs, one for import and one for unimport.
+
+Used by C<setup_import_methods>.
+
=head1 IMPORTING AND init_meta
If you want to set an alternative base object class or metaclass
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 )
],
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
use Moose ();
use Moose::Exporter;
- Moose::Exporter->build_import_methods( also => 'Moose' );
+ Moose::Exporter->setup_import_methods( also => 'Moose' );
sub init_meta {
shift;
use Moose ();
use Moose::Exporter;
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
with_caller => ['has_table'],
also => 'Moose',
);
package MooseX::Empty;
use Moose ();
- Moose::Exporter->build_import_methods( also => 'Moose' );
+ Moose::Exporter->setup_import_methods( also => 'Moose' );
}
{
return $caller . ' called wrapped1';
}
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
with_caller => ['wrapped1'],
also => 'Moose',
);
return 'as_is1';
}
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
with_caller => ['wrapped2'],
as_is => ['as_is1'],
also => 'MooseX::Sugar',
);
}
- Moose::Exporter->build_import_methods( also => 'Moose' );
+ Moose::Exporter->setup_import_methods( also => 'Moose' );
}
{
::dies_ok(
sub {
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
also => [ 'Moose', 'MooseX::CircularAlso' ],
);
},
::dies_ok(
sub {
- Moose::Exporter->build_import_methods(
+ Moose::Exporter->setup_import_methods(
also => [ 'NoSuchThing' ],
);
},