Hacking on making immutable a real(ish) trait so it doesn't show up in
[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
b7e04496 11our $VERSION = '0.88';
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
6446bd59 20sub is_mutable {0}
21sub is_immutable {1}
f5d08022 22
23sub superclasses {
24 confess "This method is read-only" if @_ > 1;
25 $_[0]->next::method;
26}
27
6446bd59 28sub _immutable_cannot_call {
29 Carp::confess "This method cannot be called on an immutable instance";
30}
f5d08022 31
32sub add_method { shift->_immutable_cannot_call }
33sub alias_method { shift->_immutable_cannot_call }
34sub remove_method { shift->_immutable_cannot_call }
35sub add_attribute { shift->_immutable_cannot_call }
36sub remove_attribute { shift->_immutable_cannot_call }
37sub remove_package_symbol { shift->_immutable_cannot_call }
38
6446bd59 39sub class_precedence_list {
40 @{ $_[0]{__immutable}{class_precedence_list}
41 ||= [ shift->next::method ] };
42}
43
44sub linearized_isa {
45 @{ $_[0]{__immutable}{linearized_isa} ||= [ shift->next::method ] };
46}
47
48sub get_all_methods {
49 @{ $_[0]{__immutable}{get_all_methods} ||= [ shift->next::method ] };
50}
51
52sub get_all_method_names {
53 @{ $_[0]{__immutable}{get_all_method_names} ||= [ shift->next::method ] };
54}
55
56sub get_all_attributes {
57 @{ $_[0]{__immutable}{get_all_attributes} ||= [ shift->next::method ] };
58}
f5d08022 59
6446bd59 60sub get_meta_instance {
61 $_[0]{__immutable}{get_meta_instance} ||= shift->next::method;
62}
63
64sub get_method_map {
65 $_[0]{__immutable}{get_method_map} ||= shift->next::method;
66}
f5d08022 67
68sub add_package_symbol {
69 confess "Cannot add package symbols to an immutable metaclass"
6446bd59 70 unless ( caller(1) )[3] eq 'Class::MOP::Package::get_package_symbol';
f5d08022 71
72 shift->next::method(@_);
73}
74
751;
15726b75 76
77__END__
78
79=pod
80
81=head1 NAME
82
83Class::MOP::Class::Immutable::Trait - Implements immutability for metaclass objects
84
85=head1 DESCRIPTION
86
9e25e01f 87This class provides a pseudo-trait that is applied to immutable metaclass
88objects. In reality, it is simply a parent class.
89
90It implements caching and read-only-ness for various metaclass methods.
15726b75 91
92=head1 AUTHOR
93
94Yuval Kogman E<lt>nothingmuch@cpan.orgE<gt>
95
96=head1 COPYRIGHT AND LICENSE
97
98Copyright 2009 by Infinity Interactive, Inc.
99
100L<http://www.iinteractive.com>
101
102This library is free software; you can redistribute it and/or modify
103it under the same terms as Perl itself.
104
105=cut
106