Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
old mode 100755 (executable)
new mode 100644 (file)
index 59d5561..dd46313
@@ -1,5 +1,5 @@
 package Mouse::Meta::Module;
-use Mouse::Util qw/:meta get_code_package get_code_ref not_supported/; # enables strict and warnings
+use Mouse::Util qw/:meta/; # enables strict and warnings
 
 use Carp         ();
 use Scalar::Util ();
@@ -14,12 +14,6 @@ if(Mouse::Util::MOUSE_XS){
     *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
 }
 
-sub _metaclass_cache { # DEPRECATED
-    my($self, $name) = @_;
-    Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead');
-    return $METAS{$name};
-}
-
 sub initialize {
     my($class, $package_name, @args) = @_;
 
@@ -38,6 +32,9 @@ sub reinitialize {
     ($package_name && !ref($package_name))
         || $class->throw_error("You must pass a package name and it cannot be blessed");
 
+    if(exists $METAS{$package_name}) {
+        unshift @args, %{ $METAS{$package_name} };
+    }
     delete $METAS{$package_name};
     return $class->initialize($package_name, @args);
 }
@@ -49,7 +46,8 @@ sub _class_of{
 }
 
 # Means of accessing all the metaclasses that have
-# been initialized thus far
+# been initialized thus far.
+# The public versions are aliased into Mouse::Util::*.
 #sub _get_all_metaclasses         {        %METAS         }
 sub _get_all_metaclass_instances { values %METAS         }
 sub _get_all_metaclass_names     { keys   %METAS         }
@@ -76,44 +74,40 @@ sub remove_attribute  { delete $_[0]->{attributes}->{$_[1]} }
 
 sub get_attribute_list{ keys   %{$_[0]->{attributes}} }
 
-# XXX: for backward compatibility
+# XXX: not completely compatible with Moose
 my %foreign = map{ $_ => undef } qw(
     Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
     Carp Scalar::Util List::Util
 );
-sub _code_is_mine{
-#    my($self, $code) = @_;
-
-    return !exists $foreign{ get_code_package($_[1]) };
+sub _get_method_body {
+    my($self, $method_name) = @_;
+    my $code = Mouse::Util::get_code_ref($self->{package}, $method_name);
+    return $code && !exists $foreign{ Mouse::Util::get_code_package($code) }
+        ? $code
+        : undef;
 }
 
 sub add_method;
 
 sub has_method {
     my($self, $method_name) = @_;
-
     defined($method_name)
         or $self->throw_error('You must define a method name');
 
-    return defined($self->{methods}{$method_name}) || do{
-        my $code = get_code_ref($self->{package}, $method_name);
-        $code && $self->_code_is_mine($code);
-    };
+    return defined( $self->{methods}{$method_name} )
+        || defined( $self->_get_method_body($method_name) );
 }
 
 sub get_method_body {
     my($self, $method_name) = @_;
-
     defined($method_name)
         or $self->throw_error('You must define a method name');
 
-    return $self->{methods}{$method_name} ||= do{
-        my $code = get_code_ref($self->{package}, $method_name);
-        $code && $self->_code_is_mine($code) ? $code : undef;
-    };
+    return $self->{methods}{$method_name}
+        ||= $self->_get_method_body($method_name);
 }
 
-sub get_method{
+sub get_method {
     my($self, $method_name) = @_;
 
     if(my $code = $self->get_method_body($method_name)){
@@ -134,7 +128,7 @@ sub get_method_list {
     return grep { $self->has_method($_) } keys %{ $self->namespace };
 }
 
-sub _collect_methods { # Mouse specific
+sub _collect_methods { # Mouse specific, used for method modifiers
     my($meta, @args) = @_;
 
     my @methods;
@@ -150,7 +144,8 @@ sub _collect_methods { # Mouse specific
                 my $subname = ( caller(1) )[3];
                 $meta->throw_error(
                     sprintf(
-                        'Methods passed to %s must be provided as a list, ArrayRef or regular expression, not %s',
+                        'Methods passed to %s must be provided as a list,'
+                        . ' ArrayRef or regular expression, not %s',
                         $subname,
                         $type,
                     )
@@ -219,6 +214,7 @@ sub create {
         $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
     }
 
+
     # instantiate a module
     {
         no strict 'refs';
@@ -228,7 +224,7 @@ sub create {
 
     my $meta = $self->initialize( $package_name, %options);
 
-    Scalar::Util::weaken $METAS{$package_name}
+    Scalar::Util::weaken($METAS{$package_name})
         if $mortal;
 
     $meta->add_method(meta => sub {
@@ -262,7 +258,7 @@ sub create {
             $meta->add_method($method_name, $method_body);
         }
     }
-    if (defined $roles){
+    if (defined $roles and !$options{in_application_to_instance}){
         Mouse::Util::apply_all_roles($package_name, @{$roles});
     }
 
@@ -279,18 +275,18 @@ sub DESTROY{
     return if $Mouse::Util::in_global_destruction;
 
     my $serial_id = $self->{anon_serial_id};
-
     return if !$serial_id;
-    # mortal anonymous class
 
-    # XXX: cleaning stash with threads causes panic/SEGV.
+    # XXX: cleaning stash with threads causes panic/SEGV on legacy perls.
     if(exists $INC{'threads.pm'}) {
         # (caller)[2] indicates the caller's line number,
-        # which is zero when the current thread is joining.
+        # which is zero when the current thread is joining (destroying).
         return if( (caller)[2] == 0);
     }
 
-    # @ISA is a magical variable, so we clear it manually.
+    # clean up mortal anonymous class stuff
+
+    # @ISA is a magical variable, so we must clear it manually.
     @{$self->{superclasses}} = () if exists $self->{superclasses};
 
     # Then, clear the symbol table hash
@@ -302,34 +298,24 @@ sub DESTROY{
     $name =~ s/ $serial_id \z//xms;
     no strict 'refs';
     delete ${$name}{ $serial_id . '::' };
-
     return;
 }
 
-sub throw_error{
-    my($self, $message, %args) = @_;
-
-    local $Carp::CarpLevel  = $Carp::CarpLevel + 1 + ($args{depth} || 0);
-    local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
-
-    if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
-        Carp::croak($message);
-    }
-    else{
-        Carp::confess($message);
-    }
-}
 
 1;
 __END__
 
 =head1 NAME
 
-Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
+Mouse::Meta::Module - The common base class of Mouse::Meta::Class and Mouse::Meta::Role
 
 =head1 VERSION
 
-This document describes Mouse version 0.63
+This document describes Mouse version 0.95
+
+=head1 DESCRIPTION
+
+This class is an abstract base class of meta classes and meta roles.
 
 =head1 SEE ALSO