bump version and update Changes
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
index 0e0195c..ec53419 100644 (file)
@@ -12,8 +12,10 @@ use Class::MOP::Class::Immutable::Class::MOP::Class;
 
 use Carp         'confess';
 use Scalar::Util 'blessed', 'weaken';
+use Sub::Name 'subname';
+use Devel::GlobalDestruction 'in_global_destruction';
 
-our $VERSION   = '0.81';
+our $VERSION   = '0.85';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -129,15 +131,26 @@ sub _new {
         # defined in Class::MOP::Class
         'superclasses' => \undef,
 
-        'methods'             => {},
-        'attributes'          => {},
-        'attribute_metaclass' => ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
-        'method_metaclass' => ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
-        'wrapped_method_metaclass' => ( $options->{'wrapped_method_metaclass'} || 'Class::MOP::Method::Wrapped' ),
-        'instance_metaclass' => ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
-        'immutable_trait' => ( $options->{'immutable_trait'} || 'Class::MOP::Class::Immutable::Trait' ),
+        'methods'    => {},
+        'attributes' => {},
+        'attribute_metaclass' =>
+            ( $options->{'attribute_metaclass'} || 'Class::MOP::Attribute' ),
+        'method_metaclass' =>
+            ( $options->{'method_metaclass'} || 'Class::MOP::Method' ),
+        'wrapped_method_metaclass' => (
+            $options->{'wrapped_method_metaclass'}
+                || 'Class::MOP::Method::Wrapped'
+        ),
+        'instance_metaclass' =>
+            ( $options->{'instance_metaclass'} || 'Class::MOP::Instance' ),
+        'immutable_trait' => (
+            $options->{'immutable_trait'}
+                || 'Class::MOP::Class::Immutable::Trait'
+        ),
         'constructor_name' => ( $options->{constructor_name} || 'new' ),
-        'constructor_class' => ( $options->{constructor_class} || 'Class::MOP::Method::Constructor' ),
+        'constructor_class' => (
+            $options->{constructor_class} || 'Class::MOP::Method::Constructor'
+        ),
         'destructor_class' => $options->{destructor_class},
     }, $class;
 }
