bump version and update Changes
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
index 41ee57e..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.82_01';
+our $VERSION   = '0.85';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -246,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/;
@@ -515,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;
@@ -527,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 ) };
@@ -607,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,
     );
 }
 
@@ -647,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)
         );
     }
 
@@ -657,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)
         );
     }
 
@@ -667,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)
         );
     }
 
@@ -825,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;
@@ -1029,7 +1057,15 @@ sub make_mutable {
     }
 }
 
-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} ) {
@@ -1076,14 +1112,6 @@ 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;
 
@@ -1214,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 ...
 
@@ -1228,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 {...},
@@ -1447,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