Checking in changes prior to tagging of version 0.40_08. Changelog diff is:
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
index 3e3a724..97d2d64 100644 (file)
@@ -4,17 +4,17 @@ use Mouse::Util qw(:meta not_supported english_list); # enables strict and warni
 use Mouse::Meta::Module;
 our @ISA = qw(Mouse::Meta::Module);
 
-sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
+sub method_metaclass;
 
 sub _construct_meta {
     my $class = shift;
 
     my %args  = @_;
 
-    $args{methods}          ||= {};
-    $args{attributes}       ||= {};
-    $args{required_methods} ||= [];
-    $args{roles}            ||= [];
+    $args{methods}          = {};
+    $args{attributes}       = {};
+    $args{required_methods} = [];
+    $args{roles}            = [];
 
     my $self = bless \%args, ref($class) || $class;
     if($class ne __PACKAGE__){
@@ -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 { $_[0]->{roles} }
+sub get_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} };
@@ -56,6 +61,7 @@ sub add_attribute {
     my $name = shift;
 
     $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
+    return;
 }
 
 sub _check_required_methods{
@@ -178,22 +184,22 @@ sub apply {
 
     my %args = (@_ == 1) ? %{ $_[0] } : @_;
 
-    if($applicant->isa('Mouse::Meta::Class')){  # Application::ToClass
+    my $instance;
+
+    if(Mouse::Util::is_a_metaclass($applicant)){  # Application::ToClass
         $args{_to} = 'class';
     }
-    elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole
+    elsif(Mouse::Util::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}){
@@ -223,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;
 }
 
@@ -241,29 +255,36 @@ sub combine {
     return $composite;
 }
 
-for my $modifier_type (qw/before after around/) {
-
-    my $modifier = "${modifier_type}_method_modifiers";
-    my $add_method_modifier =  sub {
-        my ($self, $method_name, $method) = @_;
-
-        push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
-        return;
-    };
-    my $has_method_modifiers = sub{
-        my($self, $method_name) = @_;
-        my $m = $self->{$modifier}->{$method_name};
-        return $m && @{$m} != 0;
-    };
-    my $get_method_modifiers = sub {
-        my ($self, $method_name) = @_;
-        return @{ $self->{$modifier}->{$method_name} ||= [] }
-    };
-
-    no strict 'refs';
-    *{ 'add_' . $modifier_type . '_method_modifier'  } = $add_method_modifier;
-    *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
-    *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
+sub add_before_method_modifier {
+    my ($self, $method_name, $method) = @_;
+
+    push @{ $self->{before_method_modifiers}{$method_name} ||= [] }, $method;
+    return;
+}
+sub add_around_method_modifier {
+    my ($self, $method_name, $method) = @_;
+
+    push @{ $self->{around_method_modifiers}{$method_name} ||= [] }, $method;
+    return;
+}
+sub add_after_method_modifier {
+    my ($self, $method_name, $method) = @_;
+
+    push @{ $self->{after_method_modifiers}{$method_name} ||= [] }, $method;
+    return;
+}
+
+sub get_before_method_modifiers {
+    my ($self, $method_name) = @_;
+    return @{ $self->{before_method_modifiers}{$method_name} ||= [] }
+}
+sub get_around_method_modifiers {
+    my ($self, $method_name) = @_;
+    return @{ $self->{around_method_modifiers}{$method_name} ||= [] }
+}
+sub get_after_method_modifiers {
+    my ($self, $method_name) = @_;
+    return @{ $self->{after_method_modifiers}{$method_name} ||= [] }
 }
 
 sub add_override_method_modifier{
@@ -280,23 +301,11 @@ sub add_override_method_modifier{
     $self->{override_method_modifiers}->{$method_name} = $method;
 }
 
-sub has_override_method_modifier {
-    my ($self, $method_name) = @_;
-    return exists $self->{override_method_modifiers}->{$method_name};
-}
-
 sub get_override_method_modifier {
     my ($self, $method_name) = @_;
     return $self->{override_method_modifiers}->{$method_name};
 }
 
-sub get_method_modifier_list {
-    my($self, $modifier_type) = @_;
-
-    return keys %{ $self->{$modifier_type . '_method_modifiers'} };
-}
-
-# This is currently not passing all the Moose tests.
 sub does_role {
     my ($self, $role_name) = @_;
 
@@ -312,15 +321,17 @@ sub does_role {
     return 0;
 }
 
-
 1;
-
 __END__
 
 =head1 NAME
 
 Mouse::Meta::Role - The Mouse Role metaclass
 
+=head1 VERSION
+
+This document describes Mouse version 0.40_08
+
 =head1 SEE ALSO
 
 L<Moose::Meta::Role>