micro optimization for method modifiers.
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
index cd23c63..07b54e7 100644 (file)
@@ -69,18 +69,31 @@ sub add_method {
 }
 
 # copied from Class::Inspector
-sub get_method_list {
+my $get_methods_for_class = sub {
     my $self = shift;
-    my $name = $self->name;
+    my $name = shift;
 
     no strict 'refs';
     # Get all the CODE symbol table entries
     my @functions =
-      grep !/^(?:has|with|around|before|after|blessed|extends|confess)$/,
+      grep !/^(?:has|with|around|before|after|blessed|extends|confess|override|super)$/,
       grep { defined &{"${name}::$_"} }
       keys %{"${name}::"};
-    push @functions, keys %{$self->{'methods'}->{$name}};
+    push @functions, keys %{$self->{'methods'}->{$name}} if $self;
     wantarray ? @functions : \@functions;
+};
+
+sub get_method_list {
+    my $self = shift;
+    $get_methods_for_class->($self, $self->name);
+}
+
+sub get_all_method_names {
+    my $self = shift;
+    my %uniq;
+    return grep { $uniq{$_}++ == 0 }
+            map { $get_methods_for_class->(undef, $_) }
+            $self->linearized_isa;
 }
 
 sub add_attribute {
@@ -145,20 +158,25 @@ sub clone_instance {
 
 sub make_immutable {
     my $self = shift;
-    my %args = @_;
+    my %args = (
+        inline_constructor => 1,
+        @_,
+    );
+
     my $name = $self->name;
     $self->{is_immutable}++;
 
-    if ($self->name->can('new') != Mouse::Object->can('new')) {
-        warn "Not inlining a constructor for ".$self->name." since it is not inheriting the default Mouse::Object constructor\n";
-    }
-    else {
+    if ($args{inline_constructor}) {
         $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
     }
 
     if ($args{inline_destructor}) {
         $self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
     }
+
+    # 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;
 }
 
 sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
@@ -169,23 +187,36 @@ sub attribute_metaclass { "Mouse::Meta::Class" }
 
 sub _install_modifier {
     my ( $self, $into, $type, $name, $code ) = @_;
-    if (eval "require Class::Method::Modifiers::Fast; 1") {
-        Class::Method::Modifiers::Fast::_install_modifier( 
-            $into,
-            $type,
-            $name,
-            $code
-        );
-    }
-    else {
-        require Class::Method::Modifiers;
-        Class::Method::Modifiers::_install_modifier( 
-            $into,
-            $type,
-            $name,
-            $code
-        );
+
+    # which is modifer class available?
+    my $modifier_class = do {
+        if (eval "require Class::Method::Modifiers::Fast; 1") {
+            'Class::Method::Modifiers::Fast';
+        } elsif (eval "require Class::Method::Modifiers; 1") {
+            'Class::Method::Modifiers';
+        } else {
+            Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application.");
+        }
+    };
+    my $modifier = $modifier_class->can('_install_modifier');
+
+    # replace this method itself :)
+    {
+        no strict 'refs';
+        no warnings 'redefine';
+        *{__PACKAGE__ . '::_install_modifier'} = sub {
+            my ( $self, $into, $type, $name, $code ) = @_;
+            $modifier->(
+                $into,
+                $type,
+                $name,
+                $code
+            );
+        };
     }
+
+    # call me. for first time.
+    $self->_install_modifier( $into, $type, $name, $code );
 }
 
 sub add_before_method_modifier {