@@ -235,7 +248,7 @@ sub _check_metaclass_compatibility {
     sub DESTROY {
         my $self = shift;
 
-        return if Class::MOP::in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
+        return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
 
         no warnings 'uninitialized';
         return unless $self->name =~ /^$ANON_CLASS_PREFIX/;
@@ -504,11 +517,16 @@ sub superclasses {
         # we don't know about
 
         $self->_check_metaclass_compatibility();
-        $self->update_meta_instance_dependencies();
+        $self->_superclasses_updated();
     }
     @{$self->get_package_symbol($var_spec)};
 }
 
+sub _superclasses_updated {
+    my $self = shift;
+    $self->update_meta_instance_dependencies();
+}
+
 sub subclasses {
     my $self = shift;
     my $super_class = $self->name;
@@ -516,6 +534,16 @@ sub subclasses {
     return @{ $super_class->mro::get_isarev() };
 }
 
+sub direct_subclasses {
+    my $self = shift;
+    my $super_class = $self->name;
+
+    return grep {
+        grep {
+            $_ eq $super_class
+        } Class::MOP::Class->initialize($_)->superclasses
+    } $self->subclasses;
+}
 
 sub linearized_isa {
     return @{ mro::get_linear_isa( (shift)->name ) };
@@ -596,10 +624,16 @@ sub add_method {
     # method. This is hackier, but quicker too.
     $self->{methods}{$method_name} = $method;
     
-    my $full_method_name = ($self->name . '::' . $method_name);    
+    my ( $current_package, $current_name ) = Class::MOP::get_code_info($body);
+
+    if ( $current_name eq '__ANON__' ) {
+        my $full_method_name = ($self->name . '::' . $method_name);
+        subname($full_method_name => $body);
+    }
+
     $self->add_package_symbol(
-        { sigil => '&', type => 'CODE', name => $method_name }, 
-        Class::MOP::subname($full_method_name => $body)
+        { sigil => '&', type => 'CODE', name => $method_name },
+        $body,
     );
 }
 
@@ -636,7 +670,7 @@ sub add_method {
             || confess "You must pass in a method name";
         my $method = $fetch_and_prepare_method->($self, $method_name);
         $method->add_before_modifier(
-            Class::MOP::subname(':before' => $method_modifier)
+            subname(':before' => $method_modifier)
         );
     }
 
@@ -646,7 +680,7 @@ sub add_method {
             || confess "You must pass in a method name";
         my $method = $fetch_and_prepare_method->($self, $method_name);
         $method->add_after_modifier(
-            Class::MOP::subname(':after' => $method_modifier)
+            subname(':after' => $method_modifier)
         );
     }
 
@@ -656,7 +690,7 @@ sub add_method {
             || confess "You must pass in a method name";
         my $method = $fetch_and_prepare_method->($self, $method_name);
         $method->add_around_modifier(
-            Class::MOP::subname(':around' => $method_modifier)
+            subname(':around' => $method_modifier)
         );
     }
 
@@ -814,6 +848,11 @@ sub add_attribute {
     } else {
         $self->invalidate_meta_instances();
     }
+    
+    # get our count of previously inserted attributes and
+    # increment by one so this attribute knows its order
+    my $order = (scalar keys %{$self->get_attribute_map}) - 1; 
+    $attribute->_set_insertion_order($order + 1);
 
     # then onto installing the new accessors
     $self->get_attribute_map->{$attribute->name} = $attribute;
@@ -982,10 +1021,10 @@ sub _immutable_options {
         inline_constructor => 1,
         inline_destructor  => 0,
         debug              => 0,
-        immutable_trait   => $self->immutable_trait,
-        constructor_name  => $self->constructor_name,
-        constructor_class => $self->constructor_class,
-        destructor_class  => $self->destructor_class,
+        immutable_trait    => $self->immutable_trait,
+        constructor_name   => $self->constructor_name,
+        constructor_class  => $self->constructor_class,
+        destructor_class   => $self->destructor_class,
         @args,
     );
 }
@@ -994,15 +1033,15 @@ sub make_immutable {
     my ( $self, @args ) = @_;
 
     if ( $self->is_mutable ) {
-        $self->_initialize_immutable($self->_immutable_options(@args));
+        $self->_initialize_immutable( $self->_immutable_options(@args) );
         $self->_rebless_as_immutable(@args);
         return $self;
-    } else {
+    }
+    else {
         return;
     }
 }
 
-
 sub make_mutable {
     my $self = shift;
 
@@ -1012,12 +1051,21 @@ sub make_mutable {
         $self->_remove_inlined_code(@args);
         delete $self->{__immutable};
         return $self;
-    } else {
+    }
+    else {
         return;
     }
 }
 
-sub immutable_metaclass {
+sub _rebless_as_immutable {
+    my ( $self, @args ) = @_;
+
+    $self->{__immutable}{original_class} = ref $self;
+
+    bless $self => $self->_immutable_metaclass(@args);
+}
+
+sub _immutable_metaclass {
     my ( $self, %args ) = @_;
 
     if ( my $class = $args{immutable_metaclass} ) {
@@ -1032,20 +1080,27 @@ sub immutable_metaclass {
     my $class_name;
 
     if ( $meta_attr and $trait eq $meta_attr->default ) {
-        # if the trait is the same as the default we try and pick a predictable
-        # name for the immutable metaclass
+
+       # if the trait is the same as the default we try and pick a predictable
+       # name for the immutable metaclass
         $class_name = "Class::MOP::Class::Immutable::" . ref($self);
-    } else {
-        $class_name = join("::", "Class::MOP::Class::Immutable::CustomTrait", $trait, "ForMetaClass", ref($self));
+    }
+    else {
+        $class_name
+            = join( "::", "Class::MOP::Class::Immutable::CustomTrait", $trait,
+                    "ForMetaClass", ref($self) );
     }
 
     if ( Class::MOP::is_class_loaded($class_name) ) {
         if ( $class_name->isa($trait) ) {
             return $class_name;
-        } else {
-            confess "$class_name is already defined but does not inherit $trait";
         }
-    } else {
+        else {
+            confess
+                "$class_name is already defined but does not inherit $trait";
+        }
+    }
+    else {
         my @super = ( $trait, ref($self) );
 
         my $meta = Class::MOP::Class->initialize($class_name);
@@ -1057,23 +1112,15 @@ sub immutable_metaclass {
     }
 }
 
-sub _rebless_as_immutable {
-    my ( $self, @args ) = @_;
-
-    $self->{__immutable}{original_class} = ref $self;
-
-    bless $self => $self->immutable_metaclass(@args);
-}
-
 sub _remove_inlined_code {
     my $self = shift;
 
-    $self->remove_method($_->name) for $self->_inlined_methods;
+    $self->remove_method( $_->name ) for $self->_inlined_methods;
 
     delete $self->{__immutable}{inlined_methods};
 }
 
-sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } };
+sub _inlined_methods { @{ $_[0]{__immutable}{inlined_methods} || [] } }
 
 sub _add_inlined_method {
     my ( $self, $method ) = @_;
@@ -1092,9 +1139,9 @@ sub _install_inlined_code {
     my ( $self, %args ) = @_;
 
     # FIXME
-    $self->_inline_accessors(%args) if $args{inline_accessors};
+    $self->_inline_accessors(%args)   if $args{inline_accessors};
     $self->_inline_constructor(%args) if $args{inline_constructor};
-    $self->_inline_destructor(%args) if $args{inline_destructor};
+    $self->_inline_destructor(%args)  if $args{inline_destructor};
 }
 
 sub _rebless_as_mutable {
@@ -1121,14 +1168,14 @@ sub _inline_constructor {
     #if ( my $existing = $self->name->can($args{constructor_name}) ) {
     #    if ( refaddr($existing) == refaddr(\&Moose::Object::new) ) {
 
-    unless ($args{replace_constructor}
-         or !$self->has_method($name) ) {
+    unless ( $args{replace_constructor}
+        or !$self->has_method($name) ) {
         my $class = $self->name;
         warn "Not inlining a constructor for $class since it defines"
-           . " its own constructor.\n"
-           . "If you are certain you don't need to inline your"
-           . " constructor, specify inline_constructor => 0 in your"
-           . " call to $class->meta->make_immutable\n";
+            . " its own constructor.\n"
+            . "If you are certain you don't need to inline your"
+            . " constructor, specify inline_constructor => 0 in your"
+            . " call to $class->meta->make_immutable\n";
         return;
     }
 
@@ -1145,7 +1192,7 @@ sub _inline_constructor {
     );
 
     if ( $args{replace_constructor} or $constructor->can_be_inlined ) {
-        $self->add_method($name => $constructor);
+        $self->add_method( $name => $constructor );
         $self->_add_inlined_method($constructor);
     }
 }
@@ -1161,7 +1208,7 @@ sub _inline_destructor {
 
     Class::MOP::load_class($destructor_class);
 
-    return unless $destructor_class->is_needed( $self );
+    return unless $destructor_class->is_needed($self);
 
     my $destructor = $destructor_class->new(
         options      => \%args,
@@ -1195,12 +1242,12 @@ Class::MOP::Class - Class Meta Object
   # add a method to Foo ...
   Foo->meta->add_method( 'bar' => sub {...} )
 
-      # get a list of all the classes searched
-      # the method dispatcher in the correct order
-      Foo->meta->class_precedence_list()
+  # get a list of all the classes searched
+  # the method dispatcher in the correct order
+  Foo->meta->class_precedence_list()
 
-      # remove a method from Foo
-      Foo->meta->remove_method('bar');
+  # remove a method from Foo
+  Foo->meta->remove_method('bar');
 
   # or use this to actually create classes ...
 
@@ -1209,8 +1256,8 @@ Class::MOP::Class - Class Meta Object
           version      => '0.01',
           superclasses => ['Foo'],
           attributes   => [
-              Class::MOP:: : Attribute->new('$bar'),
-              Class::MOP:: : Attribute->new('$baz'),
+              Class::MOP::Attribute->new('$bar'),
+              Class::MOP::Attribute->new('$baz'),
           ],
           methods => {
               calculate_bar => sub {...},
@@ -1428,7 +1475,13 @@ duplicates removed.
 
 =item B<< $metaclass->subclasses >>
 
-This returns a list of subclasses for this class.
+This returns a list of all subclasses for this class, even indirect
+subclasses.
+
+=item B<< $metaclass->direct_subclasses >>
+
+This returns a list of immediate subclasses for this class, which does not
+include indirect subclasses.
 
 =back