More _generate_class_type_for()
[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
6d28c5cf 6use Carp ();
ad022aac 7use Scalar::Util qw/blessed weaken/;
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
3a63a2e7 72sub add_method {
73 my($self, $name, $code) = @_;
74
75 if(!defined $name){
1b9e472d 76 $self->throw_error('You must pass a defined name');
3a63a2e7 77 }
1b9e472d 78 if(!defined $code){
79 $self->throw_error('You must pass a defined code');
80 }
81
3a63a2e7 82 if(ref($code) ne 'CODE'){
78dc5ed1 83 $code = \&{$code}; # coerce
3a63a2e7 84 }
85
8d59c723 86 $self->{methods}->{$name} = $code; # Moose stores meta object here.
3a63a2e7 87
88 my $pkg = $self->name;
89 no strict 'refs';
c6e5eee1 90 no warnings 'redefine', 'once';
3a63a2e7 91 *{ $pkg . '::' . $name } = $code;
92}
93
01afd8ff 94# XXX: for backward compatibility
95my %foreign = map{ $_ => undef } qw(
96 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
97 Carp Scalar::Util
98);
99sub _code_is_mine{
100 my($self, $code) = @_;
8e64d0fa 101
01afd8ff 102 my $package = get_code_package($code);
8e64d0fa 103
01afd8ff 104 return !exists $foreign{$package};
3a63a2e7 105}
106
107sub has_method {
108 my($self, $method_name) = @_;
109
78dc5ed1 110 defined($method_name)
111 or $self->throw_error('You must define a method name');
112
2d2e77f9 113 return 1 if $self->{methods}{$method_name};
5fd0bdcf 114
7d96ae4d 115 my $code = get_code_ref($self->{package}, $method_name);
3a63a2e7 116
117 return $code && $self->_code_is_mine($code);
118}
119
71e7b544 120sub get_method_body{
121 my($self, $method_name) = @_;
122
123 defined($method_name)
124 or $self->throw_error('You must define a method name');
125
126 return $self->{methods}{$method_name} ||= do{
7d96ae4d 127 my $code = get_code_ref($self->{package}, $method_name);
2d2e77f9 128 ($code && $self->_code_is_mine($code)) ? $code : undef;
71e7b544 129 };
130}
131
8536d351 132sub get_method{
6cfa1e5e 133 my($self, $method_name) = @_;
134
135 if($self->has_method($method_name)){
136 my $method_metaclass = $self->method_metaclass;
137 load_class($method_metaclass);
138
9010458d 139 return $method_metaclass->wrap(
60e446e8 140 body => $self->get_method_body($method_name),
9010458d 141 name => $method_name,
60e446e8 142 package => $self->name,
9010458d 143 associated_metaclass => $self,
6cfa1e5e 144 );
145 }
146
147 return undef;
8536d351 148}
3a63a2e7 149
8e64d0fa 150sub get_method_list {
3a63a2e7 151 my($self) = @_;
8e64d0fa 152
153 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 154}
155
7a50b450 156{
157 my $ANON_SERIAL = 0;
7a50b450 158
159 my %IMMORTALS;
160
161 sub create {
7eb3a8d5 162 my($self, $package_name, %options) = @_;
7a50b450 163
7eb3a8d5 164 my $class = ref($self) || $self;
165 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 166
8f40866c 167 my $superclasses;
7a50b450 168 if(exists $options{superclasses}){
745220df 169 if(Mouse::Util::TypeConstraints::_is_a_metarole($self)){
7a50b450 170 delete $options{superclasses};
171 }
8f40866c 172 else{
173 $superclasses = delete $options{superclasses};
174 (ref $superclasses eq 'ARRAY')
7eb3a8d5 175 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 176 }
7a50b450 177 }
178
8f40866c 179 my $attributes = delete $options{attributes};
180 if(defined $attributes){
181 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 182 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 183 }
184 my $methods = delete $options{methods};
185 if(defined $methods){
186 (ref $methods eq 'HASH')
7eb3a8d5 187 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 188 }
189 my $roles = delete $options{roles};
190 if(defined $roles){
191 (ref $roles eq 'ARRAY')
7eb3a8d5 192 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 193 }
7a50b450 194 my $mortal;
195 my $cache_key;
196
197 if(!defined $package_name){ # anonymous
198 $mortal = !$options{cache};
199
200 # anonymous but immortal
201 if(!$mortal){
8e64d0fa 202 # something like Super::Class|Super::Class::2=Role|Role::1
203 $cache_key = join '=' => (
8f40866c 204 join('|', @{$superclasses || []}),
205 join('|', sort @{$roles || []}),
7a50b450 206 );
207 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
208 }
8f40866c 209 $options{anon_serial_id} = ++$ANON_SERIAL;
210 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 211 }
212
213 # instantiate a module
214 {
215 no strict 'refs';
216 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
217 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
218 }
219
7eb3a8d5 220 my $meta = $self->initialize( $package_name, %options);
7a50b450 221
fe1221ad 222 weaken $METAS{$package_name}
7a50b450 223 if $mortal;
224
7eb3a8d5 225 $meta->add_method(meta => sub{
226 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 227 });
228
8f40866c 229 $meta->superclasses(@{$superclasses})
230 if defined $superclasses;
7a50b450 231
232 # NOTE:
233 # process attributes first, so that they can
234 # install accessors, but locally defined methods
235 # can then overwrite them. It is maybe a little odd, but
236 # I think this should be the order of things.
237 if (defined $attributes) {
238 if(ref($attributes) eq 'ARRAY'){
8f40866c 239 # array of Mouse::Meta::Attribute
7a50b450 240 foreach my $attr (@{$attributes}) {
8f40866c 241 $meta->add_attribute($attr);
7a50b450 242 }
243 }
244 else{
8f40866c 245 # hash map of name and attribute spec pairs
7a50b450 246 while(my($name, $attr) = each %{$attributes}){
247 $meta->add_attribute($name => $attr);
248 }
249 }
250 }
8f40866c 251 if (defined $methods) {
252 while(my($method_name, $method_body) = each %{$methods}){
253 $meta->add_method($method_name, $method_body);
7a50b450 254 }
255 }
8f40866c 256 if (defined $roles){
257 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 258 }
259
8f40866c 260 if($cache_key){
7a50b450 261 $IMMORTALS{$cache_key} = $meta;
262 }
263
264 return $meta;
265 }
266
267 sub DESTROY{
268 my($self) = @_;
269
270 my $serial_id = $self->{anon_serial_id};
271
272 return if !$serial_id;
273
e75f21db 274 # @ISA is a magical variable, so we clear it manually.
275 @{$self->{superclasses}} = () if exists $self->{superclasses};
276
277 # Then, clear the symbol table hash
278 %{$self->namespace} = ();
7a50b450 279
8f40866c 280 my $name = $self->name;
281 delete $METAS{$name};
282
e75f21db 283 $name =~ s/ $serial_id \z//xms;
7a50b450 284
285 no strict 'refs';
8f40866c 286 delete ${$name}{ $serial_id . '::' };
7a50b450 287
288 return;
289 }
290}
291
8536d351 292sub throw_error{
293 my($class, $message, %args) = @_;
294
fce211ae 295 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 296 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 297
8536d351 298 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
299 Carp::croak($message);
300 }
301 else{
302 Carp::confess($message);
303 }
304}
3a63a2e7 305
3061;
307
308__END__
309
310=head1 NAME
311
1820fffe 312Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 313
a25ca8d6 314=head1 VERSION
315
b06ce1f5 316This document describes Mouse version 0.40_01
a25ca8d6 317
b995be6a 318=head1 SEE ALSO
319
31c5194b 320L<Class::MOP::Class>
321
322L<Class::MOP::Module>
323
324L<Class::MOP::Package>
b995be6a 325
3a63a2e7 326=cut
2cea7a5f 327