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