- now stores the instance of the instance
metaclass to avoid needless recomputation
and deletes it when the cache is blown
+ - introduce methods to query Class::MOP::Class for
+ the options used to make it immutable as well as
+ the proper immutable transformer. (groditi)
* Class::MOP::Package
- {add, has, get, remove}_package_symbol all
# the reference stored in $IMMUTABLE_TRANSFORMERS{$class} and ||= should DWIM
{
+
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 ){
+ my $class = blessed $self || $self;
+ return $IMMUTABLE_TRANSFORMERS{$class} ||= $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 $class = blessed $self || $self;
-
- $IMMUTABLE_TRANSFORMERS{$class} ||= $self->create_immutable_transformer;
- my $transformer = $IMMUTABLE_TRANSFORMERS{$class};
+ my $transformer = $self->get_immutable_transformer;
$transformer->make_metaclass_immutable($self, \%options);
$IMMUTABLE_OPTIONS{$self->name} =
{ %options, IMMUTABLE_TRANSFORMER => $transformer };
print STDERR "# of Metaclass options: ", keys %IMMUTABLE_OPTIONS;
print STDERR "# of Immutable transformers: ", keys %IMMUTABLE_TRANSFORMERS;
}
-
+
1;
}
This method will reverse tranforamtion upon the class which
made it immutable.
+=item B<get_immutable_transformer>
+
+Return a transformer suitable for making this class immutable or, if this
+class is immutable, the transformer used to make it immutable.
+
+=item B<get_immutable_options>
+
+If the class is immutable, return the options used to make it immutable.
+
=item B<create_immutable_transformer>
Create a transformer suitable for making this class immutable
use strict;
use warnings;
-use Test::More tests => 84;
+use Test::More tests => 85;
use Test::Exception;
BEGIN {
ok($meta->is_mutable, '... our class is mutable');
ok(!$meta->is_immutable, '... our class is not immutable');
+ my $transformer = $meta->get_immutable_transformer;
+
lives_ok {
$meta->make_immutable();
} '... changed Foo to be immutable';
+ is($transformer, $meta->get_immutable_transformer, '... immutable transformer cache works');
ok(!$meta->make_immutable, '... make immutable now returns nothing');
ok(!$meta->is_mutable, '... our class is no longer mutable');
dies_ok { $meta->add_package_symbol() } '... exception thrown as expected';
dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected';
-
+
lives_ok { $meta->identifier() } '... no exception for get_package_symbol special case';
my @supers;