2 package Class::MOP::Immutable;
7 use Class::MOP::Method::Constructor;
10 use Scalar::Util 'blessed';
12 our $VERSION = '0.01';
13 our $AUTHORITY = 'cpan:STEVAN';
16 my ($class, $metaclass, $options) = @_;
19 '$!metaclass' => $metaclass,
20 '%!options' => $options,
21 '$!immutable_metaclass' => undef,
25 # we initialize the immutable
26 # version of the metaclass here
27 $self->create_immutable_metaclass;
32 sub immutable_metaclass { (shift)->{'$!immutable_metaclass'} }
33 sub metaclass { (shift)->{'$!metaclass'} }
34 sub options { (shift)->{'%!options'} }
36 sub create_immutable_metaclass {
40 # The immutable version of the
41 # metaclass is just a anon-class
42 # which shadows the methods
44 $self->{'$!immutable_metaclass'} = Class::MOP::Class->create_anon_class(
45 superclasses => [ blessed($self->metaclass) ],
46 methods => $self->create_methods_for_immutable_metaclass,
50 my %DEFAULT_METHODS = (
53 # if it is not blessed, then someone is asking
54 # for the meta of Class::MOP::Class::Immutable
55 return Class::MOP::Class->initialize($self) unless blessed($self);
56 # otherwise, they are asking for the metaclass
57 # which has been made immutable, which is itself
60 is_mutable => sub { 0 },
61 is_immutable => sub { 1 },
62 make_immutable => sub { ( ) },
66 # this will actually convert the
67 # existing metaclass to an immutable
69 sub make_metaclass_immutable {
70 my ($self, $metaclass, %options) = @_;
72 $options{inline_accessors} = 1 unless exists $options{inline_accessors};
73 $options{inline_constructor} = 1 unless exists $options{inline_constructor};
74 $options{inline_destructor} = 0 unless exists $options{inline_destructor};
75 $options{constructor_name} = 'new' unless exists $options{constructor_name};
76 $options{debug} = 0 unless exists $options{debug};
78 if ($options{inline_accessors}) {
79 foreach my $attr_name ($metaclass->get_attribute_list) {
80 # inline the accessors
81 $metaclass->get_attribute($attr_name)
82 ->install_accessors(1);
86 if ($options{inline_constructor}) {
87 my $constructor_class = $options{constructor_class} || 'Class::MOP::Method::Constructor';
89 $metaclass->add_method(
90 $options{constructor_name},
91 $constructor_class->new(
93 metaclass => $metaclass,
95 ) unless $metaclass->has_method($options{constructor_name});
98 if ($options{inline_destructor}) {
99 (exists $options{destructor_class})
100 || confess "The 'inline_destructor' option is present, but "
101 . "no destructor class was specified";
103 my $destructor_class = $options{destructor_class};
105 my $destructor = $destructor_class->new(
106 options => \%options,
107 metaclass => $metaclass,
110 $metaclass->add_method('DESTROY' => $destructor)
112 # we allow the destructor to determine
113 # if it is needed or not, it can perform
114 # all sorts of checks because it has the
116 if $destructor->is_needed;
119 my $memoized_methods = $self->options->{memoize};
120 foreach my $method_name (keys %{$memoized_methods}) {
121 my $type = $memoized_methods->{$method_name};
123 ($metaclass->can($method_name))
124 || confess "Could not find the method '$method_name' in " . $metaclass->name;
127 if ($type eq 'ARRAY') {
128 $metaclass->{'___' . $method_name} = [ $metaclass->$method_name ];
130 elsif ($type eq 'HASH') {
131 $metaclass->{'___' . $method_name} = { $metaclass->$method_name };
133 elsif ($type eq 'SCALAR') {
134 $metaclass->{'___' . $method_name} = $metaclass->$method_name;
137 $metaclass->{'___original_class'} = blessed($metaclass);
139 bless $metaclass => $self->immutable_metaclass->name;
142 sub create_methods_for_immutable_metaclass {
145 my %methods = %DEFAULT_METHODS;
147 foreach my $read_only_method (@{$self->options->{read_only}}) {
148 my $method = $self->metaclass->meta->find_method_by_name($read_only_method);
151 || confess "Could not find the method '$read_only_method' in " . $self->metaclass->name;
153 $methods{$read_only_method} = sub {
154 confess "This method is read-only" if scalar @_ > 1;
155 goto &{$method->body}
159 foreach my $cannot_call_method (@{$self->options->{cannot_call}}) {
160 $methods{$cannot_call_method} = sub {
161 confess "This method ($cannot_call_method) cannot be called on an immutable instance";
165 my $memoized_methods = $self->options->{memoize};
167 foreach my $method_name (keys %{$memoized_methods}) {
168 my $type = $memoized_methods->{$method_name};
169 if ($type eq 'ARRAY') {
170 $methods{$method_name} = sub { @{$_[0]->{'___' . $method_name}} };
172 elsif ($type eq 'HASH') {
173 $methods{$method_name} = sub { %{$_[0]->{'___' . $method_name}} };
175 elsif ($type eq 'SCALAR') {
176 $methods{$method_name} = sub { $_[0]->{'___' . $method_name} };
180 $methods{get_mutable_metaclass_name} = sub { (shift)->{'___original_class'} };
193 Class::MOP::Immutable - A class to transform Class::MOP::Class metaclasses
209 =item B<immutable_metaclass>
215 =item B<create_immutable_metaclass>
217 =item B<create_methods_for_immutable_metaclass>
219 =item B<make_metaclass_immutable>
225 Stevan Little E<lt>stevan@iinteractive.comE<gt>
227 =head1 COPYRIGHT AND LICENSE
229 Copyright 2006 by Infinity Interactive, Inc.
231 L<http://www.iinteractive.com>
233 This library is free software; you can redistribute it and/or modify
234 it under the same terms as Perl itself.