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