bump version to 0.90
[gitmo/Class-MOP.git] / lib / Class / MOP / Class / Immutable / Trait.pm
CommitLineData
f5d08022 1package Class::MOP::Class::Immutable::Trait;
2
3use strict;
4use warnings;
5
6use MRO::Compat;
7
6446bd59 8use Carp 'confess';
f5d08022 9use Scalar::Util 'blessed', 'weaken';
10
41ffa8a4 11our $VERSION = '0.90';
f04900bf 12$VERSION = eval $VERSION;
13our $AUTHORITY = 'cpan:STEVAN';
14
f5d08022 15# the original class of the metaclass instance
16sub get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
17
18sub immutable_options { %{ $_[0]{__immutable}{options} } }
19
78f6e9c6 20sub is_mutable { 0 }
21sub is_immutable { 1 }
f5d08022 22
a986e165 23sub _immutable_metaclass { ref $_[1] }
24
f5d08022 25sub superclasses {
78f6e9c6 26 my $orig = shift;
27 my $self = shift;
28 confess "This method is read-only" if @_;
29 $self->$orig;
f5d08022 30}
31
6446bd59 32sub _immutable_cannot_call {
33 Carp::confess "This method cannot be called on an immutable instance";
34}
f5d08022 35
78f6e9c6 36sub add_method { _immutable_cannot_call() }
37sub alias_method { _immutable_cannot_call() }
38sub remove_method { _immutable_cannot_call() }
39sub add_attribute { _immutable_cannot_call() }
40sub remove_attribute { _immutable_cannot_call() }
41sub remove_package_symbol { _immutable_cannot_call() }
f5d08022 42
6446bd59 43sub class_precedence_list {
78f6e9c6 44 my $orig = shift;
45 my $self = shift;
46 @{ $self->{__immutable}{class_precedence_list}
47 ||= [ $self->$orig ] };
6446bd59 48}
49
50sub linearized_isa {
78f6e9c6 51 my $orig = shift;
52 my $self = shift;
53 @{ $self->{__immutable}{linearized_isa} ||= [ $self->$orig ] };
6446bd59 54}
55
56sub get_all_methods {
78f6e9c6 57 my $orig = shift;
58 my $self = shift;
59 @{ $self->{__immutable}{get_all_methods} ||= [ $self->$orig ] };
6446bd59 60}
61
62sub get_all_method_names {
78f6e9c6 63 my $orig = shift;
64 my $self = shift;
65 @{ $self->{__immutable}{get_all_method_names} ||= [ $self->$orig ] };
6446bd59 66}
67
68sub get_all_attributes {
78f6e9c6 69 my $orig = shift;
70 my $self = shift;
71 @{ $self->{__immutable}{get_all_attributes} ||= [ $self->$orig ] };
6446bd59 72}
f5d08022 73
6446bd59 74sub get_meta_instance {
78f6e9c6 75 my $orig = shift;
76 my $self = shift;
77 $self->{__immutable}{get_meta_instance} ||= $self->$orig;
6446bd59 78}
79
80sub get_method_map {
78f6e9c6 81 my $orig = shift;
82 my $self = shift;
83 $self->{__immutable}{get_method_map} ||= $self->$orig;
6446bd59 84}
f5d08022 85
86sub add_package_symbol {
78f6e9c6 87 my $orig = shift;
88 my $self = shift;
f5d08022 89 confess "Cannot add package symbols to an immutable metaclass"
78f6e9c6 90 unless ( caller(3) )[3] eq 'Class::MOP::Package::get_package_symbol';
f5d08022 91
78f6e9c6 92 $self->$orig(@_);
f5d08022 93}
94
951;
15726b75 96
97__END__
98
99=pod
100
101=head1 NAME
102
103Class::MOP::Class::Immutable::Trait - Implements immutability for metaclass objects
104
105=head1 DESCRIPTION
106
9e25e01f 107This class provides a pseudo-trait that is applied to immutable metaclass
108objects. In reality, it is simply a parent class.
109
110It implements caching and read-only-ness for various metaclass methods.
15726b75 111
112=head1 AUTHOR
113
114Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
115
116=head1 COPYRIGHT AND LICENSE
117
118Copyright 2009 by Infinity Interactive, Inc.
119
120L<http://www.iinteractive.com>
121
122This library is free software; you can redistribute it and/or modify
123it under the same terms as Perl itself.
124
125=cut
126