use X Protocol phrase in docs for each module
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
index 6a049b8..abfec64 100644 (file)
@@ -73,8 +73,7 @@ sub construct_class_instance {
     # now create the metaclass
     my $meta;
     if ($class eq 'Class::MOP::Class') {
-        no strict 'refs';
-        $meta = $class->_new($options)
+        $meta = $class->_new($options);
     }
     else {
         # NOTE:
@@ -973,108 +972,43 @@ sub is_pristine {
 sub is_mutable   { 1 }
 sub is_immutable { 0 }
 
-# NOTE:
-# Why I changed this (groditi)
-#  - One Metaclass may have many Classes through many Metaclass instances
-#  - One Metaclass should only have one Immutable Transformer instance
-#  - Each Class may have different Immutabilizing options
-#  - Therefore each Metaclass instance may have different Immutabilizing options
-#  - We need to store one Immutable Transformer instance per Metaclass
-#  - We need to store one set of Immutable Transformer options per Class
-#  - Upon make_mutable we may delete the Immutabilizing options
-#  - We could clean the immutable Transformer instance when there is no more
-#      immutable Classes of that type, but we can also keep it in case
-#      another class with this same Metaclass becomes immutable. It is a case
-#      of trading of storing an instance to avoid unnecessary instantiations of
-#      Immutable Transformers. You may view this as a memory leak, however
-#      Because we have few Metaclasses, in practice it seems acceptable
-#  - To allow Immutable Transformers instances to be cleaned up we could weaken
-#      the reference stored in  $IMMUTABLE_TRANSFORMERS{$class} and ||= should DWIM
+sub immutable_transformer { $_[0]->{immutable_transformer} }
+sub _set_immutable_transformer { $_[0]->{immutable_transformer} = $_[1] }
 
 {
-
-    my %IMMUTABLE_TRANSFORMERS;
-    my %IMMUTABLE_OPTIONS;
-
-    sub get_immutable_options {
-        my $self = shift;
-        return if $self->is_mutable;
-        confess "unable to find immutabilizing options"
-            unless exists $IMMUTABLE_OPTIONS{$self->name};
-        my %options = %{$IMMUTABLE_OPTIONS{$self->name}};
-        delete $options{IMMUTABLE_TRANSFORMER};
-        return \%options;
-    }
-
-    sub get_immutable_transformer {
-        my $self = shift;
-        if( $self->is_mutable ){
-            return $IMMUTABLE_TRANSFORMERS{$self->name} ||= $self->create_immutable_transformer;
-        }
-        confess "unable to find transformer for immutable class"
-            unless exists $IMMUTABLE_OPTIONS{$self->name};
-        return $IMMUTABLE_OPTIONS{$self->name}->{IMMUTABLE_TRANSFORMER};
-    }
-
-    sub make_immutable {
-        my $self = shift;
-        my %options = @_;
-
-        my $transformer = $self->get_immutable_transformer;
-        $transformer->make_metaclass_immutable($self, \%options);
-        $IMMUTABLE_OPTIONS{$self->name} =
-            { %options,  IMMUTABLE_TRANSFORMER => $transformer };
-
-        if( exists $options{debug} && $options{debug} ){
-            print STDERR "# of Metaclass options:      ", keys %IMMUTABLE_OPTIONS;
-            print STDERR "# of Immutable transformers: ", keys %IMMUTABLE_TRANSFORMERS;
-        }
-
-        1;
-    }
-
-    sub make_mutable{
-        my $self = shift;
-        return if $self->is_mutable;
-        my $options = delete $IMMUTABLE_OPTIONS{$self->name};
-        confess "unable to find immutabilizing options" unless ref $options;
-        my $transformer = delete $options->{IMMUTABLE_TRANSFORMER};
-        $transformer->make_metaclass_mutable($self, $options);
-        1;
-    }
-}
-
-sub create_immutable_transformer {
-    my $self = shift;
-    my $class = Class::MOP::Immutable->new($self, {
+    my %Default_Immutable_Options = (
         read_only   => [qw/superclasses/],
-        cannot_call => [qw/
-           add_method
-           alias_method
-           remove_method
-           add_attribute
-           remove_attribute
-           remove_package_symbol
-        /],
-        memoize     => {
-           class_precedence_list             => 'ARRAY',
-           linearized_isa                    => 'ARRAY', # FIXME perl 5.10 memoizes this on its own, no need?
-           get_all_methods                   => 'ARRAY',
-           get_all_method_names              => 'ARRAY',
-           #get_all_attributes               => 'ARRAY', # it's an alias, no need, but maybe in the future
-           compute_all_applicable_attributes => 'ARRAY',
-           get_meta_instance                 => 'SCALAR',
-           get_method_map                    => 'SCALAR',
+        cannot_call => [
+            qw/
+                add_method
+                alias_method
+                remove_method
+                add_attribute
+                remove_attribute
+                remove_package_symbol
+                /
+        ],
+        memoize => {
+            class_precedence_list => 'ARRAY',
+            # FIXME perl 5.10 memoizes this on its own, no need?
+            linearized_isa                    => 'ARRAY',
+            get_all_methods                   => 'ARRAY',
+            get_all_method_names              => 'ARRAY',
+            compute_all_applicable_attributes => 'ARRAY',
+            get_meta_instance                 => 'SCALAR',
+            get_method_map                    => 'SCALAR',
         },
+
         # NOTE:
-        # this is ugly, but so are typeglobs, 
+        # this is ugly, but so are typeglobs,
         # so whattayahgonnadoboutit
         # - SL
-        wrapped => { 
+        wrapped => {
             add_package_symbol => sub {
                 my $original = shift;
-                confess "Cannot add package symbols to an immutable metaclass" 
-                    unless (caller(2))[3] eq 'Class::MOP::Package::get_package_symbol'; 
+                confess "Cannot add package symbols to an immutable metaclass"
+                    unless ( caller(2) )[3] eq
+                    'Class::MOP::Package::get_package_symbol';
 
                 # This is a workaround for a bug in 5.8.1 which thinks that
                 # goto $original->body
@@ -1083,8 +1017,32 @@ sub create_immutable_transformer {
                 goto $body;
             },
         },
-    });
-    return $class;
+    );
+
+    sub make_immutable {
+        my $self = shift;
+
+        return if $self->is_immutable;
+
+        my $transformer = $self->immutable_transformer
+            || Class::MOP::Immutable->new(
+            $self,
+            %Default_Immutable_Options,
+            @_
+            );
+
+        $self->_set_immutable_transformer($transformer);
+
+        $transformer->make_metaclass_immutable;
+    }
+}
+
+sub make_mutable {
+    my $self = shift;
+
+    return if $self->is_mutable;
+
+    $self->immutable_transformer->make_metaclass_mutable;
 }
 
 1;
@@ -1133,11 +1091,11 @@ Class::MOP::Class - Class Meta Object
 
 =head1 DESCRIPTION
 
-This is the largest and most complex part of the Class::MOP
-meta-object protocol. It controls the introspection and manipulation
-of Perl 5 classes, and it can create them as wlel. The best way to
-understand what this module can do, is to read the documentation for
-each of its methods.
+The Class Protocol is the largest and most complex part of the
+Class::MOP meta-object protocol. It controls the introspection and
+manipulation of Perl 5 classes, and it can create them as well. The
+best way to understand what this module can do, is to read the
+documentation for each of its methods.
 
 =head1 INHERITANCE
 
@@ -1245,7 +1203,7 @@ instances.
 This method clones an existing object instance. Any parameters you
 provide are will override existing attribute values in the object.
 
-This is a convience method for cloning an object instance, then
+This is a convenience method for cloning an object instance, then
 blessing it into the appropriate package.
 
 You could implement a clone method in your class, using this method:
@@ -1419,6 +1377,27 @@ This method returns the first method in any superclass matching the
 given name. It is effectively the method that C<SUPER::$method_name>
 would dispatch to.
 
+=item B<< $metaclass->add_method($method_name, $method) >>
+
+This method takes a method name and a subroutine reference, and adds
+the method to the class.
+
+The subroutine reference can be a L<Class::MOP::Method>, and you are
+strongly encouraged to pass a meta method object instead of a code
+reference. If you do so, that object gets stored as part of the
+class's method map directly. If not, the meta information will have to
+be recreated later, and may be incorrect.
+
+If you provide a method object, this method will clone that object if
+the object's package name does not match the class name. This lets us
+track the original source of any methods added from other classes
+(notably Moose roles).
+
+=item B<< $metaclass->remove_method($method_name) >>
+
+Remove the named method from the class. This method returns the
+L<Class::MOP::Method> object for the method.
+
 =back
 
 =head2 Attribute introspection and creation
@@ -1507,7 +1486,7 @@ Making a class immutable "freezes" the class definition. You can no
 longer call methods which alter the class, such as adding or removing
 methods or attributes.
 
-Making a class immutable lets us optimize the class by inlning some
+Making a class immutable lets us optimize the class by inlining some
 methods, and also allows us to optimize some methods on the metaclass
 object itself.
 
@@ -1528,7 +1507,7 @@ documentation.
 
 Calling this method reverse the immutabilization transformation.
 
-=item B<< $metaclass->get_immutable_transformer >>
+=item B<< $metaclass->immutable_transformer >>
 
 If the class has been made immutable previously, this returns the
 L<Class::MOP::Immutable> object that was created to do the
@@ -1551,8 +1530,8 @@ parent classes.
 
 Method modifiers work by wrapping the original method and then
 replacing it in the class's symbol table. The wrappers will handle
-calling all the modifiers in the appropariate orders and preserving
-the calling context for the original method.
+calling all the modifiers in the appropriate order and preserving the
+calling context for the original method.
 
 The return values of C<before> and C<after> modifiers are
 ignored. This is because their purpose is B<not> to filter the input
@@ -1644,6 +1623,20 @@ The return value of the modifier is what will be seen by the caller.
 
 =back
 
+=head2 Introspection
+
+=over 4
+
+=item B<< Class::MOP::Class->meta >>
+
+This will return a L<Class::MOP::Class> instance for this class.
+
+It should also be noted that L<Class::MOP> will actually bootstrap
+this module by installing a number of attribute meta-objects into its
+metaclass.
+
+=back
+
 =head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>