Checking in changes prior to tagging of version 0.74.
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
index 663f6ae..501c9ec 100644 (file)
@@ -1,5 +1,5 @@
 package Mouse::Meta::Class;
-use Mouse::Util qw/:meta get_linear_isa not_supported/; # enables strict and warnings
+use Mouse::Util qw/:meta/; # enables strict and warnings
 
 use Scalar::Util qw/blessed weaken/;
 
@@ -56,16 +56,25 @@ sub superclasses {
     if (@_) {
         foreach my $super(@_){
             Mouse::Util::load_class($super);
-            my $meta = Mouse::Util::get_metaclass_by_name($super);
 
-            next if not defined $meta;
+            my $meta = Mouse::Util::get_metaclass_by_name($super);
+            unless(defined $meta) {
+                # checks if $super is a foreign class (i.e. non-Mouse class)
+                my $mm = $super->can('meta');
+                if(!($mm && $mm == \&Mouse::Util::meta)) {
+                    if($super->can('new') or $super->can('DESTROY')) {
+                        $self->inherit_from_foreign_class($super);
+                    }
+                }
+                next;
+            }
 
             if(Mouse::Util::is_a_metarole($meta)){
                 $self->throw_error("You cannot inherit from a Mouse Role ($super)");
             }
 
+            # checks and fixes in metaclass compatiility
             next if $self->isa(ref $meta); # _superclass_meta_is_compatible
-
             $self->_reconcile_with_superclass_meta($meta);
         }
         @{ $self->{superclasses} } = @_;
@@ -73,6 +82,15 @@ sub superclasses {
 
     return @{ $self->{superclasses} };
 }
+
+sub inherit_from_foreign_class {
+    my($class, $super) = @_;
+    Carp::carp("You inherit from non-Mouse class ($super),"
+        . " but it is unlikely to work correctly."
+        . " Please concider to use MouseX::Foreign");
+    return;
+}
+
 my @MetaClassTypes = (
     'attribute',   # Mouse::Meta::Attribute
     'method',      # Mouse::Meta::Method
@@ -209,48 +227,30 @@ sub add_attribute {
 
     weaken( $attr->{associated_class} = $self );
 
-    $self->{attributes}{$attr->name} = $attr;
+    # install accessors first
     $attr->install_accessors();
 
+    # then register the attribute to the metaclass
+    $attr->{insertion_order} = keys %{ $self->{attributes} };
+    $self->{attributes}{$attr->name} = $attr;
+
     if(!$attr->{associated_methods} && ($attr->{is} || '') ne 'bare'){
         Carp::carp(qq{Attribute ($name) of class }.$self->name
             .qq{ has no associated methods (did you mean to provide an "is" argument?)});
     }
-    return $attr;
-}
 
-sub compute_all_applicable_attributes { # DEPRECATED
-    Carp::cluck('compute_all_applicable_attributes() has been deprecated. Use get_all_attributes() instead');
-
-    return shift->get_all_attributes(@_)
+    if(!Mouse::Util::MOUSE_XS) {
+        # in Mouse::PurePerl, attribute initialization code is cached, so it
+        # must be clear here. See _initialize_object() in Mouse::PurePerl.
+        delete $self->{_initialize_object};
+    }
+    return $attr;
 }
 
 sub linearized_isa;
 
 sub new_object;
-
-sub clone_object {
-    my $class  = shift;
-    my $object = shift;
-    my $args   = $object->Mouse::Object::BUILDARGS(@_);
-
-    (blessed($object) && $object->isa($class->name))
-        || $class->throw_error("You must pass an instance of the metaclass (" . $class->name . "), not ($object)");
-
-    my $cloned = bless { %$object }, ref $object;
-    $class->_initialize_object($cloned, $args, 1);
-
-    return $cloned;
-}
-
-sub clone_instance { # DEPRECATED
-    my ($class, $instance, %params) = @_;
-
-    Carp::cluck('clone_instance() has been deprecated. Use clone_object() instead');
-
-    return $class->clone_object($instance, %params);
-}
-
+sub clone_object;
 
 sub immutable_options {
     my ( $self, @args ) = @_;
@@ -263,15 +263,12 @@ sub immutable_options {
     );
 }
 
-
 sub make_immutable {
     my $self = shift;
     my %args = $self->immutable_options(@_);
 
     $self->{is_immutable}++;
 
-    $self->{strict_constructor} = $args{strict_constructor};
-
     if ($args{inline_constructor}) {
         $self->add_method($args{constructor_name} =>
             Mouse::Util::load_class($self->constructor_class)
@@ -284,8 +281,8 @@ sub make_immutable {
                 ->_generate_destructor($self, \%args));
     }
 
-    # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value
-    # at the end of a source file. 
+    # Moose's make_immutable returns true allowing calling code to skip
+    # setting an explicit true value at the end of a source file.
     return 1;
 }
 
@@ -298,22 +295,25 @@ sub make_mutable {
 sub is_immutable;
 sub is_mutable   { !$_[0]->is_immutable }
 
-sub _install_modifier_pp{
+sub _install_modifier {
     my( $self, $type, $name, $code ) = @_;
     my $into = $self->name;
 
     my $original = $into->can($name)
-        or $self->throw_error("The method '$name' is not found in the inheritance hierarchy for class $into");
+        or $self->throw_error("The method '$name' was not found in the inheritance hierarchy for $into");
 
     my $modifier_table = $self->{modifiers}{$name};
 
     if(!$modifier_table){
-        my(@before, @after, @around, $cache, $modified);
-
-        $cache = $original;
-
-        $modified = sub {
-            for my $c (@before) { $c->(@_) }
+        my(@before, @after, @around);
+        my $cache = $original;
+        my $modified = sub {
+            if(@before) {
+                for my $c (@before) { $c->(@_) }
+            }
+            unless(@after) {
+                return $cache->(@_);
+            }
 
             if(wantarray){ # list context
                 my @rval = $cache->(@_);
@@ -364,55 +364,6 @@ sub _install_modifier_pp{
     return;
 }
 
-sub _install_modifier {
-    my ( $self, $type, $name, $code ) = @_;
-
-    # load Data::Util first
-    my $no_cmm_fast = do{
-        local $@;
-        eval q{ use Data::Util 0.55 () };
-        $@;
-    };
-
-    my $impl;
-    if($no_cmm_fast){
-        $impl = \&_install_modifier_pp;
-    }
-    else{
-        $impl = sub {
-            my ( $self, $type, $name, $code ) = @_;
-            my $into = $self->name;
-
-            my $method = Mouse::Util::get_code_ref( $into, $name );
-
-            if ( !$method || !Data::Util::subroutine_modifier($method) ) {
-                unless ($method) {
-                    $method = $into->can($name)
-                        or Carp::confess("The method '$name' is not found in the inheritance hierarchy for class $into");
-                }
-                $method = Data::Util::modify_subroutine( $method,
-                    $type => [$code] );
-
-                $self->add_method($name => $method);
-            }
-            else {
-                Data::Util::subroutine_modifier( $method, $type => $code );
-                $self->add_method($name => Mouse::Util::get_code_ref($into, $name));
-            }
-
-            return;
-        };
-    }
-
-    # replace this method itself :)
-    {
-        no warnings 'redefine';
-        *_install_modifier = $impl;
-    }
-
-    $self->$impl( $type, $name, $code );
-}
-
 sub add_before_method_modifier {
     my ( $self, $name, $code ) = @_;
     $self->_install_modifier( 'before', $name, $code );
@@ -500,7 +451,12 @@ Mouse::Meta::Class - The Mouse class metaclass
 
 =head1 VERSION
 
-This document describes Mouse version 0.59
+This document describes Mouse version 0.74
+
+=head1 DESCRIPTION
+
+This class is a meta object protocol for Mouse classes,
+which is a subset of Moose::Meta:::Class.
 
 =head1 METHODS