Checking in changes prior to tagging of version 0.40_04. Changelog diff is:
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
7d96ae4d 2use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings
3a63a2e7 3
745220df 4use Mouse::Util::TypeConstraints ();
5
b397e301 6use Carp ();
7use Scalar::Util ();
fce211ae 8
fe1221ad 9my %METAS;
8536d351 10
deb9a0f3 11sub _metaclass_cache { # DEPRECATED
739525d0 12 my($class, $name) = @_;
fe1221ad 13 return $METAS{$name};
739525d0 14}
8536d351 15
739525d0 16sub initialize {
17 my($class, $package_name, @args) = @_;
8536d351 18
739525d0 19 ($package_name && !ref($package_name))
20 || $class->throw_error("You must pass a package name and it cannot be blessed");
8536d351 21
fe1221ad 22 return $METAS{$package_name}
739525d0 23 ||= $class->_construct_meta(package => $package_name, @args);
24}
8536d351 25
739525d0 26sub class_of{
27 my($class_or_instance) = @_;
28 return undef unless defined $class_or_instance;
23274b88 29 return $METAS{ ref($class_or_instance) || $class_or_instance };
8536d351 30}
31
739525d0 32# Means of accessing all the metaclasses that have
33# been initialized thus far
fe1221ad 34#sub get_all_metaclasses { %METAS }
35sub get_all_metaclass_instances { values %METAS }
36sub get_all_metaclass_names { keys %METAS }
37sub get_metaclass_by_name { $METAS{$_[0]} }
38#sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
39#sub weaken_metaclass { weaken($METAS{$_[0]}) }
40#sub does_metaclass_exist { defined $METAS{$_[0]} }
41#sub remove_metaclass_by_name { delete $METAS{$_[0]} }
739525d0 42
43165725 43sub name;
3a63a2e7 44
2591e962 45sub namespace;
46
739525d0 47# The followings are Class::MOP specific methods
48
49#sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
50#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
51#sub identifier {
52# my $self = shift;
53# return join '-' => (
54# $self->name,
55# ($self->version || ()),
56# ($self->authority || ()),
57# );
58#}
23264b5b 59
8536d351 60# add_attribute is an abstract method
61
deb9a0f3 62sub get_attribute_map { # DEPRECATED
3a683350 63 Carp::cluck('get_attribute_map() has been deprecated');
64 return $_[0]->{attributes};
65}
66
8536d351 67sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
68sub get_attribute { $_[0]->{attributes}->{$_[1]} }
69sub get_attribute_list{ keys %{$_[0]->{attributes}} }
6cfa1e5e 70sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
23264b5b 71
01afd8ff 72# XXX: for backward compatibility
73my %foreign = map{ $_ => undef } qw(
74 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
81bc0675 75 Carp Scalar::Util List::Util
01afd8ff 76);
77sub _code_is_mine{
81bc0675 78# my($self, $code) = @_;
8e64d0fa 79
81bc0675 80 return !exists $foreign{ get_code_package($_[1]) };
3a63a2e7 81}
82
3e44140b 83sub add_method;
84
3a63a2e7 85sub has_method {
86 my($self, $method_name) = @_;
87
78dc5ed1 88 defined($method_name)
89 or $self->throw_error('You must define a method name');
90
81bc0675 91 return defined($self->{methods}{$method_name}) || do{
92 my $code = get_code_ref($self->{package}, $method_name);
93 $code && $self->_code_is_mine($code);
94 };
3a63a2e7 95}
96
81bc0675 97sub get_method_body {
71e7b544 98 my($self, $method_name) = @_;
99
100 defined($method_name)
101 or $self->throw_error('You must define a method name');
102
103 return $self->{methods}{$method_name} ||= do{
7d96ae4d 104 my $code = get_code_ref($self->{package}, $method_name);
81bc0675 105 $code && $self->_code_is_mine($code) ? $code : undef;
71e7b544 106 };
107}
108
8536d351 109sub get_method{
6cfa1e5e 110 my($self, $method_name) = @_;
111
81bc0675 112 if(my $code = $self->get_method_body($method_name)){
6cfa1e5e 113 my $method_metaclass = $self->method_metaclass;
114 load_class($method_metaclass);
115
9010458d 116 return $method_metaclass->wrap(
81bc0675 117 body => $code,
9010458d 118 name => $method_name,
60e446e8 119 package => $self->name,
9010458d 120 associated_metaclass => $self,
6cfa1e5e 121 );
122 }
123
124 return undef;
8536d351 125}
3a63a2e7 126
8e64d0fa 127sub get_method_list {
3a63a2e7 128 my($self) = @_;
8e64d0fa 129
130 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 131}
132
7a50b450 133{
134 my $ANON_SERIAL = 0;
7a50b450 135
136 my %IMMORTALS;
137
138 sub create {
7eb3a8d5 139 my($self, $package_name, %options) = @_;
7a50b450 140
7eb3a8d5 141 my $class = ref($self) || $self;
142 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 143
8f40866c 144 my $superclasses;
7a50b450 145 if(exists $options{superclasses}){
f48920c1 146 if(Mouse::Util::is_a_metarole($self)){
7a50b450 147 delete $options{superclasses};
148 }
8f40866c 149 else{
150 $superclasses = delete $options{superclasses};
151 (ref $superclasses eq 'ARRAY')
7eb3a8d5 152 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 153 }
7a50b450 154 }
155
8f40866c 156 my $attributes = delete $options{attributes};
157 if(defined $attributes){
158 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 159 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 160 }
161 my $methods = delete $options{methods};
162 if(defined $methods){
163 (ref $methods eq 'HASH')
7eb3a8d5 164 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 165 }
166 my $roles = delete $options{roles};
167 if(defined $roles){
168 (ref $roles eq 'ARRAY')
7eb3a8d5 169 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 170 }
7a50b450 171 my $mortal;
172 my $cache_key;
173
174 if(!defined $package_name){ # anonymous
175 $mortal = !$options{cache};
176
177 # anonymous but immortal
178 if(!$mortal){
8e64d0fa 179 # something like Super::Class|Super::Class::2=Role|Role::1
180 $cache_key = join '=' => (
8f40866c 181 join('|', @{$superclasses || []}),
182 join('|', sort @{$roles || []}),
7a50b450 183 );
184 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
185 }
8f40866c 186 $options{anon_serial_id} = ++$ANON_SERIAL;
187 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 188 }
189
190 # instantiate a module
191 {
192 no strict 'refs';
193 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
194 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
195 }
196
7eb3a8d5 197 my $meta = $self->initialize( $package_name, %options);
7a50b450 198
b397e301 199 Scalar::Util::weaken $METAS{$package_name}
7a50b450 200 if $mortal;
201
7eb3a8d5 202 $meta->add_method(meta => sub{
203 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 204 });
205
8f40866c 206 $meta->superclasses(@{$superclasses})
207 if defined $superclasses;
7a50b450 208
209 # NOTE:
210 # process attributes first, so that they can
211 # install accessors, but locally defined methods
212 # can then overwrite them. It is maybe a little odd, but
213 # I think this should be the order of things.
214 if (defined $attributes) {
215 if(ref($attributes) eq 'ARRAY'){
8f40866c 216 # array of Mouse::Meta::Attribute
7a50b450 217 foreach my $attr (@{$attributes}) {
8f40866c 218 $meta->add_attribute($attr);
7a50b450 219 }
220 }
221 else{
8f40866c 222 # hash map of name and attribute spec pairs
7a50b450 223 while(my($name, $attr) = each %{$attributes}){
224 $meta->add_attribute($name => $attr);
225 }
226 }
227 }
8f40866c 228 if (defined $methods) {
229 while(my($method_name, $method_body) = each %{$methods}){
230 $meta->add_method($method_name, $method_body);
7a50b450 231 }
232 }
8f40866c 233 if (defined $roles){
234 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 235 }
236
8f40866c 237 if($cache_key){
7a50b450 238 $IMMORTALS{$cache_key} = $meta;
239 }
240
241 return $meta;
242 }
243
244 sub DESTROY{
245 my($self) = @_;
246
247 my $serial_id = $self->{anon_serial_id};
248
249 return if !$serial_id;
250
e75f21db 251 # @ISA is a magical variable, so we clear it manually.
252 @{$self->{superclasses}} = () if exists $self->{superclasses};
253
254 # Then, clear the symbol table hash
255 %{$self->namespace} = ();
7a50b450 256
8f40866c 257 my $name = $self->name;
258 delete $METAS{$name};
259
e75f21db 260 $name =~ s/ $serial_id \z//xms;
7a50b450 261
262 no strict 'refs';
8f40866c 263 delete ${$name}{ $serial_id . '::' };
7a50b450 264
265 return;
266 }
267}
268
8536d351 269sub throw_error{
270 my($class, $message, %args) = @_;
271
fce211ae 272 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 273 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 274
8536d351 275 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
276 Carp::croak($message);
277 }
278 else{
279 Carp::confess($message);
280 }
281}
3a63a2e7 282
2831;
284
285__END__
286
287=head1 NAME
288
1820fffe 289Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 290
a25ca8d6 291=head1 VERSION
292
6a97bbda 293This document describes Mouse version 0.40_04
a25ca8d6 294
b995be6a 295=head1 SEE ALSO
296
31c5194b 297L<Class::MOP::Class>
298
299L<Class::MOP::Module>
300
301L<Class::MOP::Package>
b995be6a 302
3a63a2e7 303=cut
2cea7a5f 304