From: Dave Rolsky Date: Mon, 7 Sep 2009 16:38:09 +0000 (-0500) Subject: Use with_meta in Moose, Moose::Role, and docs (rather than with_caller) X-Git-Tag: 0.89_02~32^2~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d5447d26a00923150fe0645a05ab00c574de5395;p=gitmo%2FMoose.git Use with_meta in Moose, Moose::Role, and docs (rather than with_caller) --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 205b0d4..5ea1d74 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -43,46 +43,42 @@ sub throw_error { } sub extends { - my $class = shift; + my $meta = shift; Moose->throw_error("Must derive at least one class") unless @_; # this checks the metaclass to make sure # it is correct, sometimes it can get out # of sync when the classes are being built - Moose::Meta::Class->initialize($class)->superclasses(@_); + $meta->superclasses(@_); } sub with { - my $class = shift; - Moose::Util::apply_all_roles(Class::MOP::Class->initialize($class), @_); + Moose::Util::apply_all_roles(shift, @_); } sub has { - my $class = shift; - my $name = shift; + my $meta = shift; + my $name = shift; Moose->throw_error('Usage: has \'name\' => ( key => value, ... )') if @_ % 2 == 1; my %options = ( definition_context => Moose::Util::_caller_info(), @_ ); my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; - Class::MOP::Class->initialize($class)->add_attribute( $_, %options ) for @$attrs; + $meta->add_attribute( $_, %options ) for @$attrs; } sub before { - my $class = shift; - Moose::Util::add_method_modifier($class, 'before', \@_); + Moose::Util::add_method_modifier(shift, 'before', \@_); } sub after { - my $class = shift; - Moose::Util::add_method_modifier($class, 'after', \@_); + Moose::Util::add_method_modifier(shift, 'after', \@_); } sub around { - my $class = shift; - Moose::Util::add_method_modifier($class, 'around', \@_); + Moose::Util::add_method_modifier(shift, 'around', \@_); } our $SUPER_PACKAGE; @@ -97,9 +93,9 @@ sub super { } sub override { - my $class = shift; + my $meta = shift; my ( $name, $method ) = @_; - Class::MOP::Class->initialize($class)->add_override_method_modifier( $name => $method ); + $meta->add_override_method_modifier( $name => $method ); } sub inner { @@ -117,13 +113,13 @@ sub inner { } sub augment { - my $class = shift; + my $meta = shift; my ( $name, $method ) = @_; - Class::MOP::Class->initialize($class)->add_augment_method_modifier( $name => $method ); + $meta->add_augment_method_modifier( $name => $method ); } Moose::Exporter->setup_import_methods( - with_caller => [ + with_meta => [ qw( extends with has before after around override augment) ], as_is => [ diff --git a/lib/Moose/Cookbook/Extending/Recipe1.pod b/lib/Moose/Cookbook/Extending/Recipe1.pod index 15d605e..1cb7df5 100644 --- a/lib/Moose/Cookbook/Extending/Recipe1.pod +++ b/lib/Moose/Cookbook/Extending/Recipe1.pod @@ -258,13 +258,13 @@ as well as those from other modules: use Moose::Exporter; Moose::Exporter->setup_import_methods( - with_caller => ['embiggen'], - also => 'Moose', + with_meta => ['embiggen'], + also => 'Moose', ); sub embiggen { - my $caller = shift; - $caller->meta()->embiggen(@_); + my $meta = shift; + $meta->embiggen(@_); } And then the consumer of your extension can use your C sub: diff --git a/lib/Moose/Cookbook/Extending/Recipe4.pod b/lib/Moose/Cookbook/Extending/Recipe4.pod index 2c7d39f..09e2b26 100644 --- a/lib/Moose/Cookbook/Extending/Recipe4.pod +++ b/lib/Moose/Cookbook/Extending/Recipe4.pod @@ -13,8 +13,8 @@ Moose::Cookbook::Extending::Recipe4 - Acting like Moose.pm and providing sugar M use Moose::Exporter; Moose::Exporter->setup_import_methods( - with_caller => ['has_table'], - also => 'Moose', + with_meta => ['has_table'], + also => 'Moose', ); sub init_meta { @@ -23,8 +23,8 @@ Moose::Cookbook::Extending::Recipe4 - Acting like Moose.pm and providing sugar M } sub has_table { - my $caller = shift; - $caller->meta->table(shift); + my $meta = shift; + $meta->table(shift); } package MyApp::Meta::Class; @@ -45,7 +45,7 @@ Given the above code, you can now replace all instances of C with C. Similarly, C is now replaced with C. -The C parameter specifies a list of functions that should +The C parameter specifies a list of functions that should be wrapped before exporting. The wrapper simply ensures that the importing package name is the first argument to the function, so we can do C>. diff --git a/lib/Moose/Role.pm b/lib/Moose/Role.pm index 8823601..ea4b80e 100644 --- a/lib/Moose/Role.pm +++ b/lib/Moose/Role.pm @@ -23,23 +23,23 @@ sub extends { } sub with { - Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ ); + Moose::Util::apply_all_roles( shift, @_ ); } sub requires { - my $meta = Moose::Meta::Role->initialize(shift); + my $meta = shift; croak "Must specify at least one method" unless @_; $meta->add_required_methods(@_); } sub excludes { - my $meta = Moose::Meta::Role->initialize(shift); + my $meta = shift; croak "Must specify at least one role" unless @_; $meta->add_excluded_roles(@_); } sub has { - my $meta = Moose::Meta::Role->initialize(shift); + my $meta = shift; my $name = shift; croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1; my %options = ( definition_context => Moose::Util::_caller_info(), @_ ); @@ -49,7 +49,7 @@ sub has { sub _add_method_modifier { my $type = shift; - my $meta = Moose::Meta::Role->initialize(shift); + my $meta = shift; my $code = pop @_; for (@_) { @@ -75,7 +75,7 @@ sub super { } sub override { - my $meta = Moose::Meta::Role->initialize(shift); + my $meta = shift; my ( $name, $code ) = @_; $meta->add_override_method_modifier( $name, $code ); } @@ -89,7 +89,7 @@ sub augment { } Moose::Exporter->setup_import_methods( - with_caller => [ + with_meta => [ qw( with requires excludes has before after around override ) ], as_is => [ diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index cf99f2e..243793c 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -187,7 +187,10 @@ sub _build_alias_package_name { sub add_method_modifier { my ( $class_or_obj, $modifier_name, $args ) = @_; - my $meta = find_meta($class_or_obj); + my $meta + = $class_or_obj->can('add_before_method_modifier') + ? $class_or_obj + : find_meta($class_or_obj); my $code = pop @{$args}; my $add_modifier_method = 'add_' . $modifier_name . '_method_modifier'; if ( my $method_modifier_type = ref( @{$args}[0] ) ) {