Moved attribute management to CMOP::HasAttributes.
[gitmo/Class-MOP.git] / lib / Class / MOP.pm
index e7ee385..2bd684e 100644 (file)
@@ -12,6 +12,8 @@ use Carp          'confess';
 use Scalar::Util  'weaken', 'reftype', 'blessed';
 use Try::Tiny;
 
+use Class::MOP::HasAttributes;
+use Class::MOP::HasMethods;
 use Class::MOP::Class;
 use Class::MOP::Attribute;
 use Class::MOP::Method;
@@ -25,7 +27,7 @@ BEGIN {
     *check_package_cache_flag = \&mro::get_pkg_gen;
 }
 
-our $VERSION   = '0.94';
+our $VERSION   = '0.95';
 our $XS_VERSION = $VERSION;
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
@@ -76,7 +78,7 @@ sub _class_to_pmfile {
 
 sub load_first_existing_class {
     my @classes = @_
-        or return;
+      or return;
 
     foreach my $class (@classes) {
         unless ( _is_valid_class_name($class) ) {
@@ -87,54 +89,31 @@ sub load_first_existing_class {
 
     my $found;
     my %exceptions;
-    for my $class (@classes) {
-        my ($fail, $e) = _try_load_one_class($class);
 
-        if ($fail) {
-            my $pmfile = _class_to_pmfile($class);
-            $exceptions{$class} = $e;
-            last if $e !~ /^Can't locate \Q$pmfile\E in \@INC/;
-        }
-        else {
-            $found = $class;
-            last;
-        }
-    }
+    for my $class (@classes) {
+        my $file = _class_to_pmfile($class);
 
-    return $found if $found;
+        return $class if is_class_loaded($class);;
 
-    confess join(
-        "\n",
-        map {
-            sprintf(
-                "Could not load class (%s) because : %s", $_,
-                $exceptions{$_}
-                )
+        return $class if try {
+            local $SIG{__DIE__};
+            require $file;
+            return 1;
+        }
+        catch {
+            unless (/^Can't locate \Q$file\E in \@INC/) {
+                confess "Couldn't load class ($class) because: $_";
             }
-        grep {
-            exists $exceptions{$_}
-            } @classes
-    );
-}
 
-sub _try_load_one_class {
-    my $class = shift;
-
-    return if is_class_loaded($class);
-
-    my $file = _class_to_pmfile($class);
-
-    my ($failed, $error);
-    try {
-        local $SIG{__DIE__};
-        require($file);
+            return;
+        };
     }
-    catch {
-        $failed = 1;
-        $error = $_;
-    };
 
-    return $failed, $error;
+    if ( @classes > 1 ) {
+        confess "Can't locate any of @classes in \@INC (\@INC contains: @INC).";
+    } else {
+        confess "Can't locate " . _class_to_pmfile($classes[0]) . " in \@INC (\@INC contains: @INC).";
+    }
 }
 
 sub load_class {
@@ -183,68 +162,101 @@ sub _is_valid_class_name {
 # inherit them using _construct_instance
 
 ## --------------------------------------------------------
-## Class::MOP::Package
+## Class::MOP::HasMethods
 
-Class::MOP::Package->meta->add_attribute(
-    Class::MOP::Attribute->new('package' => (
+Class::MOP::HasMethods->meta->add_attribute(
+    Class::MOP::Attribute->new('_methods' => (
         reader   => {
-            # NOTE: we need to do this in order
-            # for the instance meta-object to
-            # not fall into meta-circular death
-            #
+            # NOTE:
             # we just alias the original method
             # rather than re-produce it here
-            'name' => \&Class::MOP::Package::name
+            '_full_method_map' => \&Class::MOP::HasMethods::_full_method_map
         },
+        default => sub { {} }
     ))
 );
 
-Class::MOP::Package->meta->add_attribute(
-    Class::MOP::Attribute->new('namespace' => (
-        reader => {
+Class::MOP::HasMethods->meta->add_attribute(
+    Class::MOP::Attribute->new('method_metaclass' => (
+        reader   => {
             # NOTE:
             # we just alias the original method
             # rather than re-produce it here
-            'namespace' => \&Class::MOP::Package::namespace
+            'method_metaclass' => \&Class::MOP::HasMethods::method_metaclass
         },
-        init_arg => undef,
-        default  => sub { \undef }
+        default  => 'Class::MOP::Method',
     ))
 );
 
-Class::MOP::Package->meta->add_attribute(
-    Class::MOP::Attribute->new('_methods' => (
+Class::MOP::HasMethods->meta->add_attribute(
+    Class::MOP::Attribute->new('wrapped_method_metaclass' => (
         reader   => {
             # NOTE:
             # we just alias the original method
             # rather than re-produce it here
-            '_full_method_map' => \&Class::MOP::Package::_full_method_map
+            'wrapped_method_metaclass' => \&Class::MOP::HasMethods::wrapped_method_metaclass
         },
-        default => sub { {} }
+        default  => 'Class::MOP::Method::Wrapped',
     ))
 );
 
-Class::MOP::Package->meta->add_attribute(
-    Class::MOP::Attribute->new('method_metaclass' => (
+## --------------------------------------------------------
+## Class::MOP::HasMethods
+
+Class::MOP::HasAttributes->meta->add_attribute(
+    Class::MOP::Attribute->new('attributes' => (
+        reader   => {
+            # NOTE: we need to do this in order
+            # for the instance meta-object to
+            # not fall into meta-circular death
+            #
+            # we just alias the original method
+            # rather than re-produce it here
+            '_attribute_map' => \&Class::MOP::HasAttributes::_attribute_map
+        },
+        default  => sub { {} }
+    ))
+);
+
+Class::MOP::HasAttributes->meta->add_attribute(
+    Class::MOP::Attribute->new('attribute_metaclass' => (
         reader   => {
             # NOTE:
             # we just alias the original method
             # rather than re-produce it here
-            'method_metaclass' => \&Class::MOP::Package::method_metaclass
+            'attribute_metaclass' => \&Class::MOP::HasAttributes::attribute_metaclass
         },
-        default  => 'Class::MOP::Method',
+        default  => 'Class::MOP::Attribute',
     ))
 );
 
+## --------------------------------------------------------
+## Class::MOP::Package
+
 Class::MOP::Package->meta->add_attribute(
-    Class::MOP::Attribute->new('wrapped_method_metaclass' => (
+    Class::MOP::Attribute->new('package' => (
         reader   => {
+            # NOTE: we need to do this in order
+            # for the instance meta-object to
+            # not fall into meta-circular death
+            #
+            # we just alias the original method
+            # rather than re-produce it here
+            'name' => \&Class::MOP::Package::name
+        },
+    ))
+);
+
+Class::MOP::Package->meta->add_attribute(
+    Class::MOP::Attribute->new('namespace' => (
+        reader => {
             # NOTE:
             # we just alias the original method
             # rather than re-produce it here
-            'wrapped_method_metaclass' => \&Class::MOP::Package::wrapped_method_metaclass
+            'namespace' => \&Class::MOP::Package::namespace
         },
-        default  => 'Class::MOP::Method::Wrapped',
+        init_arg => undef,
+        default  => sub { \undef }
     ))
 );
 
@@ -297,21 +309,6 @@ Class::MOP::Module->meta->add_attribute(
 ## Class::MOP::Class
 
 Class::MOP::Class->meta->add_attribute(
-    Class::MOP::Attribute->new('attributes' => (
-        reader   => {
-            # NOTE: we need to do this in order
-            # for the instance meta-object to
-            # not fall into meta-circular death
-            #
-            # we just alias the original method
-            # rather than re-produce it here
-            'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
-        },
-        default  => sub { {} }
-    ))
-);
-
-Class::MOP::Class->meta->add_attribute(
     Class::MOP::Attribute->new('superclasses' => (
         accessor => {
             # NOTE:
@@ -325,18 +322,6 @@ Class::MOP::Class->meta->add_attribute(
 );
 
 Class::MOP::Class->meta->add_attribute(
-    Class::MOP::Attribute->new('attribute_metaclass' => (
-        reader   => {
-            # NOTE:
-            # we just alias the original method
-            # rather than re-produce it here
-            'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
-        },
-        default  => 'Class::MOP::Attribute',
-    ))
-);
-
-Class::MOP::Class->meta->add_attribute(
     Class::MOP::Attribute->new('instance_metaclass' => (
         reader   => {
             # NOTE: we need to do this in order
@@ -707,6 +692,15 @@ $_->meta->make_immutable(
     Class::MOP::Method::Wrapped
 /;
 
+$_->meta->make_immutable(
+    inline_constructor  => 0,
+    constructor_name    => undef,
+    inline_accessors => 0,
+) for qw/
+    Class::MOP::HasAttributes
+    Class::MOP::HasMethods
+/;
+
 1;
 
 __END__
@@ -925,7 +919,7 @@ unconditionally.
 
 If the module cannot be loaded, an exception is thrown.
 
-For historical reasons, this function returns explicitly returns a true value.
+For historical reasons, this function explicitly returns a true value.
 
 =item B<Class::MOP::is_class_loaded($class_name)>