More _generate_class_type_for()
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
index 04e6cc1..d10023c 100644 (file)
@@ -29,11 +29,16 @@ sub create_anon_role{
     return $self->create(undef, @_);
 }
 
-sub is_anon_role{
-    return exists $_[0]->{anon_serial_id};
-}
+sub is_anon_role;
+
+sub get_roles;
 
-sub get_roles { $_[0]->{roles} }
+sub calculate_all_roles {
+    my $self = shift;
+    my %seen;
+    return grep { !$seen{ $_->name }++ }
+           ($self, map  { $_->calculate_all_roles } @{ $self->get_roles });
+}
 
 sub get_required_method_list{
     return @{ $_[0]->{required_methods} };
@@ -179,22 +184,22 @@ sub apply {
 
     my %args = (@_ == 1) ? %{ $_[0] } : @_;
 
-    if($applicant->isa('Mouse::Meta::Class')){  # Application::ToClass
+    my $instance;
+
+    if(Mouse::Util::TypeConstraints::_is_a_metaclass($applicant)){  # Application::ToClass
         $args{_to} = 'class';
     }
-    elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole
+    elsif(Mouse::Util::TypeConstraints::_is_a_metarole($applicant)){ # Application::ToRole
         $args{_to} = 'role';
     }
     else{                                       # Appplication::ToInstance
         $args{_to} = 'instance';
+        $instance = $applicant;
 
-        my $metaclass = $applicant->meta->create_anon_class(
-            superclasses => [ref $applicant],
+        $applicant = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')->create_anon_class(
+            superclasses => [ref $instance],
             cache        => 1,
         );
-        bless $applicant, $metaclass->name; # rebless
-
-        $applicant = $metaclass;
     }
 
     if($args{alias} && !exists $args{-alias}){
@@ -224,6 +229,14 @@ sub apply {
     $self->_apply_methods($applicant, \%args);
     $self->_apply_modifiers($applicant, \%args);
     $self->_append_roles($applicant, \%args);
+
+
+    if(defined $instance){ # Application::ToInstance
+        # rebless instance
+        bless $instance, $applicant->name;
+        $applicant->_initialize_object($instance, $instance);
+    }
+
     return;
 }
 
@@ -306,6 +319,10 @@ __END__
 
 Mouse::Meta::Role - The Mouse Role metaclass
 
+=head1 VERSION
+
+This document describes Mouse version 0.40_01
+
 =head1 SEE ALSO
 
 L<Moose::Meta::Role>