X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FUtil.pm;h=243793cfec42cf6262f6e766de16b52ce77bd67d;hb=90b20bd60aaeefc2f19154e534abf4bfc1177d8c;hp=36fdf58bae8afe993ea1b2660a3237d411940462;hpb=9e4ed568f2e5c1041e6fea8d0cbde420562ab5df;p=gitmo%2FMoose.git diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 36fdf58..243793c 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -8,7 +8,7 @@ use Sub::Exporter; use Scalar::Util 'blessed'; use Class::MOP 0.60; -our $VERSION = '0.84'; +our $VERSION = '0.89_01'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -24,6 +24,8 @@ my @exports = qw[ resolve_metaclass_alias add_method_modifier english_list + meta_attribute_alias + meta_class_alias ]; Sub::Exporter::setup_exporter({ @@ -79,7 +81,7 @@ sub ensure_all_roles { sub apply_all_roles { my $applicant = shift; - _apply_all_roles($applicant, sub { 1 }, @_); + _apply_all_roles($applicant, undef, @_); } sub _apply_all_roles { @@ -105,7 +107,9 @@ sub _apply_all_roles { } } - @$roles = grep { local $_ = $_->[0]; $role_filter->() } @$roles; + if ( defined $role_filter ) { + @$roles = grep { local $_ = $_->[0]; $role_filter->() } @$roles; + } return unless @$roles; @@ -146,6 +150,15 @@ sub resolve_metatrait_alias { return resolve_metaclass_alias( @_, trait => 1 ); } +sub _build_alias_package_name { + my ($type, $name, $trait) = @_; + return 'Moose::Meta::' + . $type + . '::Custom::' + . ( $trait ? 'Trait::' : '' ) + . $name; +} + { my %cache; @@ -156,12 +169,9 @@ sub resolve_metatrait_alias { return $cache{$cache_key}{$metaclass_name} if $cache{$cache_key}{$metaclass_name}; - my $possible_full_name - = 'Moose::Meta::' - . $type - . '::Custom::' - . ( $options{trait} ? "Trait::" : "" ) - . $metaclass_name; + my $possible_full_name = _build_alias_package_name( + $type, $metaclass_name, $options{trait} + ); my $loaded_class = Class::MOP::load_first_existing_class( $possible_full_name, @@ -177,7 +187,10 @@ sub resolve_metatrait_alias { 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] ) ) { @@ -214,6 +227,30 @@ sub _caller_info { return \%info; } +sub _create_alias { + my ($type, $name, $trait, $for) = @_; + my $package = _build_alias_package_name($type, $name, $trait); + Class::MOP::Class->initialize($package)->add_method( + register_implementation => sub { $for } + ); +} + +sub meta_attribute_alias { + my ($to, $from) = @_; + $from ||= caller; + my $meta = Class::MOP::class_of($from); + my $trait = $meta->isa('Moose::Meta::Role'); + _create_alias('Attribute', $to, $trait, $from); +} + +sub meta_class_alias { + my ($to, $from) = @_; + $from ||= caller; + my $meta = Class::MOP::class_of($from); + my $trait = $meta->isa('Moose::Meta::Role'); + _create_alias('Class', $to, $trait, $from); +} + 1; __END__ @@ -274,8 +311,8 @@ applicant can be a role name, class name, or object. The C<$applicant> must already have a metaclass object. The list of C<@roles> should be a list of names, each of which can be -followed by an optional hash reference of options (C and -C). +followed by an optional hash reference of options (C<-excludes> and +C<-alias>). =item B @@ -307,8 +344,8 @@ when specifying the C or C option for an attribute: metaclass => "Bar", ); -The name resolution mechanism is covered in L. +The name resolution mechanism is covered in +L. =item B @@ -316,6 +353,14 @@ Given a list of scalars, turns them into a proper list in English ("one and two", "one, two, three, and four"). This is used to help us make nicer error messages. +=item B + +=item B + +Create an alias from the class C<$from> (or the current package, if +C<$from> is unspecified), so that +L works properly. + =back =head1 TODO