save immutable options across make_mutable/make_immutable
Jesse Luehrs [Thu, 10 Sep 2009 01:47:11 +0000 (20:47 -0500)]
lib/Class/MOP/Class.pm
t/075_immutable_options_saved.t

index 4d4d6f7..a0b4e39 100644 (file)
@@ -939,7 +939,7 @@ sub is_immutable { 0 }
 sub _immutable_options {
     my ( $self, @args ) = @_;
 
-    return (
+    $self->{_immutable_options} ||= {
         inline_accessors   => 1,
         inline_constructor => 1,
         inline_destructor  => 0,
@@ -948,16 +948,22 @@ sub _immutable_options {
         constructor_name   => $self->constructor_name,
         constructor_class  => $self->constructor_class,
         destructor_class   => $self->destructor_class,
+    };
+    $self->{_immutable_options} = {
+        %{ $self->{_immutable_options} },
         @args,
-    );
+    };
+
+    return %{ $self->{_immutable_options} };
 }
 
 sub make_immutable {
     my ( $self, @args ) = @_;
 
     if ( $self->is_mutable ) {
-        $self->_initialize_immutable( $self->_immutable_options(@args) );
-        $self->_rebless_as_immutable(@args);
+        $self->_immutable_options(@args);
+        $self->_initialize_immutable;
+        $self->_rebless_as_immutable;
         return $self;
     }
     else {
@@ -981,11 +987,13 @@ sub make_mutable {
 }
 
 sub _rebless_as_immutable {
-    my ( $self, @args ) = @_;
+    my ( $self ) = @_;
 
     $self->{__immutable}{original_class} = ref $self;
 
-    bless $self => $self->_immutable_metaclass(@args);
+    bless $self => $self->_immutable_metaclass(
+        %{ $self->{_immutable_options} }
+    );
 }
 
 sub _immutable_metaclass {
@@ -1067,10 +1075,10 @@ sub _add_inlined_method {
 }
 
 sub _initialize_immutable {
-    my ( $self, %args ) = @_;
+    my ( $self ) = @_;
 
-    $self->{__immutable}{options} = \%args;
-    $self->_install_inlined_code(%args);
+    $self->{__immutable}{options} = $self->{_immutable_options};
+    $self->_install_inlined_code(%{ $self->{_immutable_options} });
 }
 
 sub _install_inlined_code {
index ba5e2af..d5b2ae3 100644 (file)
@@ -15,9 +15,7 @@ ok(!$meta->has_method('new'),
 
 $meta->make_mutable;
 $meta->make_immutable;
-{ local $TODO = "make_immutable doesn't save options yet";
 ok($meta->has_method('foo'),
    "constructor is generated with correct name by default after roundtrip");
 ok(!$meta->has_method('new'),
    "constructor is not generated with incorrect name by default after roundtrip");
-}