resolve_metaclass_alias
add_method_modifier
english_list
+ meta_attribute_alias
+ meta_class_alias
];
Sub::Exporter::setup_exporter({
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;
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,
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__
("one and two", "one, two, three, and four"). This is used to help us
make nicer error messages.
+=item B<meta_class_alias($to[, $from])>
+
+=item B<meta_attribute_alias($to[, $from])>
+
+Create an alias from the class C<$from> (or the current package, if
+C<$from> is unspecified), so that
+L<Moose/Metaclass and Trait Name Resolution> works properly.
+
=back
=head1 TODO
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 8;
+use Test::Moose qw(does_ok);
+
+BEGIN {
+ package Foo::Meta::Role;
+ use Moose::Role;
+ Moose::Util::meta_class_alias
+ FooRole => 'Foo::Meta::Role';
+
+ package Foo::Meta::Class;
+ use Moose;
+ extends 'Moose::Meta::Class';
+ with 'Foo::Meta::Role';
+ Moose::Util::meta_class_alias
+ FooClass => 'Foo::Meta::Class';
+
+ package Foo::Meta::Role::Attribute;
+ use Moose::Role;
+ Moose::Util::meta_attribute_alias
+ FooAttrRole => 'Foo::Meta::Role::Attribute';
+
+ package Foo::Meta::Attribute;
+ use Moose;
+ extends 'Moose::Meta::Attribute';
+ with 'Foo::Meta::Role::Attribute';
+ Moose::Util::meta_attribute_alias
+ FooAttrClass => 'Foo::Meta::Attribute';
+
+ package Bar::Meta::Role;
+ use Moose::Role;
+ Moose::Util::meta_class_alias 'BarRole';
+
+ package Bar::Meta::Class;
+ use Moose;
+ extends 'Moose::Meta::Class';
+ with 'Bar::Meta::Role';
+ Moose::Util::meta_class_alias 'BarClass';
+
+ package Bar::Meta::Role::Attribute;
+ use Moose::Role;
+ Moose::Util::meta_attribute_alias 'BarAttrRole';
+
+ package Bar::Meta::Attribute;
+ use Moose;
+ extends 'Moose::Meta::Attribute';
+ with 'Bar::Meta::Role::Attribute';
+ Moose::Util::meta_attribute_alias 'BarAttrClass';
+}
+
+package FooWithMetaClass;
+use Moose -metaclass => 'FooClass';
+
+has bar => (
+ metaclass => 'FooAttrClass',
+ is => 'ro',
+);
+
+
+package FooWithMetaTrait;
+use Moose -traits => 'FooRole';
+
+has bar => (
+ traits => [qw(FooAttrRole)],
+ is => 'ro',
+);
+
+package BarWithMetaClass;
+use Moose -metaclass => 'BarClass';
+
+has bar => (
+ metaclass => 'BarAttrClass',
+ is => 'ro',
+);
+
+
+package BarWithMetaTrait;
+use Moose -traits => 'BarRole';
+
+has bar => (
+ traits => [qw(BarAttrRole)],
+ is => 'ro',
+);
+
+package main;
+my $fwmc_meta = FooWithMetaClass->meta;
+my $fwmt_meta = FooWithMetaTrait->meta;
+isa_ok($fwmc_meta, 'Foo::Meta::Class');
+isa_ok($fwmc_meta->get_attribute('bar'), 'Foo::Meta::Attribute');
+does_ok($fwmt_meta, 'Foo::Meta::Role');
+does_ok($fwmt_meta->get_attribute('bar'), 'Foo::Meta::Role::Attribute');
+
+my $bwmc_meta = BarWithMetaClass->meta;
+my $bwmt_meta = BarWithMetaTrait->meta;
+isa_ok($bwmc_meta, 'Bar::Meta::Class');
+isa_ok($bwmc_meta->get_attribute('bar'), 'Bar::Meta::Attribute');
+does_ok($bwmt_meta, 'Bar::Meta::Role');
+does_ok($bwmt_meta->get_attribute('bar'), 'Bar::Meta::Role::Attribute');