tweak the initializer args, document set_initial_value
[gitmo/Class-MOP.git] / lib / Class / MOP / Immutable.pm
index 1d91c8d..aabb62b 100644 (file)
@@ -9,7 +9,7 @@ use Class::MOP::Method::Constructor;
 use Carp         'confess';
 use Scalar::Util 'blessed';
 
-our $VERSION   = '0.01';
+our $VERSION   = '0.04';
 our $AUTHORITY = 'cpan:STEVAN';
 
 sub new {
@@ -47,7 +47,9 @@ sub create_immutable_metaclass {
     );
 }
 
+
 my %DEFAULT_METHODS = (
+    # I don't really understand this, but removing it breaks tests (groditi)
     meta => sub {
         my $self = shift;
         # if it is not blessed, then someone is asking
@@ -57,9 +59,9 @@ my %DEFAULT_METHODS = (
         # which has been made immutable, which is itself
         return $self;
     },
-    is_mutable     => sub {  0  },
-    is_immutable   => sub {  1  },
-    make_immutable => sub { ( ) },
+    is_mutable     => sub { 0  },
+    is_immutable   => sub { 1  },
+    make_immutable => sub { () },
 );
 
 # NOTE:
@@ -67,13 +69,19 @@ my %DEFAULT_METHODS = (
 # existing metaclass to an immutable
 # version of itself
 sub make_metaclass_immutable {
-    my ($self, $metaclass, %options) = @_;
+    my ($self, $metaclass, $options) = @_;
+
+    foreach my $pair (
+            [ inline_accessors   => 1     ],
+            [ inline_constructor => 1     ],
+            [ inline_destructor  => 0     ],
+            [ constructor_name   => 'new' ],
+            [ debug              => 0     ],
+        ) {
+        $options->{$pair->[0]} = $pair->[1] unless exists $options->{$pair->[0]};
+    }
 
-    $options{inline_accessors}   = 1     unless exists $options{inline_accessors};
-    $options{inline_constructor} = 1     unless exists $options{inline_constructor};
-    $options{inline_destructor}  = 0     unless exists $options{inline_destructor};
-    $options{constructor_name}   = 'new' unless exists $options{constructor_name};
-    $options{debug}              = 0     unless exists $options{debug};
+    my %options = %$options;
 
     if ($options{inline_accessors}) {
         foreach my $attr_name ($metaclass->get_attribute_list) {
@@ -85,12 +93,12 @@ sub make_metaclass_immutable {
 
     if ($options{inline_constructor}) {
         my $constructor_class = $options{constructor_class} || 'Class::MOP::Method::Constructor';
-
         $metaclass->add_method(
             $options{constructor_name},
             $constructor_class->new(
                 options   => \%options,
                 metaclass => $metaclass,
+                is_inline => 1,
             )
         ) unless $metaclass->has_method($options{constructor_name});
     }
@@ -134,16 +142,14 @@ sub make_metaclass_immutable {
         }
     }
 
-    #I'm not sure i understand this, stevan suggested the addition i don't think its actually needed
-    #my $is_immutable = $metaclass->is_anon_class;
-    #$self->immutable_metaclass->add_method('is_anon_class' => sub { $is_immutable });
-
     $metaclass->{'___original_class'} = blessed($metaclass);
     bless $metaclass => $self->immutable_metaclass->name;
 }
 
 sub make_metaclass_mutable {
-    my ($self, $immutable, %options) = @_;
+    my ($self, $immutable, $options) = @_;
+
+    my %options = %$options;
 
     my $original_class = $immutable->get_mutable_metaclass_name;
     delete $immutable->{'___original_class'} ;
@@ -165,23 +171,23 @@ sub make_metaclass_mutable {
           if $immutable->get_method('DESTROY')->blessed eq $options{destructor_class};
     }
 
-    #14:01 <@stevan> nah,. you shouldnt
-    #14:01 <@stevan> they are just inlined
-    #14:01 <@stevan> which is the default in Moose anyway
-    #14:02 <@stevan> and adding new attributes will just DWIM
-    #14:02 <@stevan> and you really cant change an attribute anyway
-    #if ($options{inline_accessors}) {
-    #    foreach my $attr_name ($immutable->get_attribute_list) {
-    #        my $attr = $immutable->get_attribute($attr_name);
-    #        $attr->remove_accessors;
-    #        $attr->install_accessors(0);
-    #    }
-    #}
-
-    #14:26 <@stevan> the only user of ::Method::Constructor is immutable
-    #14:27 <@stevan> if someone uses it outside of immutable,.. they are either: mst or groditi
-    #14:27 <@stevan> so I am not worried
-    $options{constructor_name} = 'new' unless exists $options{constructor_name};
+    # NOTE:
+    # 14:01 <@stevan> nah,. you shouldnt
+    # 14:01 <@stevan> they are just inlined
+    # 14:01 <@stevan> which is the default in Moose anyway
+    # 14:02 <@stevan> and adding new attributes will just DWIM
+    # 14:02 <@stevan> and you really cant change an attribute anyway
+    # if ($options{inline_accessors}) {
+    #     foreach my $attr_name ($immutable->get_attribute_list) {
+    #         my $attr = $immutable->get_attribute($attr_name);
+    #         $attr->remove_accessors;
+    #         $attr->install_accessors(0);
+    #     }
+    # }
+
+    # 14:26 <@stevan> the only user of ::Method::Constructor is immutable
+    # 14:27 <@stevan> if someone uses it outside of immutable,.. they are either: mst or groditi
+    # 14:27 <@stevan> so I am not worried
     if ($options{inline_constructor}) {
         my $constructor_class = $options{constructor_class} || 'Class::MOP::Method::Constructor';
         $immutable->remove_method( $options{constructor_name}  )
@@ -228,6 +234,8 @@ sub create_methods_for_immutable_metaclass {
 
     $methods{get_mutable_metaclass_name} = sub { (shift)->{'___original_class'} };
 
+    $methods{immutable_transformer} = sub { $self };
+
     return \%methods;
 }
 
@@ -331,7 +339,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006, 2007 by Infinity Interactive, Inc.
+Copyright 2006-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>