simpler get_all_method_names implementation
[gitmo/Moose.git] / lib / Class / MOP / Class.pm
index 45dfd46..f7cf3d7 100644 (file)
@@ -11,6 +11,7 @@ use Class::MOP::Method::Constructor;
 use Class::MOP::MiniTrait;
 
 use Carp         'confess';
+use Class::Load  'is_class_loaded', 'load_class';
 use Scalar::Util 'blessed', 'reftype', 'weaken';
 use Sub::Name    'subname';
 use Try::Tiny;
@@ -26,7 +27,7 @@ sub initialize {
     my $class = shift;
 
     my $package_name;
-    
+
     if ( @_ % 2 ) {
         $package_name = shift;
     } else {
@@ -101,7 +102,7 @@ sub _construct_class_instance {
     }
 
     # and check the metaclass compatibility
-    $meta->_check_metaclass_compatibility();  
+    $meta->_check_metaclass_compatibility();
 
     Class::MOP::store_metaclass_by_name($package_name, $meta);
 
@@ -715,7 +716,7 @@ sub get_meta_instance {
 
 sub _create_meta_instance {
     my $self = shift;
-    
+
     my $instance = $self->instance_metaclass->new(
         associated_metaclass => $self,
         attributes => [ $self->get_all_attributes() ],
@@ -727,6 +728,7 @@ sub _create_meta_instance {
     return $instance;
 }
 
+# TODO: this is actually not being used!
 sub _inline_rebless_instance {
     my $self = shift;
 
@@ -995,18 +997,18 @@ sub class_precedence_list {
     my $self = shift;
     my $name = $self->name;
 
-    unless (Class::MOP::IS_RUNNING_ON_5_10()) { 
+    unless (Class::MOP::IS_RUNNING_ON_5_10()) {
         # NOTE:
         # We need to check for circular inheritance here
-        # if we are are not on 5.10, cause 5.8 detects it 
-        # late. This will do nothing if all is well, and 
+        # if we are are not on 5.10, cause 5.8 detects it
+        # late. This will do nothing if all is well, and
         # blow up otherwise. Yes, it's an ugly hack, better
-        # suggestions are welcome.        
+        # suggestions are welcome.
         # - SL
-        ($name || return)->isa('This is a test for circular inheritance') 
+        ($name || return)->isa('This is a test for circular inheritance')
     }
 
-    # if our mro is c3, we can 
+    # if our mro is c3, we can
     # just grab the linear_isa
     if (mro::get_mro($name) eq 'c3') {
         return @{ mro::get_linear_isa($name) }
@@ -1014,7 +1016,7 @@ sub class_precedence_list {
     else {
         # NOTE:
         # we can't grab the linear_isa for dfs
-        # since it has all the duplicates 
+        # since it has all the duplicates
         # already removed.
         return (
             $name,
@@ -1107,7 +1109,7 @@ sub find_method_by_name {
     my ($self, $method_name) = @_;
     (defined $method_name && length $method_name)
         || confess "You must define a method name to find";
-    foreach my $class ($self->linearized_isa) {
+    foreach my $class ($self->linearized_isa, 'UNIVERSAL') {
         my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
         return $method if defined $method;
     }
@@ -1118,7 +1120,7 @@ sub get_all_methods {
     my $self = shift;
 
     my %methods;
-    for my $class ( reverse $self->linearized_isa ) {
+    for my $class ( 'UNIVERSAL', reverse $self->linearized_isa ) {
         my $meta = Class::MOP::Class->initialize($class);
 
         $methods{ $_->name } = $_ for $meta->_get_local_methods;
@@ -1129,8 +1131,7 @@ sub get_all_methods {
 
 sub get_all_method_names {
     my $self = shift;
-    my %uniq;
-    return grep { !$uniq{$_}++ } map { Class::MOP::Class->initialize($_)->get_method_list } $self->linearized_isa;
+    map { $_->name } $self->get_all_methods;
 }
 
 sub find_all_methods_by_name {
@@ -1138,7 +1139,7 @@ sub find_all_methods_by_name {
     (defined $method_name && length $method_name)
         || confess "You must define a method name to find";
     my @methods;
-    foreach my $class ($self->linearized_isa) {
+    foreach my $class ($self->linearized_isa, 'UNIVERSAL') {
         # fetch the meta-class ...
         my $meta = Class::MOP::Class->initialize($class);
         push @methods => {
@@ -1154,7 +1155,7 @@ sub find_next_method_by_name {
     my ($self, $method_name) = @_;
     (defined $method_name && length $method_name)
         || confess "You must define a method name to find";
-    my @cpl = $self->linearized_isa;
+    my @cpl = ($self->linearized_isa, 'UNIVERSAL');
     shift @cpl; # discard ourselves
     foreach my $class (@cpl) {
         my $method = Class::MOP::Class->initialize($class)->get_method($method_name);
@@ -1269,7 +1270,7 @@ sub _immutable_options {
 sub make_immutable {
     my ( $self, @args ) = @_;
 
-    return if not $self->is_mutable;
+    return unless $self->is_mutable;
 
     my ($file, $line) = (caller)[1..2];
 
@@ -1279,6 +1280,7 @@ sub make_immutable {
         $self->_immutable_options(@args),
     );
     $self->_rebless_as_immutable(@args);
+
     return $self;
 }
 
@@ -1331,7 +1333,7 @@ sub _immutable_metaclass {
     }
 
     return $class_name
-        if Class::MOP::is_class_loaded($class_name);
+        if is_class_loaded($class_name);
 
     # If the metaclass is a subclass of CMOP::Class which has had
     # metaclass roles applied (via Moose), then we want to make sure
@@ -1421,7 +1423,7 @@ sub _inline_constructor {
 
     my $constructor_class = $args{constructor_class};
 
-    Class::MOP::load_class($constructor_class);
+    load_class($constructor_class);
 
     my $constructor = $constructor_class->new(
         options      => \%args,
@@ -1458,7 +1460,7 @@ sub _inline_destructor {
 
     my $destructor_class = $args{destructor_class};
 
-    Class::MOP::load_class($destructor_class);
+    load_class($destructor_class);
 
     return unless $destructor_class->is_needed($self);