default => 'Moose::Error::Default',
));
+__PACKAGE__->meta->add_attribute('role_metaclass' => (
+ accessor => 'role_metaclass',
+ default => 'Moose::Meta::Role',
+));
+
sub initialize {
my $class = shift;
my $pkg = shift;
- return Class::MOP::get_metaclass_by_name($pkg)
- || $class->SUPER::initialize($pkg,
+ if (my $meta = Class::MOP::get_metaclass_by_name($pkg)) {
+ return $meta;
+ }
+ return $class->SUPER::initialize($pkg,
'attribute_metaclass' => 'Moose::Meta::Attribute',
'method_metaclass' => 'Moose::Meta::Method',
'instance_metaclass' => 'Moose::Meta::Instance',
@_
- );
+ );
}
sub create {
## some things don't always fit, so they go here ...
+sub initialize {
+ my $class = shift;
+ my $pkg = shift;
+ if (my $meta = Class::MOP::get_metaclass_by_name($pkg)) {
+ return $meta;
+ }
+ return $class->SUPER::initialize($pkg, @_);
+}
+
sub add_attribute {
my $self = shift;
my $name = shift;
my $meta;
if ($role->can('meta')) {
$meta = $role->meta();
+ # we might have had metaclass called on us
+
+ if (blessed($meta) && $meta->isa('Moose::Meta::Class')) {
+ $metaclass = $meta->{'role_metaclass'} || $metaclass;
+ Class::MOP::remove_metaclass_by_name($role);
+ $meta = $metaclass->initialize($role);
+ $meta->add_method(
+ 'meta' => sub {
+ # re-initialize so it inherits properly
+ $metaclass->initialize( ref($_[0]) || $_[0] );
+ }
+ );
+ }
unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) {
require Moose;
}
else {
$meta = $metaclass->initialize($role);
-
$meta->add_method(
'meta' => sub {
# re-initialize so it inherits properly
--- /dev/null
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 1;
+use Moose ();
+
+BEGIN {
+
+ package My::Meta::Role;
+ use Moose;
+ extends 'Moose::Meta::Role';
+
+ has test_serial => (
+ is => 'ro',
+ isa => 'Int',
+ default => 1,
+ );
+ no Moose;
+
+}
+{
+
+ package MyRole;
+ use metaclass 'Moose::Meta::Class' =>
+ ( role_metaclass => 'My::Meta::Role' );
+ use Moose::Role;
+
+ no Moose::Role;
+};
+
+isa_ok( MyRole->meta, 'My::Meta::Role' );
+
+# my $role = MyRole->meta->create_anon_role;
+# is( $role->test_serial, 1, "default value for the serial attribute" );