make github the primary repository
[gitmo/Moose.git] / lib / Class / MOP / Class / Immutable / Trait.pm
CommitLineData
38bf2a25 1package Class::MOP::Class::Immutable::Trait;
2
3use strict;
4use warnings;
5
6use MRO::Compat;
7
8use Carp 'confess';
9use Scalar::Util 'blessed', 'weaken';
10
38bf2a25 11# the original class of the metaclass instance
12sub _get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
13
14sub is_mutable { 0 }
15sub is_immutable { 1 }
16
17sub _immutable_metaclass { ref $_[1] }
18
c5ef5096 19sub _immutable_read_only {
20 my $name = shift;
21 confess "The '$name' method is read-only when called on an immutable instance";
38bf2a25 22}
23
24sub _immutable_cannot_call {
25 my $name = shift;
26 Carp::confess "The '$name' method cannot be called on an immutable instance";
27}
28
c5ef5096 29for my $name (qw/superclasses/) {
30 no strict 'refs';
31 *{__PACKAGE__."::$name"} = sub {
32 my $orig = shift;
33 my $self = shift;
34 _immutable_read_only($name) if @_;
35 $self->$orig;
36 };
37}
38
38bf2a25 39for my $name (qw/add_method alias_method remove_method add_attribute remove_attribute remove_package_symbol add_package_symbol/) {
40 no strict 'refs';
41 *{__PACKAGE__."::$name"} = sub { _immutable_cannot_call($name) };
42}
43
44sub class_precedence_list {
45 my $orig = shift;
46 my $self = shift;
47 @{ $self->{__immutable}{class_precedence_list}
48 ||= [ $self->$orig ] };
49}
50
51sub linearized_isa {
52 my $orig = shift;
53 my $self = shift;
54 @{ $self->{__immutable}{linearized_isa} ||= [ $self->$orig ] };
55}
56
57sub get_all_methods {
58 my $orig = shift;
59 my $self = shift;
60 @{ $self->{__immutable}{get_all_methods} ||= [ $self->$orig ] };
61}
62
63sub get_all_method_names {
64 my $orig = shift;
65 my $self = shift;
66 @{ $self->{__immutable}{get_all_method_names} ||= [ $self->$orig ] };
67}
68
69sub get_all_attributes {
70 my $orig = shift;
71 my $self = shift;
72 @{ $self->{__immutable}{get_all_attributes} ||= [ $self->$orig ] };
73}
74
75sub get_meta_instance {
76 my $orig = shift;
77 my $self = shift;
78 $self->{__immutable}{get_meta_instance} ||= $self->$orig;
79}
80
81sub _method_map {
82 my $orig = shift;
83 my $self = shift;
84 $self->{__immutable}{_method_map} ||= $self->$orig;
85}
86
871;
88
89# ABSTRACT: Implements immutability for metaclass objects
90
91__END__
92
93=pod
94
95=head1 DESCRIPTION
96
97This class provides a pseudo-trait that is applied to immutable metaclass
98objects. In reality, it is simply a parent class.
99
100It implements caching and read-only-ness for various metaclass methods.
101
102=cut
103